-
Notifications
You must be signed in to change notification settings - Fork 0
/
pulputin.ino
724 lines (599 loc) · 20.3 KB
/
pulputin.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
// Copyright (C) 2023 Tuomas Airaksinen
// License: GPL. See GPL.txt for more info
// #define USE_LOWPOWER
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <avr/wdt.h>
#ifdef USE_LOWPOWER
#include <LowPower.h>
#endif
static const uint16_t ONE_WIRE_PIN = 30; // Temperature sensor
static const uint16_t OUT_HEATER_PIN = 34;
static const uint16_t BUTTON1_PIN = 39;
static const uint16_t BUTTON2_PIN = 41;
static const uint16_t BUTTON3_PIN = 43;
static const uint16_t BUTTON4_PIN = 45;
static const uint16_t BUTTON5_PIN = 47;
static const uint16_t BUTTON6_PIN = 49;
static const uint16_t BUTTON7_PIN = 51;
static const uint16_t BUTTON8_PIN = 53;
static const uint16_t WATER_LEVEL_PIN = 48;
static const uint16_t IN_MOISTURE1_PIN = A0;
static const uint16_t OUT_PUMP_PIN = 4; // PWM possible
static const uint16_t ALARM_PIN = 3;
static const uint16_t MOTION_PIN = 52;
static const uint16_t MOTION_GROUND_PIN = 50;
uint16_t moisture1Percent = 0;
bool waterLevel = false;
bool maxWaterLevel = false;
bool motionSns = false;
bool alarmRunning = false;
// How many ml water has been pumped each hour
// Latest is first item.
// Once an hour last item is removed and each item is moved one forward.
uint16_t pumpStatistics[24];
uint16_t pumpedTotal = 0;
// How many milliseconds has been spent to heat, each hour.
uint32_t heatStatistics[24];
// EEPROM addresses
static const uint16_t EEPROM_PUMP_STATISTICS = 0; // 2*24 = 48
static const uint16_t EEPROM_CONFIGURED = 48;
static const uint16_t EEPROM_PUMP_TOTAL = 49;
// NOT NEEDED. ADDRESS CAN BE USED FOR OTHER PURPOSE LATER...
// static const uint16_t EEPROM_LAST_TIME_CLOCK_CORRECTED = 51; // 8
static const uint16_t EEPROM_PUMP_STARTED = 59; // 8
static const uint16_t EEPROM_IDLE_STARTED = 67; // 8
static const uint16_t EEPROM_LAST_WET = 75;
static const uint16_t EEPROM_STATS_CUR_DAY = 83; // 1
static const uint16_t EEPROM_DISPLAY_MODE = 84; // 1
static const uint16_t EEPROM_HEATER_STARTED = 85; // 8
static const uint16_t EEPROM_HEAT_STATISTICS = 93; // 4*24 = 96
static const uint16_t EEPROM_LAST = 188;
static const byte EEPROM_CHECKVALUE = 0b10101010;
static const uint32_t EPOCH_OFFSET = 1694490000;
// Times, in millisecond (since starting device)
uint64_t epochAtStart = 0;
uint64_t timeNow = 0;
DateTime dateTimeNow;
uint64_t lastTimeClockCorrected = 0;
uint64_t tempLastRead = 0;
uint64_t modeLastChanged = 0;
uint64_t pumpStartedMs = 0;
uint64_t idleStartedMs = 0;
uint64_t lastWetMs = 0;
uint64_t forceStopStartedMs = 0;
uint64_t motionStopStartedMs = 0;
uint8_t statisticsCurrentDay = 0;
uint8_t displayMode = 0; // DISPLAY_*
bool wasMotionStopped = false;
bool wasForceStopped = false;
bool wasWet = false;
bool pumpRunning = false;
uint64_t heaterStartedMs = 0;
uint64_t heaterIdleStartedMs = 0;
bool heaterRunning = false;
uint16_t minutesAgo(uint64_t timestamp) { return (timeNow - timestamp) / 1000 / 60; }
static const uint16_t PUMP_WATER_SPEED = 106; // Pump speed, ml per 100 seconds
// Convert millilitres to milliseconds and vice versa
uint64_t mlToMs(uint32_t millilitres) { return 100000 * millilitres / PUMP_WATER_SPEED; }
uint32_t msToMl(uint64_t milliseconds) { return milliseconds * PUMP_WATER_SPEED / 100000; }
static const uint32_t ONE_SECOND = 1000;
static const uint32_t ONE_HOUR = 3600000;
static const uint32_t ONE_MINUTE = ONE_HOUR/60;
static const uint32_t FIFTEEN_MINUTES = ONE_MINUTE*15;
static const float TEMP_LIMIT = 5.0;
static const float TEMP_ALARM_LOW = 3.0;
static const uint32_t HEATER_POWER = 50; // Watts
static const uint32_t TARGET_POWER = 5; // Watts
static const uint32_t HEATER_ON_TIME = 5*ONE_SECOND;
static const uint32_t HEATER_IDLE_TIME = HEATER_POWER * (float)HEATER_ON_TIME / TARGET_POWER - HEATER_ON_TIME;
static const uint8_t DISPLAY_SUMMER = 0;
static const uint8_t DISPLAY_WINTER = 1;
static const uint8_t DISPLAY_INTERVAL = 2;
uint8_t modeNow = DISPLAY_SUMMER;
static const uint16_t CONTAINER_SIZE = 28000; // Water container size in (ml)
static const uint16_t PUMP_PORTION = 100; // Amount of water pumped at once (ml)
static const uint32_t PERIOD_TIME = 15*ONE_MINUTE; // Adjusted water amount is PUMP_PORTION / PERIOD_TIME.
static const uint32_t PUMP_TIME = mlToMs(PUMP_PORTION);
static const uint32_t IDLE_TIME = PERIOD_TIME - PUMP_TIME;
static const uint32_t WET_TIME = ONE_HOUR;
static const uint32_t DRY_TOO_LONG_TIME = ONE_HOUR*48;
static const uint32_t FORCE_STOP_TIME = ONE_HOUR;
static const uint32_t MOTION_STOP_TIME = ONE_MINUTE * 15;
float temperature = TEMP_LIMIT + 1; // in celsius
float minTemperature = 100; // in celsius
float maxTemperature = -100; // in celsius
bool tempSensorFail = false;
bool showBootInfo = true;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
DS3231 rtc;
void initializePins() {
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
pinMode(BUTTON4_PIN, INPUT_PULLUP);
pinMode(BUTTON5_PIN, INPUT_PULLUP);
pinMode(BUTTON6_PIN, INPUT_PULLUP);
pinMode(BUTTON7_PIN, INPUT_PULLUP);
pinMode(BUTTON8_PIN, INPUT_PULLUP);
pinMode(MOTION_PIN, INPUT);
pinMode(IN_MOISTURE1_PIN, INPUT);
pinMode(OUT_PUMP_PIN, OUTPUT);
pinMode(OUT_HEATER_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ALARM_PIN, OUTPUT);
pinMode(MOTION_GROUND_PIN, OUTPUT);
digitalWrite(MOTION_GROUND_PIN, LOW);
pinMode(WATER_LEVEL_PIN, INPUT_PULLUP);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(ALARM_PIN, LOW);
digitalWrite(OUT_PUMP_PIN, LOW);
digitalWrite(OUT_HEATER_PIN, LOW);
}
void readEeprom() {
if (eeprom_read_byte(EEPROM_CONFIGURED) != EEPROM_CHECKVALUE) {
resetEEPROM();
}
for (uint16_t i = 0; i < 24; i++) {
pumpStatistics[i] = eeprom_read_word(EEPROM_PUMP_STATISTICS + i*2);
heatStatistics[i] = eeprom_read_dword(EEPROM_HEAT_STATISTICS + i*4);
}
pumpedTotal = eeprom_read_word(EEPROM_PUMP_TOTAL);
eeprom_read_block(&pumpStartedMs, EEPROM_PUMP_STARTED, 8);
eeprom_read_block(&idleStartedMs, EEPROM_IDLE_STARTED, 8);
eeprom_read_block(&heaterStartedMs, EEPROM_HEATER_STARTED, 8);
eeprom_read_block(&lastWetMs, EEPROM_LAST_WET, 8);
statisticsCurrentDay = eeprom_read_byte(EEPROM_STATS_CUR_DAY);
displayMode = eeprom_read_byte(EEPROM_DISPLAY_MODE);
}
void saveEeprom() {
for (uint16_t i = 0; i < 24; i++) {
eeprom_update_word(EEPROM_PUMP_STATISTICS + i*2, pumpStatistics[i]);
eeprom_update_dword(EEPROM_HEAT_STATISTICS + i*4, heatStatistics[i]);
}
eeprom_update_word(EEPROM_PUMP_TOTAL, pumpedTotal);
eeprom_update_block(&pumpStartedMs, EEPROM_PUMP_STARTED, 8);
eeprom_update_block(&idleStartedMs, EEPROM_IDLE_STARTED, 8);
eeprom_update_block(&heaterStartedMs, EEPROM_HEATER_STARTED, 8);
eeprom_update_block(&lastWetMs, EEPROM_LAST_WET, 8);
eeprom_update_byte(EEPROM_STATS_CUR_DAY, statisticsCurrentDay);
eeprom_update_byte(EEPROM_DISPLAY_MODE, displayMode);
}
void dayPassed() {
for (int16_t i = 23; i > 0; i--) {
pumpStatistics[i] = pumpStatistics[i - 1];
heatStatistics[i] = heatStatistics[i - 1];
}
pumpStatistics[0] = 0;
heatStatistics[0] = 0;
}
void resetEEPROM() {
for (int16_t i = 23; i >= 0; i--) {
pumpStatistics[i] = 0;
heatStatistics[i] = 0;
}
pumpedTotal = 0;
pumpStartedMs = timeNow;
idleStartedMs = timeNow;
lastWetMs = 0;
forceStopStartedMs = 0;
statisticsCurrentDay = dateTimeNow.day();
eeprom_update_byte(EEPROM_CONFIGURED, EEPROM_CHECKVALUE);
saveEeprom();
//readEeprom();
}
static const uint16_t BUF_SIZE = 18;
char lcdBuf1[BUF_SIZE];
char lcdBuf2[BUF_SIZE];
char lcdBuf1a[BUF_SIZE];
char lcdBuf2a[BUF_SIZE];
char floatBuf1[BUF_SIZE];
char floatBuf2[BUF_SIZE];
char floatBuf3[BUF_SIZE];
char floatBuf4[BUF_SIZE];
char floatBuf5[BUF_SIZE];
char timeOrTempBuf[BUF_SIZE];
void updateLcdSummer() {
dtostrf((float)(pumpStatistics[0]/1000.0), 4, 1, floatBuf1);
dtostrf((float)(pumpStatistics[1]/1000.0), 4, 1, floatBuf2);
int32_t totalMinutes = minutesAgo(waterLevel ? pumpStartedMs: lastWetMs);
int32_t hours = totalMinutes/60;
int32_t minutesLeft = totalMinutes - hours*60;
uint16_t waterRemainingPercent = ((float)(CONTAINER_SIZE - pumpedTotal - 1) / CONTAINER_SIZE)*100;
snprintf(lcdBuf1, BUF_SIZE, "%s %s %luh %lum ", floatBuf1, floatBuf2, hours, minutesLeft);
snprintf(lcdBuf2, BUF_SIZE, "%2d%% %s%s%s %s ",
waterRemainingPercent,
waterLevel ? "We" : "Dr",
motionSns ? "Mo": " ",
cantStart() ? "St" : " ",
timeOrTempBuf
);
}
void updateLcdWinter() {
int32_t totalMinutes = minutesAgo(heaterStartedMs);
int32_t hours = totalMinutes/60;
int32_t minutesLeft = totalMinutes - hours*60;
dtostrf((float)(heatStatistics[0]/60000.0), 4, 1, floatBuf1); // Show heat on in minutes
dtostrf((float)(heatStatistics[1]/60000.0), 4, 1, floatBuf2);
dtostrf(temperature, 3, 1, floatBuf3);
dtostrf(minTemperature, 3, 1, floatBuf4);
dtostrf(maxTemperature, 3, 1, floatBuf5);
snprintf(lcdBuf1, BUF_SIZE, "%s %s %luh %lum ", floatBuf1, floatBuf2, hours, minutesLeft);
snprintf(lcdBuf2, BUF_SIZE, "%s %s %s %s ",
floatBuf3,
floatBuf4,
floatBuf5,
heaterRunning ? "He" : timeOrTempBuf
);
}
void updateLcd() {
bool showForceStop = !digitalRead(BUTTON4_PIN);
bool showResetContainer = !digitalRead(BUTTON6_PIN);
bool backlightBtn = !digitalRead(BUTTON3_PIN);
bool showTimes = !digitalRead(BUTTON1_PIN);
bool showContainer = !digitalRead(BUTTON5_PIN);
float leftWater = (CONTAINER_SIZE - pumpedTotal)/1000.0;
if (timeNow - modeLastChanged > 5000) {
modeLastChanged = timeNow;
modeNow = (modeNow + 1)%2;
}
if (displayMode == DISPLAY_WINTER || modeNow == 0) {
snprintf(timeOrTempBuf, BUF_SIZE, "%2u%02u ", dateTimeNow.hour(), dateTimeNow.minute());
} else {
dtostrf(temperature, 4, 1, floatBuf3);
snprintf(timeOrTempBuf, BUF_SIZE, "%sC ", floatBuf3);
}
if(showContainer) {
float pumpedTotalLitres = pumpedTotal / 1000.0;
dtostrf(pumpedTotalLitres, 0, 2, floatBuf1);
snprintf(lcdBuf1, BUF_SIZE, "Pumped: %s l ", floatBuf1);
dtostrf(leftWater, 0, 2, floatBuf1);
snprintf(lcdBuf2, BUF_SIZE, "Left: %s l ", floatBuf1);
}
else if (showForceStop) {
snprintf(lcdBuf1, BUF_SIZE, "Force stopping ");
snprintf(lcdBuf2, BUF_SIZE, "for 1 hour ");
}
else if (showResetContainer) {
snprintf(lcdBuf1, BUF_SIZE, "Container ");
snprintf(lcdBuf2, BUF_SIZE, "filled ");
}
else if (showTimes) {
snprintf(lcdBuf1, BUF_SIZE, "Wet %u min ago ", minutesAgo(lastWetMs));
snprintf(lcdBuf2, BUF_SIZE, "Pumped %u min ago ", minutesAgo(pumpStartedMs));
} else {
if(displayMode == DISPLAY_SUMMER) {
updateLcdSummer();
} else if(displayMode == DISPLAY_WINTER) {
updateLcdWinter();
} else if(displayMode == DISPLAY_INTERVAL) {
if(modeNow == DISPLAY_SUMMER) updateLcdSummer();
else updateLcdWinter();
}
}
if (!showBootInfo && strcmp(lcdBuf1a, lcdBuf1) != 0) {
lcd.setCursor(0, 0);
lcd.print(lcdBuf1);
strcpy(lcdBuf1a, lcdBuf1);
}
if (strcmp(lcdBuf2a, lcdBuf2) != 0) {
lcd.setCursor(0, 1);
lcd.print(lcdBuf2);
strcpy(lcdBuf2a, lcdBuf2);
}
}
void printBootInfo() {
snprintf(timeOrTempBuf, BUF_SIZE, "%u.%u %2u:%02u ", dateTimeNow.day(), dateTimeNow.month(), dateTimeNow.hour(), dateTimeNow.minute());
snprintf(lcdBuf1, BUF_SIZE, "BTN1 %s", timeOrTempBuf);
lcd.setCursor(0, 0);
lcd.print(lcdBuf1);
}
uint64_t blinkStoppedMs = 0;
uint64_t blinkStartedMs = 0;
bool blinkNow = false;
void manageBlink() {
if(!blinkNow && (timeNow - blinkStoppedMs > 30000)) {
blinkNow = true;
blinkStartedMs = timeNow;
}
if(blinkNow && (timeNow - blinkStartedMs > 50)) {
blinkNow = false;
blinkStoppedMs = timeNow;
}
}
bool isBeeping() {
bool backlightBtn = !digitalRead(BUTTON3_PIN);
return backlightBtn || (blinkNow && alarmRunning);
}
void updateBeeper() {
analogWrite(ALARM_PIN, isBeeping() ? 50 : 0);
}
void manageBuiltinLedBlink() {
if(blinkNow) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
updateBuiltinLed();
}
}
bool forceStopPressed = false;
bool resetButtonPressed = false;
bool backlightButtonPressed = false;
bool backlightOn = false;
bool resetContainerPressed = false;
bool forceRunPressed = false;
bool modeChangePressed = false;
bool button1Pressed = false;
void readInput() {
bool button1 = !digitalRead(BUTTON1_PIN);
if (button1) {
showBootInfo = false;
}
if (button1 != button1Pressed && button1) {
minTemperature = 100;
maxTemperature = -100;
tempLastRead = 0;
}
button1Pressed = button1;
bool modeChangeBtn = !digitalRead(BUTTON2_PIN);
if (modeChangeBtn != modeChangePressed) {
if(modeChangeBtn) {
displayMode = (displayMode + 1) % 3;
saveEeprom();
Serial.println(displayMode);
}
}
modeChangePressed = modeChangeBtn;
bool forceRunBtn = !digitalRead(BUTTON7_PIN);
if(forceRunBtn != forceRunPressed) {
if(forceRunBtn) {
if(isWinter()) {
digitalWrite(OUT_HEATER_PIN, HIGH);
} else {
digitalWrite(OUT_PUMP_PIN, HIGH);
}
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(OUT_HEATER_PIN, heaterRunning ? HIGH: LOW);
digitalWrite(OUT_PUMP_PIN, pumpRunning ? HIGH: LOW);
updateBuiltinLed();
}
}
forceRunPressed = forceRunBtn;
bool containerBtn = !digitalRead(BUTTON6_PIN);
if(containerBtn != resetContainerPressed && containerBtn) {
pumpedTotal = 0;
saveEeprom();
}
resetContainerPressed = containerBtn;
bool resetBtn = !digitalRead(BUTTON8_PIN);
if (resetBtn != resetButtonPressed && resetBtn) {
resetEEPROM();
readEeprom();
}
resetButtonPressed = resetBtn;
bool backlightBtn = !digitalRead(BUTTON3_PIN);
if (backlightBtn != backlightButtonPressed && backlightBtn) {
backlightOn = !backlightOn;
if(backlightOn) {
lcd.backlight();
} else {
lcd.noBacklight();
}
}
backlightButtonPressed = backlightBtn;
bool forceBtn = !digitalRead(BUTTON4_PIN);
if (forceBtn != forceStopPressed && forceBtn) {
forceStopStartedMs = timeNow;
wasForceStopped = true;
}
forceStopPressed = forceBtn;
motionSns = digitalRead(MOTION_PIN);
if(motionSns) {
motionStopStartedMs = timeNow;
wasMotionStopped = true;
}
moisture1Percent = 100 - (uint16_t)((float)analogRead(IN_MOISTURE1_PIN)/ 1023. * 100);
waterLevel = digitalRead(WATER_LEVEL_PIN);
}
void startHeat() {
Serial.println("startHeat");
heaterRunning = true;
heaterStartedMs = timeNow;
digitalWrite(OUT_HEATER_PIN, HIGH);
updateBuiltinLed();
}
void stopHeat() {
Serial.println("stopHeat");
heaterRunning = false;
digitalWrite(OUT_HEATER_PIN, LOW);
heaterIdleStartedMs = timeNow;
uint32_t heaterTime = timeNow - heaterStartedMs;
heatStatistics[0] += heaterTime;
updateBuiltinLed();
saveEeprom();
}
void updateBuiltinLed() {
digitalWrite(LED_BUILTIN, (heaterRunning || pumpRunning) ? HIGH: LOW);
}
void startPump() {
pumpRunning = true;
pumpStartedMs = timeNow;
digitalWrite(OUT_PUMP_PIN, HIGH);
updateBuiltinLed();
resetMaxWaterLevel();
saveEeprom();
}
void stopPump() {
pumpRunning = false;
digitalWrite(OUT_PUMP_PIN, LOW);
updateBuiltinLed();
uint32_t pumped = msToMl(timeNow - pumpStartedMs);
pumpStatistics[0] += pumped;
pumpedTotal += pumped;
idleStartedMs = timeNow;
saveEeprom();
}
void updateMaxWaterLevel() {
if (waterLevel) {
maxWaterLevel = true;
}
}
void resetMaxWaterLevel() {
maxWaterLevel = waterLevel;
}
bool stopPumpTimePassed() { return timeNow - pumpStartedMs > PUMP_TIME;}
bool idleTimePassed() { return timeNow - idleStartedMs > IDLE_TIME; }
bool wetRecently() { return wasWet && (timeNow - lastWetMs < WET_TIME); }
bool dryTooLong() { return timeNow - lastWetMs > DRY_TOO_LONG_TIME; }
bool forceStoppedRecently() { return wasForceStopped && (timeNow - forceStopStartedMs < FORCE_STOP_TIME); }
bool motionStoppedRecently() { return wasMotionStopped && (timeNow - motionStopStartedMs < MOTION_STOP_TIME); }
bool stopHeaterTimePassed() { return timeNow - heaterStartedMs > HEATER_ON_TIME; }
bool heaterIdleTimePassed() { return timeNow - heaterIdleStartedMs > HEATER_IDLE_TIME; }
bool isTriggerTemp() { return temperature < TEMP_LIMIT; }
bool isAlarmTemp() { return temperature < TEMP_ALARM_LOW; }
bool isOperating() { return pumpRunning || heaterRunning; }
bool isWinter() { return true; }
bool cantStart() { return isWinter() || isTriggerTemp() || wetRecently() || forceStoppedRecently() || motionStoppedRecently(); }
void manageWaterPump() {
updateMaxWaterLevel();
if (maxWaterLevel) {
lastWetMs = timeNow;
wasWet = true;
}
if (pumpRunning) {
if (stopPumpTimePassed() || cantStart()) {
stopPump();
}
} else if (idleTimePassed()) {
if (!maxWaterLevel && !cantStart()) {
startPump();
} else {
resetMaxWaterLevel();
idleStartedMs = timeNow;
}
}
}
void manageHeater() {
if (heaterRunning) {
if(stopHeaterTimePassed()) stopHeat();
} else if (heaterIdleTimePassed() && isTriggerTemp()) {
startHeat();
}
}
void manageAlarm() {
float leftWater = (CONTAINER_SIZE - pumpedTotal)/1000.0;
alarmRunning = (!isWinter() && dryTooLong()) || showBootInfo || tempSensorFail || isAlarmTemp() || (!isWinter() && (leftWater < 7.5 && !forceStoppedRecently()));
}
void alarmReason() {
float leftWater = (CONTAINER_SIZE - pumpedTotal)/1000.0;
Serial.println(leftWater);
Serial.println(dryTooLong());
Serial.println(isWinter());
Serial.println(tempSensorFail);
}
void printStats() {
for(uint16_t i = 0; i<24; i++) {
Serial.println(pumpStatistics[i]);
}
}
uint32_t counter = 0;
void setup() {
wdt_enable(WDTO_2S);
Serial.begin(9600);
Wire.begin();
rtc.begin();
sensors.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(__DATE__, __TIME__));
}
//Serial.println(__TIME__);
//rtc.adjust(DateTime(__DATE__, __TIME__));
dateTimeNow = rtc.now();
dateTimeNow.tostr(lcdBuf1);
Serial.println(lcdBuf1);
Serial.println(dateTimeNow.hour());
Serial.println(dateTimeNow.minute());
epochAtStart = (uint64_t)(dateTimeNow.unixtime() - EPOCH_OFFSET) * 1000;
initializePins();
readEeprom();
printStats();
lcd.init();
Serial.println("Heat params in seconds");
Serial.println(HEATER_ON_TIME);
Serial.println(HEATER_IDLE_TIME);
printBootInfo();
}
unsigned long millisAdd = 0;
unsigned long myMillis() {
return millis() + millisAdd;
}
void setMinMaxTemp() {
if (minTemperature > temperature) {
minTemperature = temperature;
}
if (maxTemperature < temperature) {
maxTemperature = temperature;
}
}
void loop() {
wdt_reset();
timeNow = epochAtStart + myMillis();
dateTimeNow.setunixtime((timeNow / 1000) + EPOCH_OFFSET);
if(dateTimeNow.day() != statisticsCurrentDay) {
dayPassed();
statisticsCurrentDay = dateTimeNow.day();
saveEeprom();
}
// We do not want to fix clock (because it might jump backwards) during operations.
// It would mess up time based volume etc. calculations
if (timeNow - lastTimeClockCorrected > FIFTEEN_MINUTES && !isOperating()) {
int32_t correction = rtc.now().unixtime() - dateTimeNow.unixtime();
epochAtStart += correction * 1000;
timeNow = epochAtStart + myMillis();
lastTimeClockCorrected = timeNow;
saveEeprom();
}
if (timeNow - tempLastRead > 10000) {
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
if (temp != DEVICE_DISCONNECTED_C) {
temperature = temp;
setMinMaxTemp();
tempSensorFail = false;
} else {
temperature = TEMP_LIMIT + 1;
tempSensorFail = true;
}
tempLastRead = timeNow;
}
readInput();
manageWaterPump();
manageHeater();
manageAlarm();
updateLcd();
manageBlink();
updateBeeper();
manageBuiltinLedBlink();
counter++;
if(false && counter % 100 == 0) {
Serial.println("speed");
Serial.println(counter);
Serial.println(myMillis());
Serial.println(1000*counter/myMillis());
Serial.flush();
}
#ifdef USE_LOWPOWER
if(!isBeeping()) {
LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_ON);
millisAdd += 120;
// LowPower disables, so let's re-enable.
wdt_enable(WDTO_2S);
}
#endif
}