-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectCode - Copy.ino
More file actions
374 lines (260 loc) · 10 KB
/
projectCode - Copy.ino
File metadata and controls
374 lines (260 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include <Keypad.h>
//#include "TouchScreen.h" // only when you want to use touch screen#
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SoftwareSerial.h>
#include "Fonts/FreeSans9pt7b.h" // when you want other fonts
#include "Fonts/FreeSans12pt7b.h" // when you want other fonts
#include "Fonts/FreeSerif12pt7b.h" // when you want other fonts
#include "FreeDefaultFonts.h" // when you want other fonts
#include "SPI.h" // using sdcard for display bitmap image
#include "SD.h"
#include <ZMPT101B.h>
#define SENSITIVITY 500.0f
#define BLACK 0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define oneWireBus 11
#define voltageSensor A8
#define currentSensor A9
#define tempSensor A5
#define relay 10
ZMPT101B voltageSensor1(A8, 50.0);
int sensitivity = 66;
int offsetVoltage = 2500;
int adcVoltage = 0;
int TimeCounter = 0;
int TotalVoltage =0;
int TotalCurrent = 0;
float CurrentVoltage = 0;
float CurrentCurrent =0;
int startTime = millis();
//varaible inidacting start of the program
int startProgram = 0;
SoftwareSerial _serial(18,19);
void drawScreen();
float readTemperature();
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; //{39, 41, 43, 45};
byte colPins[COLS] = {30, 32, 34, 36};//{47, 49, 51, 53};
String textInput = "";
int charCounter = 0;
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int _voltageSensor = 0;
int _currentSensor = 0;
int currentValue = 0;
int _tempSensor = 0;
bool _startCharging = 0;
float temp1 = 0.0;
int AmountPaid = 0;
String safetyLevel = "G";
String voltage, temp, current="";
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
//(int CS=A3, int RS=A2, int WR=A1, int RD=A0, int RST=A4)
MCUFRIEND_kbv tft(A3, A2, A1, A0, A4);
void setup()
{
sensors.begin();
Serial.begin(9600);
_serial.begin(9600);
_serial.println("AT");
_serial.println("AT+CMGF=1");
uint16_t ID = tft.readID();
// uint16_t identifier = 0x9341;
Serial.println("Starting--- code ");
tft.begin(ID);
Serial.print("TFT ID is : ");
Serial.print(ID);
//screen resolution
uint16_t width = tft.width(); //int16_t width(void);
uint16_t height = tft.height(); //int16_t height(void);
pinMode(voltageSensor, INPUT);
pinMode(currentSensor, INPUT);
pinMode(relay, OUTPUT);
Serial.print("Screen resolution is :");
Serial.print(width);
Serial.print(" ");
Serial.print(height);
voltageSensor1.setSensitivity(SENSITIVITY);
}
void loop(){
int currentTime = millis();
char receivedData ;
//get the voltage value of the voltage sensor
_voltageSensor = analogRead(voltageSensor);
float voltage1 = voltageSensor1.getRmsVoltage();
Serial.print(" Voltage sensor is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " );
Serial.println(_voltageSensor);
if(_voltageSensor > 400){
voltage = "0";
_voltageSensor = 0;
}
voltage = voltage1;
Serial.print("line 180 voltage is : >> ");
Serial.println(voltage);
_currentSensor = analogRead(currentSensor);
Serial.print(" current sensor is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
Serial.println(_currentSensor);
adcVoltage = (_currentSensor / 1024.0) * 5200;
currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
//display value from current sensor in the screen
current = currentValue;
// _tempSensor = analogRead(tempSensor);
Serial.print("current is : >> ");
Serial.println(current);
if(currentTime - startTime > 200)
{
TotalVoltage += _voltageSensor;
TotalCurrent += currentValue;
TimeCounter ++;
}
/*
/*
Average current and voltage
*/
current = TotalCurrent / TimeCounter;
voltage = TotalVoltage / TimeCounter;
// sensors.requestTemperatures();
// temp = sensors.getTempCByIndex(0);
temp1 = readTemperature();
Serial.print("temperature is : >>> ");
Serial.print(temp1);
Serial.println(" C");
//paramters that must be true to start charging
if(_tempSensor < 80){
_startCharging = 1;
digitalWrite(relay, HIGH);
}
//Stop Charging in case temperature sensor is above room temperature
if(_tempSensor > 80 ){
// variable controlling charging
_startCharging = 0;
digitalWrite(relay, LOW);
}
if(_startCharging){
//Serial.println("Charging has Started");
digitalWrite(relay, HIGH);
}
// Serial.print("Voltage sensor value ");
// Serial.println(_voltageSensor);
// Serial.print("current sensor value ");
// Serial.println(_currentSensor);
if (startProgram == 0 ){
drawScreen(); mbv
//after the frist run don't draw the screen again
startProgram = 1;
}
// draw screen for the first time
// drawScreen();
//Screen Functionality code
char enter = '#';
char next = '*';
char clr = 'C';
char options = 'D';
char customKey = customKeypad.getKey();
if (customKey == enter){
//voltage = textInput;
textInput = "";
Serial.println(customKey);
Serial.print("Voltage");
Serial.println(voltage);
drawScreen();
}
else if (customKey == next){
current = textInput;
textInput = "";
Serial.println(customKey);
Serial.print("current");
Serial.println(current);
drawScreen();
}
else if (customKey == clr){
current= "";
voltage ="";
textInput="";
safetyLevel = "";
AmountPaid = 0;
drawScreen();
}else if(customKey == options){
// Draw page for more settings
Serial.println("Options table ");
}
else if(customKey){
textInput = textInput + customKey;
Serial.print("Text Input :");
Serial.println(textInput);
}
}
void drawScreen(){
Serial.println(" Drawing screen ");
tft.setTextColor(BLACK);
tft.fillScreen(DARKGREY);
tft.setTextSize(2);
tft.setCursor(0,0);
tft.print("Charging Station \n");
tft.setCursor(0,60);
tft.print("Voltage : >> ");
tft.println(voltage);
tft.setCursor(0,90);
tft.print("Current : >> ");
tft.println(current);
tft.setTextSize(2);
tft.setCursor(0,120);
tft.print("Battery Safety:>> ");
tft.println(safetyLevel);
tft.setCursor(0,200);
tft.print("Next option press *");
tft.setCursor(0,250);
tft.print("Amount Paid ");
tft.println(AmountPaid);
tft.setCursor(0,270);
tft.print("Temperature:>> ");
tft.println(temp1);
tft.setTextSize(1.5);
tft.println("Press C to clr scr");
tft.setCursor(0, 300);
tft.print("We're Number 1");
}
float readTemperature(){
sensors.requestTemperatures();
// Serial.print("Dallas temp resolution ");
float temp = 0.0;
// Serial.println(sensors.getResolution());
//delay(500);
// sensors.getWaitForConversion();
temp = sensors.getTempCByIndex(0);
if(temp != -127){
Serial.print("the temperature is ");
Serial.println(temp);
return temp;
}else{
Serial.println("Error occurred when reading temperature value ");
return -1000.0;
}
}