-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectCode.ino
More file actions
445 lines (364 loc) · 9.67 KB
/
projectCode.ino
File metadata and controls
445 lines (364 loc) · 9.67 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include <Keypad.h>
#include <mcp_can.h>
#include <DHT11.h>
#include <ZMPT101B.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 "newLock.h"
#include "CAN_COMMUNICATION.h"
// #include "GSM.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 tempSensor A5
#define relay 10
ZMPT101B voltageSensor1(A8, 50.0);
#define voltageSensor A8
#define currentSensor A9
// Temperature that determine thermal runaway
int inttemp = 65;
int outtemp = 80;
float sensitivity = 0.100;
int offsetVoltage = 2.5;
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;
int dirPin = 15; // CW+
int stepPin = 20; //14; CLk+
int enPin = 16; // EN+
int stepsPerRevolution = 1600; // for 1/8 step mode
int angle = 0;
int step = 0;
int stepDelay = 100;
int checkAngle = 0;
int a = 0;
bool direction = true; // true for forward, false for reverse
void drawScreen();
float readTemperature();
void drawScreenBAT();
void fireSuppression();
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 = false;
bool _fireSuppression = false;
float temp1 = 0.0;
int AmountPaid = 0;
String safetyLevel = "G";
String voltage, temp, current = "";
DHT11 dht11(38);
Lock door(41);
char enter = '#';
char next = '*';
char clr = 'C';
char battery = 'B';
char options = 'D';
//(int CS=A3, int RS=A2, int WR=A1, int RD=A0, int RST=A4)
MCUFRIEND_kbv tft(A3, A2, A1, A0, A4);
int temperature = 0;
// CAN_PROTOCOL can(17, 53);
void setup()
{
Serial.begin(9600);
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);
Serial.print("Screen resolution is :");
Serial.print(width);
Serial.print(" ");
Serial.print(height);
voltageSensor1.setSensitivity(SENSITIVITY);
pinMode(voltageSensor, INPUT);
pinMode(currentSensor, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
// mySerial.begin(9600);
// Serial.println("Initializing...");
// delay(100);
// Serial.println("<><><><><><<><>>>> AT");
// mySerial.println("AT"); // Sends an ATTENTION command, reply should be OK
// updateSerial();
// delay(100);
// mySerial.println("AT+CPIN?");
// updateSerial();
// delay(100);
// mySerial.println("AT+CPIN=9574");
// updateSerial();
// delay(100);
// Serial.println(">>>>>>>>>>>>>>>> AT+CPIN"); // printing out serial
// Serial.println(" >>>>>>>>>>>>>>>> AT+CREG? ");
// delay(100);
// mySerial.println("AT+CREG?");
// updateSerial();
// delay(100);
// mySerial.println("AT+CMGF=1"); // Configuration for sending SMS
// delay(100);
// Serial.println("><><><><><><<><>>>> AT+CMFG=1"); // printing out serial for debugging
}
void loop()
{
// if (clientPower > 0.0)
// {
// _startCharging = true;
// }
// else
// {
// _startCharging = false;
// }
if (_startCharging)
{
Serial.println("Charging has Started");
// close the door
door.lock(0);
digitalWrite(relay, LOW);
}
else if (_startCharging == false)
{
door.lock(1);
}
if (can.rSOC == 100)
{
_startCharging = false;
}
if (temperature > outtemp || can.NT1 > inttemp || can.NT2 > inttemp || can.NT3 > inttemp || can.NT4 > inttemp || can.NT5 > inttemp || can.NT6 > inttemp)
{
Serial.println("Fire Suppression has Started");
// close the door
fireSuppression();
}
//run can
can.canMain();
//open the door assume not charging
// previousTime = millis();
// updateSerial();
// delay(100);
// int currentTime = millis();
char receivedData;
// get the voltage value of the voltage sensor
float voltage1 = voltageSensor1.getRmsVoltage();
Serial.print(" Voltage sensor is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
Serial.println(voltage1);
if (voltage1 > 400)
{
voltage = "0";
voltage1 = 0;
}
voltage = voltage1;
_currentSensor = analogRead(currentSensor);
adcVoltage = (_currentSensor / 1023) * 5;
current = ((adcVoltage - offsetVoltage) / sensitivity);
// display value from current sensor in the screen
Serial.print(" current sensor is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
Serial.println(current);
temperature = dht11.readTemperature();
if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT)
{
Serial.print("Temperature: >>>>>>>>>>>");
Serial.print(temperature);
Serial.println(" °C");
}
else
{
Serial.println(DHT11::getErrorString(temperature));
}
if (startProgram == 0)
{
drawScreen();
// after the frist run don't draw the screen again
startProgram = 1;
}
// draw screen for the first time
// drawScreen();
// Screen Functionality code
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 == battery){
// Draw page for more settings
Serial.println("-BATTERY INFORMATION DISPLAY ");
drawScreenBAT();
}
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(String(voltage));
tft.setCursor(0, 90);
tft.print("Current : >> ");
tft.println(String(current));
tft.setTextSize(2);
tft.setCursor(0, 120);
tft.print("Battery Safety:>> ");
tft.println(String(safetyLevel));
tft.setCursor(0, 200);
tft.print("Next option press *");
tft.setCursor(0, 250);
tft.print("Amount Paid ");
tft.println(String(AmountPaid));
tft.setCursor(0, 270);
tft.print("Temperature:>> ");
tft.println(String(temperature));
tft.setTextSize(1.5);
tft.println("Press C to clr scr");
tft.setCursor(0, 300);
tft.print("We're Number 1");
}
void drawScreenBAT()
{
Serial.println(" Drawing CAN ");
tft.setTextColor(BLACK);
tft.fillScreen(DARKGREY);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.print("CAN INFORMATION ");
tft.setCursor(0, 60);
tft.print("BATTERY VOLTAGE : >> ");
tft.println(can.volt);
tft.setCursor(0, 90);
tft.print("BATTERY Current : >> ");
tft.println(can.curr);
tft.setTextSize(2);
tft.setCursor(0, 120);
tft.print("Battery % charge:>> ");
tft.println(can.rSOC);
tft.setCursor(0, 200);
tft.print("NTC1 :>>");
tft.println(can.NT1);
tft.setCursor(0, 250);
tft.print("NTC2 :>>");
tft.println(can.NT2);
tft.setCursor(0, 270);
tft.print("NTC3 :>> ");
tft.println(can.NT3);
tft.setTextSize(1.5);
tft.println("Press C to clr scr");
tft.setCursor(0, 300);
tft.print("We're Number 1");
}
void movingFN(int duration, bool direction)
{
Serial.print(" Moving Function ");
Serial.println(direction);
unsigned long startTime = millis();
while (millis() - startTime < duration)
{
if (checkAngle == 0)
{
a = +1;
}
if (checkAngle == 360)
{
a = -1;
}
angle = angle + a;
a = angle;
step = map(a, 0, 360, 0, stepsPerRevolution);
digitalWrite(dirPin, direction ? HIGH : LOW);
for (int i = 0; i < step; i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
}
}
void fireSuppression()
{
Serial.println(" &&&&&&&&&&& Fire suppression && ");
movingFN(1000, true); // Move forward for 5 seconds
delay(500); // Pause for 2 seconds
movingFN(5000, false); // Move in reverse for 5 seconds
delay(1000); // Pause for 2 seconds
}