-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoilerBalancer.ino
787 lines (730 loc) · 21.7 KB
/
BoilerBalancer.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//////////////////////////////////////////////////////
// //
// Balance Boiler 1.07 //
// //
//////////////////////////////////////////////////////
// INCLUSIONS
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EasyTransfer.h>
// DEFINITIONS
#define _DEBUG_ true
#define _TXSERIAL_ false
#define VERSION 1.07
#define NUMBOILER 6 // number of boiler [min 2 max 6]
#define ONE_WIRE_BUS 9 // one wire pin DS18B20
const int RelayPin[] = {2, 3, 4, 5, 6, 7}; // relay attached to this pin {A, B, C, D, E, F}
DeviceAddress TempAddress[]= {
{ 0x28, 0xFF, 0x24, 0x3C, 0x84, 0x16, 0x04, 0x2E }, // A
{ 0x28, 0xFF, 0x38, 0xD3, 0x84, 0x16, 0x05, 0xFE }, // B
{ 0x28, 0xFF, 0x92, 0x27, 0x85, 0x16, 0x05, 0xA4 }, // C
{ 0x28, 0xFF, 0x2B, 0x81, 0x63, 0x16, 0x03, 0x73 }, // D
{ 0x28, 0xFF, 0x4F, 0x7C, 0x63, 0x16, 0x03, 0x91 }, // E
{ 0x28, 0xFF, 0x7E, 0xC9, 0x73, 0x16, 0x04, 0x01 } // F
};
//////////////////////////////////////////////////////
/////////////// END configuration Part ///////////////
//////////////////////////////////////////////////////
#define MODE_ALL_OFF 0 // Mode all relay OFF
#define MODE_ALL_ON 1 // Mode all relay ON
#define MODE_AUTO 2 // Mode Automatic
#define ITEM_OFF 0 // Menu Item OFF
#define ITEM_ON 1 // Menu Item ON
#define ITEM_MODE 2 // Menu Item Mode
#define OPEN_VALVE HIGH // relay OFF valve OPEN
#define CLOSE_VALVE LOW // relay ON valve CLOSE
// EEPROM
#define DEFAULTON 60 // default centigrade temp for on relay
#define DEFAULTOFF 40 // default centigrade temp for off relay
#define EEMAGICADDR 0 // eeprom address of magic byte
#define EEMAGICVALUE 78 // eeprom value of magic byte
#define EETEMP_OFF_ADDR 2 // eeprom address of OFF temperaure
#define EETEMP_ON_ADDR 3 // eeprom address of ON temperaure
#define EEMODE_ADDR 4 // eeprom MODE address (save current active MODE)
// BUTTON
#define BTNUP 0
#define BTNDWN 1
#define BTNSEL 2
//DS18B20
#define TEMPERATURE_PRECISION 9
// system
unsigned long currentMillis = 0;
unsigned long previousMillis = 0; // millis() returns an unsigned long.
int SelectedItem = ITEM_MODE; // 0 = ITEM_OFF 1 = ITEM_ON 2 = ITEM_MODE
int ActiveMode = MODE_AUTO; // default MODE
const char LetterBoiler[] = {"ABCDEF"};
const String ModeName[] = {"ALL OFF","ALL ON"," AUTO"};
const long TimerSave = 30000; // save data 30 seconds after last push button
const long TimerStandby = 60000; // go in standby 1 minute after last push button
const long TimerTemperature = 1000; // read temperature every 1 second
const long TimerSerialData = 10000; // send data temperature on serial line every 10 seconds
const long TimerSwitch = 10000; // switch one relay at least after 10 seconds from other relay switch
unsigned long LastReadTemperature= TimerTemperature + 1;
unsigned long LastAction = TimerStandby + 1;
unsigned long LastSendData = 0;
unsigned long LastSwitch = TimerSwitch + 1; // init at high value for start switching immediatly after power on
boolean Saved = false;
boolean StandBy = true;
// buttons
const int ButtonPin[] = {10, 11, 12}; // push button is attached to this pin
int ButtonState[] = {HIGH,HIGH,HIGH};
boolean ButtonActive[] = {false,false,false};
const long debounceDelay = 50;
// relay
boolean RelayStatus[] = {false, false, false, false, false, false};
boolean RelaySafeMode[] = {false, false, false, false, false, false};
// temperature
int OnTemp = DEFAULTON;
int OffTemp = DEFAULTOFF;
int TempValue[] = {0,0,0,0,0,0};
// LCD
#define I2C_ADDR 0x20
#define BACKLIGHT_PIN 7
#define En_pin 4
#define Rw_pin 5
#define Rs_pin 6
#define D4_pin 0
#define D5_pin 1
#define D6_pin 2
#define D7_pin 3
const int startPositionLCD[][2] = {{0 ,0}, {7 ,0}, {14,0}, {0 ,1}, {7 ,1}, {14,1}}; // column and row position of each temp value on LCD
// custom char
byte customDeg[8] = {B01000, B10100, B01000, B00011, B00100, B00100, B00011, B00000};
byte customON[8] = {B00000, B00000, B01100, B11110, B11110, B01100, B00000, B00000};
byte customUpArrow[8] = {B00100, B01110, B10101, B00100, B00100, B00100, B00000, B00000};
// DECLARATION
// DS18B20 library
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// LCD Library
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// EasyTransfer object
EasyTransfer ET;
struct SEND_DATA_STRUCTURE{
int TempValue[];
boolean RelayStatus[];
int ActiveMode;
};
SEND_DATA_STRUCTURE txData;
//
// SETUP
//
void setup(void) {
Serial.begin(9600);
if (_DEBUG_) {
Serial.print("BOILER: ");
Serial.println(NUMBOILER);
}
if (_TXSERIAL_) {
ET.begin(details(txData), &Serial);
}
initEEprom();
initRelay();
initButton();
initLCD();
initSensor();
delay(5000);
clearLCDtop();
checkChangeMode();
}
//
// LOOP
//
void loop(void) {
currentMillis = millis();
checkButtons();
scanSensors();
refreshLCD();
saveData();
sendSerialData();
}
void tempControl(void) {
if (ActiveMode == MODE_AUTO) {
for (int i = 0; i < NUMBOILER; i++) {
// check if some sensor are in error
if (TempValue[i] == -127) {
// sensor in error - OPEN VALVE
//doVALVE(i, OPEN_VALVE, false);
}
else {
if (RelayStatus[i]) {
// Relay already ON valve OPEN
if (!RelaySafeMode[i]) { // check if not in SafeMode
if (TempValue[i] <= OffTemp) {
// CLOSE valve - the temperature is cold
if (_DEBUG_) Serial.print("CLOSE valve: ");
if (_DEBUG_) Serial.println(LetterBoiler[i]);
doVALVE(i, CLOSE_VALVE, false);
}
}
else {
// Exit Safe Mode from Relay already ON
// and Temperature is mucher than midTemp
int midTemp = (OnTemp + OffTemp) / 2;
if (TempValue[i] >= midTemp) exitSafeMode(); // exit from SafeMode
}
}
else {
// Relay already OFF valve CLOSE
if (!isSafeMode()) {
// not in SAFE MODE
if (TempValue[i] >= OnTemp) {
// OPEN valve - the temperature is HOT
if (_DEBUG_) Serial.print("OPEN valve: ");
if (_DEBUG_) Serial.println(LetterBoiler[i]);
doVALVE(i, OPEN_VALVE, false);
if (isSafeMode()) exitSafeMode(); // exit from SafeMode
}
}
else {
// in SAFE MODE
// Exit Safe Mode from Relay already OFF
// and Temperature is mucher than midTemp
int midTemp = (OnTemp + OffTemp) / 2;
if (TempValue[i] >= midTemp) exitSafeMode();
}
}
}
}
checkSafeMode();
}
}
void initAutoMode(void) {
if (ActiveMode == MODE_AUTO) {
int midTemp = (OnTemp + OffTemp) / 2;
if (_DEBUG_) Serial.print("Mid Temp: ");
if (_DEBUG_) Serial.println(midTemp);
for (int i = 0; i < NUMBOILER; i++) {
if (TempValue[i] <= midTemp) {
// CLOSE valve - the temperature is cold
if (_DEBUG_) Serial.print("CLOSE valve: ");
if (_DEBUG_) Serial.println(LetterBoiler[i]);
doVALVE(i, CLOSE_VALVE, false);
}
else {
// OPEN valve - the temperature is hot
if (_DEBUG_) Serial.print("OPEN valve: ");
if (_DEBUG_) Serial.println(LetterBoiler[i]);
doVALVE(i, OPEN_VALVE, false);
}
}
}
}
void checkSafeMode(void) {
if (ActiveMode == MODE_AUTO) {
if (isAllClose()) {
// if all VALVE are CLOSED
// OPEN the most hot
int pos = foundMaxTemp();
doVALVE(pos, OPEN_VALVE, true);
}
else {
if (isSafeMode()) {
// already in safe mode
// check if there are other HOT boiler
// and OPEN it
int pos = foundMaxTemp();
for (int i = 0; i < NUMBOILER; i++) {
if (RelaySafeMode[i]) {
if (i != pos) {
if ((unsigned int)(TempValue[i] - TempValue[pos]) > 3) {
doVALVE(pos, OPEN_VALVE, true);
doVALVE(i, CLOSE_VALVE, true);
i = NUMBOILER; // exit
}
}
}
}
}
}
}
}
boolean isSafeMode(void) {
// check if all VALVE are not in Safe Mode (false)
boolean tmp = false;
for (int i = 0; i < NUMBOILER; i++) {
if (RelaySafeMode[i]) {
tmp = true; // Found one VALVE in Safe Mode
i = NUMBOILER; // exit
}
}
if (_DEBUG_) {
if (tmp) Serial.println("Safe Mode");
}
return tmp;
}
void exitSafeMode(void) {
// exit from Safe Mode
// set all Relay SafeMode = false
for (int i = 0; i < NUMBOILER; i++) {
if (RelaySafeMode[i]) { // check if a VALVE is in SafeMode
//doVALVE(i, CLOSE_VALVE, true); // delete this line
RelaySafeMode[i] = false;
}
}
}
boolean isAllClose(void) {
// check if all VALVE are CLOSED (true)
boolean tmp = true;
for (int i = 0; i < NUMBOILER; i++) {
if (RelayStatus[i]) {
tmp = false; // Found one VALVE OPEN
i = NUMBOILER; // exit
}
}
if (_DEBUG_) {
if (tmp) Serial.println("All Valve are Close");
}
return tmp;
}
int foundMaxTemp(void) {
// found max sensor Temp
int maxTemp = -127; // store max sensor temp
int maxNumber; // store max sensor number
for (int i = 0; i < NUMBOILER; i++) {
if (TempValue[i] > maxTemp) {
maxNumber = i;
maxTemp = TempValue[i];
}
}
if (_DEBUG_) {
Serial.print("Max Temperature: ");
Serial.println(maxNumber);
}
return maxNumber;
}
void switchMode(boolean reverse) {
if (!reverse) {
switch (ActiveMode) {
case MODE_ALL_OFF:
ActiveMode = MODE_ALL_ON;
break;
case MODE_ALL_ON:
ActiveMode = MODE_AUTO;
initAutoMode();
break;
case MODE_AUTO:
ActiveMode = MODE_ALL_OFF;
break;
}
}
else {
switch (ActiveMode) {
case MODE_ALL_OFF:
ActiveMode = MODE_AUTO;
initAutoMode();
break;
case MODE_ALL_ON:
ActiveMode = MODE_ALL_OFF;
break;
case MODE_AUTO:
ActiveMode = MODE_ALL_ON;
break;
}
}
if (_DEBUG_) Serial.print("switchMode:");
if (_DEBUG_) Serial.println(ActiveMode);
if (_TXSERIAL_) txData.ActiveMode = ActiveMode;
checkChangeMode();
}
void checkButtons(void){
ButtonState[BTNSEL] = digitalRead(ButtonPin[BTNSEL]);
ButtonState[BTNUP] = digitalRead(ButtonPin[BTNUP]);
ButtonState[BTNDWN] = digitalRead(ButtonPin[BTNDWN]);
if ((unsigned long)(currentMillis - previousMillis) > debounceDelay){
if (ButtonState[BTNSEL] == LOW){
LastAction = currentMillis;
if (!ButtonActive[BTNSEL]){
if (_DEBUG_) Serial.println("Button SEL Pressed");
ButtonActive[BTNSEL] = true;
if (StandBy){
StandBy = false;
lcd.backlight();
}
else{
if (ActiveMode == MODE_AUTO){
switch (SelectedItem){
case ITEM_OFF:
// select ON ITEM
SelectedItem = ITEM_ON;
break;
case ITEM_ON:
// select MODE ITEM
SelectedItem = ITEM_MODE;
break;
case ITEM_MODE:
// select MODE OFF
SelectedItem = ITEM_OFF;
break;
}
writeCursorLCD();
}
}
}
}
else{
if (ButtonActive[BTNSEL]){
if (_DEBUG_) Serial.println("Button SEL Released");
ButtonActive[BTNSEL] = false;
}
}
if (ButtonState[BTNUP] == LOW){
LastAction = currentMillis;
if (!ButtonActive[BTNUP]){
if (_DEBUG_) Serial.println("Button UP Pressed");
ButtonActive[BTNUP] = true;
if (StandBy){
StandBy = false;
lcd.backlight();
}
else{
Saved = false;
switch (SelectedItem){
case ITEM_OFF:
// increase OFF Temp
if (OffTemp < 100 && OffTemp < OnTemp) OffTemp++;
break;
case ITEM_ON:
// increase ON Temp
if (OnTemp < 100) OnTemp++;
break;
case ITEM_MODE:
// change MODE
switchMode(true);
break;
}
}
}
}
else{
if (ButtonActive[BTNUP]){
if (_DEBUG_) Serial.println("Button UP Released");
ButtonActive[BTNUP] = false;
}
}
if (ButtonState[BTNDWN] == LOW){
LastAction = currentMillis;
if (!ButtonActive[BTNDWN]){
if (_DEBUG_) Serial.println("Button DWN Pressed");
ButtonActive[BTNDWN] = true;
if (StandBy){
StandBy = false;
lcd.backlight();
}
else{
Saved = false;
switch (SelectedItem){
case ITEM_OFF:
// decrease OFF Temp
if (OffTemp > 25) OffTemp--;
break;
case ITEM_ON:
// decrease ON Temp
if (OnTemp > 25 && OnTemp > OffTemp) OnTemp--;
break;
case ITEM_MODE:
// change MODE
switchMode(false);
break;
}
}
}
}
else{
if (ButtonActive[BTNDWN] == true){
if (_DEBUG_) Serial.println("Button DWN Released");
ButtonActive[BTNDWN] = false;
}
}
previousMillis = currentMillis;
}
}
void checkChangeMode(void) {
// 0 = MODE_ALL_OFF 1 = MODE_ALL_ON 2 = MODE_AUTO
clearLCDbottom();
switch (ActiveMode) {
case MODE_ALL_OFF:
if (isSafeMode()) exitSafeMode(); // exit from SafeMode
writeWaitLCD();
closeAllVALVE();
break;
case MODE_ALL_ON:
if (isSafeMode()) exitSafeMode(); // exit from SafeMode
writeWaitLCD();
openAllVALVE();
break;
case MODE_AUTO:
initAutoMode();
break;
}
if (_DEBUG_) Serial.print("Mode: ");
if (_DEBUG_) Serial.println(ModeName[ActiveMode]);
}
void saveData(void) {
if (((unsigned long)(currentMillis - LastAction) > TimerSave) && !Saved) {
if (_DEBUG_) Serial.println("Saving Data");
EEPROM.update(EETEMP_OFF_ADDR, OffTemp);
EEPROM.update(EETEMP_ON_ADDR, OnTemp);
EEPROM.update(EEMODE_ADDR, ActiveMode);
Saved = true;
}
}
// LCD Session
void refreshLCD(void) {
if (StandBy) {
screenSaver();
}
else {
if ((unsigned long)(currentMillis - LastAction) > TimerStandby) {
if (!StandBy) {
screenSaver();
StandBy = true;
}
}
else {
// write mode
writeModeLCD();
// write sensor temperatures
writeSensTempLCD();
// write temperature thresold
writeThersoldLCD();
// write cursor
writeCursorLCD();
}
}
}
void writeSensTempLCD(void) {
for (int i = 0; i < NUMBOILER; i++) {
lcd.setCursor(startPositionLCD[i][0],startPositionLCD[i][1]);
lcd.print(LetterBoiler[i]);
if (RelayStatus[i])
lcd.write((uint8_t)1); // ON Char
else {
lcd.print(" "); // space for OFF char
}
if (TempValue[i] == -127) {
lcd.print("err ");
}
else {
if (TempValue[i] < 10) {
lcd.print(" "); // add a space for one digit number
}
lcd.print(TempValue[i]);
lcd.write((uint8_t)0); // degree char
}
}
}
void writeCursorLCD(void) {
lcd.setCursor(7,3);
lcd.print(" "); // clear cursor space
switch (SelectedItem) {
case ITEM_OFF:
lcd.setCursor(11,3);
lcd.print((char)126); // right arrow
break;
case ITEM_ON:
lcd.setCursor(7,3);
lcd.print((char)127); // left arrow
break;
case ITEM_MODE:
lcd.setCursor(9,3);
lcd.write((uint8_t)2); // up arrow
break;
}
}
void writeModeLCD(void) {
// write Mode
lcd.setCursor(7,2);
lcd.print(ModeName[ActiveMode]);
lcd.setCursor(14,2);
if (isSafeMode()) {
lcd.print("SAFE ");
for (int i = 0; i < NUMBOILER; i++) {
if (RelaySafeMode[i]) {
lcd.print(LetterBoiler[i]);
}
}
}
else {
lcd.print(" ");
}
}
void writeWaitLCD(void) {
// write WAIT
lcd.setCursor(7,2);
lcd.print(" WAIT ");
lcd.setCursor(14,2);
lcd.print(" ");
}
void writeThersoldLCD(void) {
// write temp threshold if MODE_AUTO
if (ActiveMode == MODE_AUTO) {
lcd.setCursor(0,3);
lcd.print("ON ");
lcd.print(OnTemp);
lcd.write((uint8_t)0); // degree char
lcd.setCursor(13,3);
lcd.print("OFF ");
lcd.print(OffTemp);
lcd.write((uint8_t)0); // degree char
}
}
// init Session
void initLCD(void) {
if (_DEBUG_) Serial.println("initLCD");
lcd.begin(20,4);
lcd.backlight();
lcd.createChar(0, customDeg);
lcd.createChar(1, customON);
lcd.createChar(2, customUpArrow);
lcd.setCursor(0, 0);
lcd.print("Powered by");
lcd.setCursor(3, 1);
lcd.print("C.D. Impianti");
lcd.setCursor(5, 2);
lcd.print("tel 392/0581809");
lcd.setCursor(0, 3);
lcd.print("E#");
lcd.setCursor(15, 3);
lcd.print("v");
lcd.print(VERSION);
lcd.home();
}
void initRelay(void) {
if (_DEBUG_) Serial.println("initRelay");
for (int i = 0; i < NUMBOILER; i++) {
pinMode(RelayPin[i], OUTPUT);
}
closeAllVALVE();
}
void initButton(void) {
if (_DEBUG_) Serial.println("initButton");
pinMode(ButtonPin[BTNSEL], INPUT_PULLUP);
pinMode(ButtonPin[BTNDWN], INPUT_PULLUP);
pinMode(ButtonPin[BTNUP], INPUT_PULLUP);
}
void initEEprom(void) {
if (_DEBUG_) Serial.println("initEEPROM");
if (EEPROM.read(EEMAGICADDR) == EEMAGICVALUE) {
// already inizialized
if (_DEBUG_) Serial.println("EEPROM already inizialized");
OnTemp = EEPROM.read(EETEMP_ON_ADDR);
OffTemp = EEPROM.read(EETEMP_OFF_ADDR);
ActiveMode = EEPROM.read(EEMODE_ADDR);
if (_DEBUG_) Serial.print("EEPROM OnTemp: ");
if (_DEBUG_) Serial.println(OnTemp);
if (_DEBUG_) Serial.print("EEPROM OffTemp: ");
if (_DEBUG_) Serial.println(OffTemp);
if (_DEBUG_) Serial.print("EEPROM Mode: ");
if (_DEBUG_) Serial.println(ActiveMode);
}
else {
// first boot never inizialized
if (_DEBUG_) Serial.println("First Boot");
EEPROM.update(EEMAGICADDR, EEMAGICVALUE);
EEPROM.update(EETEMP_ON_ADDR, OnTemp);
EEPROM.update(EETEMP_OFF_ADDR, OffTemp);
EEPROM.update(EEMODE_ADDR, ActiveMode);
}
}
void initSensor(void) {
if (_DEBUG_) Serial.println("initSensor");
if (_DEBUG_) Serial.print("Found ");
if (_DEBUG_) Serial.print(sensors.getDeviceCount(), DEC);
if (_DEBUG_) Serial.println(" devices.");
for (int i = 0; i < NUMBOILER; i++) {
sensors.setResolution(TempAddress[i], TEMPERATURE_PRECISION);
}
}
// Temperature Sensor Session
void scanSensors(void) {
// scan temperature sensors every interval TimerTemperature
if((unsigned long)(currentMillis - LastReadTemperature) > TimerTemperature) {
sensors.requestTemperatures();
for (int i = 0; i < NUMBOILER; i++) {
TempValue[i] = (int) sensors.getTempC(TempAddress[i]);
if (_TXSERIAL_) txData.TempValue[i] = TempValue[i];
if (_DEBUG_) Serial.print(LetterBoiler[i]);
if (_DEBUG_) Serial.print(": ");
if (_DEBUG_) Serial.print(TempValue[i]);
if (_DEBUG_) Serial.print("C - ");
}
if (_DEBUG_) Serial.println(" ");
LastReadTemperature = currentMillis;
tempControl();
}
}
// LCD Session
void screenSaver() {
writeModeLCD();
writeSensTempLCD();
lcd.setCursor(0,3);
lcd.print(" ");
lcd.noBacklight();
}
void clearLCDtop(void) {
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
void clearLCDbottom(void) {
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
}
// Relay Session
void closeAllVALVE(void) {
if (_DEBUG_) Serial.println("closeAllVALVE");
for (int i = 0; i < NUMBOILER; i++) {
doVALVE(i, CLOSE_VALVE, false);
writeSensTempLCD();
if (_TXSERIAL_) txData.RelayStatus[i] = RelayStatus[i];
//delay(1000);
}
}
void openAllVALVE(void) {
if (_DEBUG_) Serial.println("openAllVALVE");
for (int i = 0; i < NUMBOILER; i++) {
doVALVE(i, OPEN_VALVE, false);
writeSensTempLCD();
if (_TXSERIAL_) txData.RelayStatus[i] = RelayStatus[i];
//delay(1000);
}
}
void doVALVE(int number, int action, bool safeMode) {
// prevent switching relay too rapidly limited from TimerSwitch
//if ((unsigned long)(currentMillis - LastSwitch) > TimerSwitch)
//{
// action to CLOSE or OPEN VALVE
digitalWrite(RelayPin[number], action);
if (action == OPEN_VALVE)
{
RelayStatus[number] = true;
if (safeMode) RelaySafeMode[number] = true;
}
else
{
RelayStatus[number] = false;
if (safeMode) RelaySafeMode[number] = false;
}
if (_TXSERIAL_) txData.RelayStatus[number] = RelayStatus[number];
// LastSwitch = currentMillis;
//}
}
// Serial Data
void sendSerialData(void) {
// send seral data every interval TimerSerialData
if ((unsigned long)(currentMillis - LastSendData) > TimerSerialData) {
//send the data id _DEBUG_ is disabled
if (!_DEBUG_) {
ET.sendData();
}
LastSendData = currentMillis;
}
}