-
Notifications
You must be signed in to change notification settings - Fork 0
/
charge-mon-add-2nd-amp.ino
544 lines (469 loc) · 15.1 KB
/
charge-mon-add-2nd-amp.ino
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <ADS1115.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <Wire.h>
ADS1115 adc; /* Use this for the 16-bit version */
//LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); // ARDUINO UNO
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //mega
LiquidCrystal_I2C lcd2(0x27, 20, 4);
const byte
btLeft=9,
btRight=10,
btUp=11,
btDown=12,
btOk=13;
byte buttons[] = {9,10,11,12,13}; //10 for additional pin
const byte nrButtons = 5; // change to 4 if additional button added
int t;
byte pressedButton;
String FirstLine="Voltage Current ";
String sMenu="Menu";//16
const byte cMenu=16;
String sOVP="OVP";
const byte cOVP=17;
String sUVP="UVP";
const byte cUVP=17;
String sOCP="OCP";
const byte cOCP=17;
String sVolt="VOLTAGE";//0
const byte cVolt=0;
String sCurrent="CURRENT";//8
const byte cCurrent=8;
char sRun;
const byte cpvVolt=0;
const byte cpvCurrent=10;
int UVVal=0,OVVal=0,OCVal=0;
int editvolt=1,
editcurrent=2,
eUVVal=1, //edit Under Voltage
eOVVal=2,
eOCVal=3,
isview=4,
aOV=0,
aUV=0,
aOC=0,
aaNormal=0,
aaBlinking=1,
aaAck=2,
statusmenu,cstatusmenu;
//=================================================================================================================================================================================================================================
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 4095;
void EEPROMWriteInt(int address, int value)
{
byte two = (value & 0xFF);
byte one = ((value /256) & 0xFF);
EEPROM.update(address, two);
EEPROM.update(address + 1, one);
}
int EEPROMReadInt(int address)
{
byte two = EEPROM.read(address);
byte one = EEPROM.read(address + 1);
return ((two ) & 0xFFFF) + ((one *256) & 0xFFFF);
}
//=================================================================================================================================================================================================================================
byte checkButtonPress() {
byte bP = 0;
byte rBp = 0;
for (t = 0; t<nrButtons;t++) {
if (digitalRead(buttons[t]) == 0) {bP = (t + 1);}
}
rBp = bP;
while (bP != 0) { // wait while the button is still down
bP = 0;
for (t = 0; t<nrButtons;t++) {
if (digitalRead(buttons[t]) == 0) {bP = (t + 1);}
}
}
return rBp;
}
float aadc0, // Volt
aadc1, // current
realcurrent,
maxvoltref
;
int mVperAmp = 66,i=0;
float adc0, adc1, adc2, adc3,adc1_per_max,v,halfref,avolt,aamp;
ADS1115ScaleFloat scale0,scale1,scale2;
byte customBackslash[8] = {
0b00000,
0b10000,
0b01000,
0b00100,
0b00010,
0b00001,
0b00000,
0b00000
};
void setup() {
lcd2.createChar(7, customBackslash);
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) //
//ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
// ads.setSPS(ADS1015_REG_CONFIG_DR_3300SPS)
//ads.begin();
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) //
// ads.setSPS(ADS1015_REG_CONFIG_DR_3300SPS)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
Wire.begin();
adc.setSpeed(ADS1115_SPEED_860SPS);//ADS1115_SPEED_8SPS
// adc.setSpeed(ADS1115_SPEED_250SPS );//ADS1115_SPEED_8SPS
pinMode(9,INPUT_PULLUP);
pinMode(10,INPUT_PULLUP);
pinMode(11,INPUT_PULLUP);
pinMode(12,INPUT_PULLUP);
pinMode(13,INPUT_PULLUP);
//undervoltagevalue=0;overvoltagevalue=0;overcurrentvalue=0;
int show=1;
int edit=2;
int save=3;
int status;
lcd2.init();
lcd2.backlight();
lcd2.setCursor(cVolt,0);
//lcd2.print(sVolt);
lcd2.setCursor(cCurrent,0);
//lcd2.print(sCurrent);
pinMode(8,OUTPUT);
digitalWrite(8,0);
lcd2.createChar(7, customBackslash);
lcd.begin(16,2);
lcd.clear();
lcd2.setCursor(0,0);
lcd2.print(FirstLine);
// EEPROMWriteInt(2,1300); //one time set //130.0 volt default value for initializing new hardware
// EEPROMWriteInt(4,1000); //100.0 volt
// EEPROMWriteInt(8,100); // 10.0 volt
OVVal=EEPROMReadInt(2);
UVVal=EEPROMReadInt(4);
OCVal=EEPROMReadInt(8);
cstatusmenu=isview;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250/2; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
int irun=0;
int sblink=0;
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
sblink=(!(sblink && 1))&&1;
/*
if(cstatusmenu==isview){
if (sblink==1){
lcd2.setCursor(19,0);
lcd2.print(1);
}
if (sblink==0){
lcd2.setCursor(19,0);
lcd2.print(0);
}
}
*/
if(irun>=3){irun=0;}else {irun++;}
switch (irun){
case 0:sRun='-';
break;
case 1:sRun=7;
break;
case 2:sRun='|';
break;
case 3:sRun='/';
break;
}
}
void loop() {
/*
if(irun>=3){irun=0;}else {irun++;}
switch (irun){
case 0:sRun='-';
break;
case 1:sRun=7;
break;
case 2:sRun='|';
break;
case 3:sRun='/';
break;
}
*/
if(cstatusmenu==isview){
/// test blinking
/*
if (sblink==1){
lcd2.setCursor(18,0);
lcd2.print(1);
}
if (sblink==0){
lcd2.setCursor(18,0);
lcd2.print(0);
}
*/ // end test blinking
lcd2.setCursor(19,0);
lcd2.print(sRun);
v=0;
avolt=0;
maxvoltref=0;
pressedButton = checkButtonPress();
if (pressedButton ==0) { // jika ada press button abaikan proses
for(int ii=0;ii<13;ii++){
v +=adc.convert(ADS1115_CHANNEL1, ADS1115_RANGE_6144);// +-6144
avolt +=adc.convert(ADS1115_CHANNEL0, ADS1115_RANGE_6144);// +-6144
adc3 +=adc.convert(ADS1115_CHANNEL3, ADS1115_RANGE_6144);// +-6144
maxvoltref +=adc.convert(ADS1115_CHANNEL2, ADS1115_RANGE_6144);
}
}
v=v/13;
avolt=avolt/13;
adc3=adc3/13;
maxvoltref=maxvoltref/13;
halfref=maxvoltref/2;
scale0.setRef(halfref+2.5, 0,maxvoltref, 30.000);
scale1.setRef(0,0,maxvoltref,150.000);
scale2.setRef(14312.24, 0,maxvoltref, 50.000);
//20-02-16-21:25
// if (cstatusmenu==isview){
if (scale1.scale(avolt)<(UVVal/10.0)){ // check Over Voltage
// lcd2.setCursor(cVolt,3);
// lcd2.print("UV ALARM");
if (aUV!=aaAck){
aUV=aaBlinking;
}
}
else{
// lcd2.setCursor(cVolt,3);
// lcd2.print(" ");
if (aUV==aaAck){aUV=aaNormal;}
}
if (aUV==aaBlinking){ // handle blinking when Volt low threshold reach
if (sblink==1){
lcd2.setCursor(cVolt,3);
lcd2.print("UV ALARM");
}
if (sblink==0){
lcd2.setCursor(cVolt,3);
lcd2.print(" ");
}
}
if (aUV==aaAck){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cVolt,3);
lcd2.print("UV ALARM");
}
if (aUV==aaNormal){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cVolt,3);
lcd2.print(" ");
}
/// end Under Voltage
if (scale1.scale(avolt)>(OVVal/10.0)){ // check Under Voltage
// lcd2.setCursor(cVolt,1);
// lcd2.print("OV ALARM");
if (aOV!=aaAck){
aOV=aaBlinking;
}
}
else{
// lcd2.setCursor(cVolt,1);
// lcd2.print(" ");
if (aOV==aaAck){aOV=aaNormal;}
}
if (aOV==aaBlinking){ // handle blinking when Volt low threshold reach
if (sblink==1){
lcd2.setCursor(cVolt,1);
lcd2.print("OV ALARM");
}
if (sblink==0){
lcd2.setCursor(cVolt,1);
lcd2.print(" ");
}
}
if (aOV==aaAck){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cVolt,1);
lcd2.print("OV ALARM");
}
if (aOV==aaNormal){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cVolt,1);
lcd2.print(" ");
}
/// end over voltage
if (scale0.scale(v)>(OCVal/10.0)){ // check Over Current
// lcd2.setCursor(cCurrent,1);
// lcd2.print("OC ALARM");
if (aOC!=aaAck){
aOC=aaBlinking;
}
}
else{
// lcd2.setCursor(cCurrent,1);
// lcd2.print(" ");
if (aOC==aaAck){aOC=aaNormal;}
}
if (aOC==aaBlinking){ // handle blinking when Volt low threshold reach
if (sblink==1){
lcd2.setCursor(cCurrent,1);
lcd2.print("OC ALARM");
}
if (sblink==0){
lcd2.setCursor(cCurrent,1);
lcd2.print(" ");
}
}
if (aOC==aaAck){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cCurrent,1);
lcd2.print("OC ALARM");
}
if (aOC==aaNormal){ // handle blinking when Volt low threshold reach
lcd2.setCursor(cCurrent,1);
lcd2.print(" ");
}
kirimmsg();
lcd2.setCursor(cVolt,2);
lcd2.print(scale1.scale(avolt),1); //new
lcd2.print(" "); /////real
lcd2.setCursor(cCurrent,2);
lcd2.print(scale0.scale(v),1); /////real
lcd2.print(" "); /////real
//scale1.scale(avolt)
lcd2.setCursor(cCurrent,3);
lcd2.print(scale2.scale(adc3),1); /////real
lcd2.print(" "); /////real
}
pressedButton = checkButtonPress();
if (pressedButton !=0) {
cstatusmenu=eOVVal;
switch (pressedButton) {
// If I wanted a 4 button controll of the menu
case 1: //Left - Up button
switch (statusmenu){
case 1:if(OVVal>0){ OVVal--;}
lcd2.setCursor(0,1);lcd2.print(" ");
lcd2.setCursor(0,1);lcd2.print((OVVal/10.0),1);
break;
case 2:if (UVVal>0){ UVVal--;}
lcd2.setCursor(0,1);lcd2.print(" ");
lcd2.setCursor(0,1);lcd2.print((UVVal/10.0),1);
break;
case 3:if (OCVal>0){OCVal--;}
lcd2.setCursor(0,1);lcd2.print(" ");
lcd2.setCursor(0,1);lcd2.print((OCVal/10.0),1);
break;
}
break;
case 2: // Right Down
//lcd.setCursor(0,0); lcd.print("Button 1"); //up +++
switch (statusmenu){
case 1:if(OVVal<10000){ OVVal++;}
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((OVVal/10.0),1);
break;
case 2:if (UVVal<10000){ UVVal++;}
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((UVVal/10.0),1);
break;
case 3:if (OCVal<10000){OCVal++;}
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((OCVal/10.0),1);
break;
}
break;
case 3:
// saving an edited parameter
switch (statusmenu){
case 1:
EEPROMWriteInt(2,OVVal);
break;
case 2:
EEPROMWriteInt(4,UVVal);
break;
case 3:
EEPROMWriteInt(8,OCVal);
break;
}
break;
//end saving / update parameter
case 4:// Menu
lcd2.setCursor(0,1);lcd2.print(" ");
lcd2.setCursor(0,0);
if (statusmenu<3){statusmenu++;}else statusmenu=0;
switch (statusmenu){
case 1:
lcd2.print("Over Voltage Set ");
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((OVVal/10.0),1);
cstatusmenu=eOVVal;
break;
case 2:
lcd2.print("Under Voltage Set");
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((UVVal/10.0),1);
cstatusmenu=eUVVal;
break;
case 3:
lcd2.print("Over Current Set ");
lcd2.setCursor(0,1); lcd2.print(" ");
lcd2.setCursor(0,1); lcd2.print((OCVal/10.0),1);
cstatusmenu=eOCVal; //
break;
case 0:
lcd2.setCursor(0,0);
lcd2.print(FirstLine);
lcd2.setCursor(0,1);
lcd2.print(" ");
lcd2.setCursor(0,2);
lcd2.print(" ");
lcd2.setCursor(0,3);
lcd2.print(" ");
cstatusmenu=isview; //
lcd2.setCursor(0,1);lcd2.print(" ");
break;
}
break; // case 4 Main menu
case 5: // Ack / Buzzer Stop;
if(aUV==aaBlinking){
aUV=aaAck;
}
if(aOV==aaBlinking){
aOV=aaAck;
}
if(aOC==aaBlinking){
aOC=aaAck;
}
cstatusmenu=isview;
break; // case 5;
}
}
}// loop
void kirimmsg(void){
/*
Serial.print("AIN0: raw "); Serial.println(adc0);
Serial.print("AIN1: raw "); Serial.println(adc1);
Serial.print("AIN0: calc "); Serial.println(aadc0);
Serial.print("AIN1: real "); Serial.println(adc.convert(ADS1115_CHANNEL1, ADS1115_RANGE_6144));
Serial.print("AIN1: mv "); Serial.println(scale0.scale(v),4);
Serial.print("AIN0: scle "); Serial.println(scale1.scale(avolt),4);
Serial.print("AIN2: "); Serial.println(adc2);
Serial.print("AIN3: "); Serial.println(adc3);
Serial.print("Half Ref "); Serial.println(halfref);
*/
Serial.print("mvxref ["); Serial.print(maxvoltref); Serial.println("]");
Serial.print("mvhref ["); Serial.print(halfref); Serial.println("]");
Serial.print("AIN0 Raw["); Serial.print(avolt); Serial.println("]");
Serial.print("AIN1 Raw["); Serial.print(v); Serial.println("]");
Serial.print("AIN2 Raw["); Serial.print(maxvoltref); Serial.println("]");
Serial.print("AIN3 Raw["); Serial.print(adc3); Serial.println("]");
Serial.print("Volt ["); Serial.print(scale1.scale(avolt),1); Serial.println("]");
Serial.print("Amp ["); Serial.print(scale0.scale(v),2); Serial.println("]");
Serial.print("AVolt ["); Serial.print(v); Serial.println("]");
}