-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTEC_Controller.ino
More file actions
616 lines (564 loc) · 21.9 KB
/
TEC_Controller.ino
File metadata and controls
616 lines (564 loc) · 21.9 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
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
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
String padString(String s, int strLen = 16, bool back = true, String fillChar = " ");
const uint8_t smallAirFanSwitchPin = 9;
const uint8_t largeAirFanSwitchPin = 10;
const uint8_t airTECSwitchPin = 11;
const uint8_t lightSwitchPin = 8;
//const uint8_t waterTECSwitchPin = 7;
const uint8_t uvSwitchPin = 4;
const uint8_t tempSenseDataPin = 2;
const int buttonPin = 3;
uint8_t waterTempHigh = 68;
uint8_t waterTempTarget = 55;
uint8_t airTempTarget = 69;
uint8_t airTempHigh = 71;
uint8_t airTemp = 0;
uint8_t waterTemp = 0;
bool airCoolingActive = false;
bool waterCoolingActive = false;
bool airTECActive = false;
bool airFansActive = false;
bool waterLEDHigh = false;
bool waterTECActive = false;
bool uvActive = false;
bool lightActive = false;
bool isWaterChangeDay = false;
bool buttonActive = false;
int buttonState = 0;
unsigned long buttonPressTimer = 0;
unsigned long loopTimer = 0;
unsigned long updateDisplayTimer = 0;
unsigned long runScheduleTimer = 0;
unsigned long timeoutTimer = 0;
unsigned long blinkTimer = 0;
uint8_t menuPosition = 0;
uint8_t blinkState = 0;
OneWire tempSenseDataWire{tempSenseDataPin};
DallasTemperature sensors{&tempSenseDataWire};
RTC_DS3231 clock;
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
enum DisplayModes {timeDisplay, tempDisplay, scheduleDisplay};
enum ButtonPressType {noPress, shortPress, longPress, programPress};
ButtonPressType buttonPress = noPress;
DisplayModes displayMode = timeDisplay;
DisplayModes lastDisplay = scheduleDisplay;
bool onMainMenu = true;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
uint8_t airProbe = 0;
uint8_t waterProbe = 1;
enum ScheduleIdxNames {On1Hour, On1Min, Off1Hour, Off1Min, On2Hour, On2Min, Off2Hour, Off2Min, UVOnHour, UVOnMin, UVOffHour, UVOffMin, DayAirHi, DayAirTg, NiteAirHi, NiteAirTg, DayH2OHi, DayH2OTg, NiteH2OHi, NiteH2OTg};
int schedule[13][20] = {{4, 0, 20, 0, 2, 30, 2, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 1, Veg 1, Seed/Clone, 16-8
{4, 0, 16, 0, 21, 30, 22, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 2, Veg 2, Early Grow, 12-5.5-1-5.5
{4, 0, 16, 0, 21, 30, 22, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 3, Veg 3, Early Grow, 12-5.5-1-5.5
{4, 0, 16, 0, 21, 30, 22, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 4, Veg 4, Late Grow, 12-5.5-1-5.5
{4, 0, 16, 0, 21, 30, 21, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 5, Bloom 1, Early Bloom, 12-12
{4, 0, 16, 0, 21, 30, 21, 30, 7, 0, 7, 0, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 6, Bloom 2, Early Bloom, 12-12
{4, 0, 15, 0, 21, 30, 21, 30, 7, 0, 7, 10, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 7, Bloom 3, Mid Bloom, 11-13
{4, 0, 15, 0, 21, 30, 21, 30, 7, 0, 7, 20, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 8, Bloom 4, Mid Bloom, 11-13
{4, 0, 15, 0, 21, 30, 21, 30, 7, 0, 7, 40, 68, 55, 66, 55, 68, 66, 70, 68}, //Week 9, Bloom 5, Mid Bloom, 11-13
//{9, 0, 20, 0, 2, 30, 2, 30, 7, 0, 7, 40, 68, 55, 66, 55, 68, 66, 70, 68}, //Extra week, Extra Bloom, Mid Bloom, 11-13
{4, 0, 14, 0, 21, 30, 21, 30, 7, 0, 8, 0, 68, 55, 66, 55, 70, 68, 68, 66}, //Week 10, Bloom 6, Late Bloom, 10-14, Low Nite temps when UV is high
{4, 0, 14, 0, 21, 30, 21, 30, 7, 0, 8, 0, 68, 55, 66, 55, 70, 68, 68, 66}, //Week 11, Bloom 7, Late Bloom, 10-14, Low Nite temps when UV is high
{4, 0, 13, 0, 21, 30, 21, 30, 7, 0, 7, 10, 68, 55, 66, 55, 70, 68, 68, 66}, //Week 12, Bloom 8, Ripen, 9-15, Low Nite temps @ late growth for colors
{4, 0, 12, 0, 21, 30, 21, 30, 7, 0, 7, 5, 68, 55, 66, 55, 68, 66, 68, 66}}; //Week 13, Bloom 9, Flush, 8-16
DateTime startDay = DateTime(2021, 4, 25, schedule[0][On1Hour], schedule[0][On1Min], 0);
DateTime endDay = startDay + TimeSpan(7*13, 0, 0, 0);
DateTime scheduleRunTime = startDay - TimeSpan(100, 0, 0, 0);
uint8_t scheduleWeek = 0;
void setup() {
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
sensors.begin(); //Init DallasTemperature library
pinMode(uvSwitchPin, OUTPUT);
//pinMode(waterTECSwitchPin, OUTPUT);
pinMode(lightSwitchPin, OUTPUT);
pinMode(buttonPin, INPUT);
if (Serial)
Serial.begin(9600);
if (!clock.begin())
{
Serial.println("Couldn't find RTC");
while(1)
delay(100000); //Can't continue without RTC
}
//clock.adjust(DateTime(2021, 5, 31, 13, 37, 5));
readStartDay();
runSchedule(clock.now());
}
void loop() {
//Calculate time passed on timers-----------------------
DateTime now = clock.now();
unsigned long nowMillis = millis();
unsigned long updateDisplayTimeElapsed = abs(nowMillis-updateDisplayTimer);
unsigned long loopTimeElapsed = abs(nowMillis-loopTimer);
unsigned long runScheduleTimeElapsed = abs(nowMillis-loopTimer);
unsigned long buttonPressTimeElapsed = abs(nowMillis-buttonPressTimer);
unsigned long timeoutTimeElapsed = abs(nowMillis-timeoutTimer);
unsigned long blinkTimeElapsed = abs(nowMillis-blinkTimer);
bool menuSwitch = false;
//Look for button presses-----------------------------
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH and not buttonActive)
{
buttonPressTimer = nowMillis;
buttonActive = true;
}
else if (buttonState == LOW and buttonActive)
{
buttonActive = false;
buttonPress = (buttonPressTimeElapsed >= 600 ? longPress : shortPress);
}
//Handle button presses-------------------------------
if (buttonPress == shortPress)
{
if (onMainMenu)
{
lcd.clear();
displayMode = (displayMode < lastDisplay ? DisplayModes(int(displayMode)+1) : timeDisplay);
}
}
else if (buttonPress == longPress)
{
if (onMainMenu)
{
onMainMenu = false;
buttonPress = programPress;
menuSwitch = true;
}
}
//Timeout display and go back to time display-----------------
if (onMainMenu and displayMode != timeDisplay and not buttonActive and (buttonPress == noPress or buttonPress == programPress) and timeoutTimeElapsed >= 60000)
displayMode = timeDisplay;
else if (displayMode != timeDisplay and (buttonActive or (buttonPress != noPress and buttonPress != programPress)))
timeoutTimer = nowMillis;
//Update displays--------------------------------------
if (displayMode == timeDisplay and (updateDisplayTimeElapsed >= 250 or buttonPress != noPress))
{
updateDisplayTimer = nowMillis;
lcd.setCursor(0,0);
String hourStr = String(now.hour()), minuteStr = String(now.minute()), secondStr = String(now.second());
if (hourStr.length() == 1) {hourStr = "0" + hourStr;}
if (minuteStr.length() == 1) {minuteStr = "0" + minuteStr;}
if (secondStr.length() == 1) {secondStr = "0" + secondStr;}
String timeStr = "Time: " + hourStr + ":" + minuteStr + ":" + secondStr;
lcd.print(padString(timeStr));
lcd.setCursor(0,1);
if (isWaterChangeDay and not uvActive and lightActive)
lcd.print(padString("Change h2o now!"));
else if (lightActive and not uvActive)
lcd.print(padString("Light:On, UV:Off"));
else if (not lightActive and not uvActive)
lcd.print(padString("PLANTS SLEEPING!"));
else if (lightActive and uvActive)
lcd.print(padString("UV ON, DONT LOOK"));
else
lcd.print(padString("ERR: UV AT NIGHT"));
}
else if (displayMode == tempDisplay and (updateDisplayTimeElapsed >= 5000 or buttonPress != noPress))
{
updateDisplayTimer = nowMillis;
sensors.requestTemperatures();
airTemp = sensors.getTempFByIndex(airProbe);
waterTemp = sensors.getTempFByIndex(waterProbe);
String Tw = String(waterTemp), Ta = String(airTemp);
String Twr = "|"+String(waterTempTarget)+"-"+String(waterTempHigh);
String Tar = "|"+String(airTempTarget)+"-"+String(airTempHigh);
String wc = (waterCoolingActive ? "On" : "Off");
String ac = (airCoolingActive ? "On" : "Off");
if (onMainMenu)
{
lcd.setCursor(0,0);
lcd.print(padString("W:" + Tw + Twr + " C:" + wc));
lcd.setCursor(0,1);
lcd.print(padString("A:" + Ta + Tar + " C:" + ac));
}
else
{
lcd.setCursor(0,0);
lcd.print(padString("W:"+Tw+" A:"+Ta));
lcd.setCursor(0,1);
if (menuPosition == 0)
lcd.print("*");
else
lcd.print(" ");
lcd.print("SWAP ");
if (menuPosition == 1)
lcd.print("*");
else
lcd.print(" ");
lcd.print("EXIT");
if (buttonPress == shortPress)
{
if (menuPosition == 1)
menuPosition = 0;
else
menuPosition++;
buttonPress = programPress;
}
else if (buttonPress == longPress)
{
switch (menuPosition)
{
case 0: {int temp = airProbe; airProbe = waterProbe; waterProbe = temp; onMainMenu = true; menuPosition = 0; break;}
case 1: {onMainMenu = true; menuPosition = 0; break;}
}
buttonPress = programPress;
}
}
}
else if (displayMode == scheduleDisplay and ((not onMainMenu and blinkTimeElapsed >= 375) or updateDisplayTimeElapsed >= 60000 or buttonPress != noPress))
{
if (buttonPress != noPress)
{
updateDisplayTimer = nowMillis;
uint32_t nowUnix = now.unixtime();
uint32_t startUnix = startDay.unixtime();
double secDiff = (nowUnix > startUnix ? nowUnix - startUnix : 0);
uint32_t week = ((secDiff / 604800L) < 13 ? secDiff / 604800L : 12);
scheduleWeek = week;
}
lcd.setCursor(0,0);
String line1 = "Wk";
line1 += String(scheduleWeek+1);
line1 += " ";
line1 += "St:";
if (not onMainMenu)
{
line1 += (blinkState == 0 and menuPosition == 0 ? " " : String(startDay.year()));
line1 += (blinkState == 0 and menuPosition == 1 ? " " : padString(String(startDay.month()), 2, false, "0"));
line1 += (blinkState == 0 and menuPosition == 2 ? " " : padString(String(startDay.day()), 2, false, "0"));
blinkState = (blinkState == 0 ? 1 : 0);
blinkTimer = nowMillis;
if (menuSwitch)
startDay = DateTime(2021, 1, 1, startDay.hour(), startDay.minute(), startDay.second());
if (buttonPress == shortPress)
{
if (menuPosition == 0)
startDay = DateTime(startDay.year() < 2030 ? startDay.year()+1 : 2021, startDay.month(), startDay.day(), startDay.hour(), startDay.minute(), startDay.second());
else if (menuPosition == 1)
startDay = DateTime(startDay.year(), startDay.month() <= 12 ? startDay.month()+1 : 1, startDay.day(), startDay.hour(), startDay.minute(), startDay.second());
else if (menuPosition == 2)
{
int sdmonth = startDay.month();
startDay = startDay + TimeSpan(1,0,0,0);
if (startDay.month() != sdmonth)
startDay = DateTime(startDay.year(), sdmonth, 1, startDay.hour(), startDay.minute(), startDay.second());
}
}
else if (buttonPress == longPress)
{
if (menuPosition < 2)
menuPosition++;
else
{
onMainMenu = true;
menuPosition = 0;
writeStartDay();
}
}
if (buttonPress != programPress and buttonPress != noPress)
{
buttonPress = programPress;
blinkState = 1;
}
}
else
{
line1 += String(startDay.year());
line1 += padString(String(startDay.month()), 2, false, "0");
line1 += padString(String(startDay.day()), 2, false, "0");
}
lcd.setCursor(0,0);
lcd.print(padString(line1));
if (buttonPress != noPress)
{
float onHr1 = schedule[scheduleWeek][On1Hour] + (schedule[scheduleWeek][On1Min]/60.0);
float offHr1 = schedule[scheduleWeek][Off1Hour] + (schedule[scheduleWeek][Off1Min]/60.0);
float onHr2 = schedule[scheduleWeek][On2Hour] + (schedule[scheduleWeek][Off2Min]/60.0);
float offHr2 = schedule[scheduleWeek][Off2Hour] + (schedule[scheduleWeek][Off2Min]/60.0);
uint8_t uvOn = (schedule[scheduleWeek][UVOnHour]*60) + schedule[scheduleWeek][UVOnMin];
uint8_t uvOff = (schedule[scheduleWeek][UVOffHour]*60) + schedule[scheduleWeek][UVOffMin];
uint8_t uvMin = uvOff-uvOn;
float lightCycle[4] = {offHr1-onHr1, onHr2-offHr1, offHr2-onHr2, (24-offHr2)+onHr1};
if (lightCycle[2] == 0) lightCycle[1] += lightCycle[3];
lcd.setCursor(0,1);
String line2 = String(int(lightCycle[0]))+"-";
line2 += (float(int(lightCycle[1])) == lightCycle[1] ? String(int(lightCycle[1])) : String(lightCycle[1]).substring(0,3));
if (lightCycle[2] != 0) line2 += "-";
if (lightCycle[2] != 0) line2 += (float(int(lightCycle[2])) == lightCycle[2] ? String(int(lightCycle[2])) : String(lightCycle[2]).substring(0,3))+"-";
if (lightCycle[2] != 0) line2 += (float(int(lightCycle[3])) == lightCycle[3] ? String(int(lightCycle[3])) : String(lightCycle[3]).substring(0,3));
line2 += " " + String(uvMin) + "m";
lcd.print(padString(line2));
}
}
//Execute the rest only every 30 seconds------------------------
if (loopTimeElapsed < 30000 or buttonActive or buttonPress != noPress)
{
buttonPress = noPress;
return;
}
else
{
loopTimer = nowMillis;
lcd.setCursor(0,1);
lcd.print(padString("Exec Loop"));
buttonPress = programPress;
}
//Run schedule every 5 minutes (300,000 seconds)----------------
if (runScheduleTimeElapsed >= 300000)
{
runScheduleTimer = nowMillis;
runSchedule(now);
}
sensors.requestTemperatures();
airTemp = sensors.getTempFByIndex(airProbe);
waterTemp = sensors.getTempFByIndex(waterProbe);
/*Blink builtin LED if water temp is above 68 deg F. Turn on water TEC and fan.
if (!waterCoolingActive and waterTemp >= waterTempHigh)
{
switchWaterTEC(true);
waterCoolingActive = true;
}
else if (waterCoolingActive and waterTemp <= waterTempTarget)
{
digitalWrite(LED_BUILTIN, LOW);
switchWaterTEC(false);
waterCoolingActive = false;
}
*/
//Check air temp and activate cooling, if necessary
if (airCoolingActive and airTemp <= airTempTarget)
{
switchAirTEC(false);
switchAirFans(false);
airCoolingActive = false;
}
else if (!airCoolingActive and airTemp >= airTempHigh)
{
switchAirFans(true);
switchAirTEC(true);
airCoolingActive = true;
}
//Serial output, if available
if (Serial)
printTemps(airTemp, waterTemp, now);
lcd.clear();
}
void writeStartDay()
{
short sdNums[6] = {startDay.year(),startDay.month(),startDay.day(),startDay.hour(),startDay.minute(),startDay.second()};
for (int i = 0; i < 6; ++i)
EEPROM.put(i*sizeof(short), sdNums[i]);
}
void readStartDay()
{
short sdNums[6];
for (short i = 0; i < 6; ++i)
EEPROM.get(i*sizeof(short), sdNums[i]);
startDay = DateTime(sdNums[0],sdNums[1],sdNums[2],sdNums[3],sdNums[4],sdNums[5]);
}
String padString(String s, int strLen, bool back, String fillChar)
{
String newStr = String(s);
int len = s.length();
if (len < strLen)
for (int x = strLen-len; x > 0; x--)
newStr = (back ? newStr+fillChar : fillChar+newStr);
return newStr;
}
//Turn tent air cooling fans on/off
void switchAirFans(bool on)
{
if (!airFansActive and on)
{
analogWrite(smallAirFanSwitchPin, 255);
analogWrite(largeAirFanSwitchPin, 255);
airFansActive = true;
}
else if (airFansActive and !on)
{
analogWrite(smallAirFanSwitchPin, 0);
analogWrite(largeAirFanSwitchPin, 0);
airFansActive = false;
}
}
//Turn tent air cooling TEC on/off
void switchAirTEC(bool on)
{
if (!airTECActive and on)
{
analogWrite(airTECSwitchPin, 255);
airTECActive = true;
}
else if (airTECActive and !on)
{
analogWrite(airTECSwitchPin, 0);
airTECActive = false;
}
}
/*Turn hydroponic nutrient water reservoir cooling TEC on/off.
void switchWaterTEC(bool on)
{
if (!waterTECActive and on)
{
digitalWrite(waterTECSwitchPin, HIGH);
waterTECActive = true;
}
else if (waterTECActive and !on)
{
digitalWrite(waterTECSwitchPin, LOW);
waterTECActive = false;
}
}
*/
//Print temperature data to serial output
void printTemps(int airTemp, int waterTemp, DateTime now)
{
uint32_t nowUnix = now.unixtime();
uint32_t startUnix = startDay.unixtime();
double secDiff = (nowUnix > startUnix ? nowUnix - startUnix : 0);
uint32_t week = ((secDiff / 604800L) < 13 ? secDiff / 604800L : 12);
scheduleWeek = week;
Serial.println("");
Serial.println("--------------------------");
Serial.print("Light: ");
Serial.println(lightActive);
Serial.print("UV: ");
Serial.println(uvActive);
Serial.print("Air Temp: ");
Serial.print(airTemp);
Serial.println(" degrees F");
Serial.print("Water Temp: ");
Serial.print(waterTemp);
Serial.println(" degrees F");
Serial.print("Air cooling on: ");
Serial.println(airCoolingActive);
Serial.print("Water cooling on: ");
Serial.println(waterCoolingActive);
Serial.print("Week ");
Serial.println(week+1);
Serial.print("High air temp: ");
Serial.println(airTempHigh);
Serial.print("Target air temp: ");
Serial.println(airTempTarget);
//Serial.print("High water temp: ");
//Serial.println(waterTempHigh);
//Serial.print("Target water temp: ");
//Serial.println(waterTempTarget);
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
/*
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
*/
}
//Check time/date, set lighting schedule and temperature based on schedule
void runSchedule(DateTime now)
{
for (int i = 0; i < 13; i++)
{
DateTime schedDay = startDay + TimeSpan(7*i,0,0,0);
if (now.year() == schedDay.year() and now.month() == schedDay.month() and now.day() == schedDay.day() and now.hour() >= 7)
isWaterChangeDay = true;
}
lcd.setCursor(0,1);
lcd.print(padString("Exec Schedule"));
uint32_t startUnix = startDay.unixtime();
uint32_t endUnix = endDay.unixtime();
uint32_t nowUnix = now.unixtime();
double secDiff = (nowUnix > startUnix ? nowUnix - startUnix : 0);
uint32_t week = (secDiff / 604800L < 13 ? secDiff / 604800L : 12);
scheduleWeek = week;
//Light hours from schedule
DateTime tomorrow(now + TimeSpan(1, 0, 0, 0));
uint32_t on1 = DateTime(now.year(), now.month(), now.day(), schedule[week][On1Hour], schedule[week][On1Min], 0).unixtime();
uint32_t off1 = DateTime(now.year(), now.month(), now.day(), schedule[week][Off1Hour], schedule[week][Off1Min], 0).unixtime();
uint32_t on2 = DateTime(now.year(), now.month(), now.day(), schedule[week][On2Hour], schedule[week][On2Min], 0).unixtime();
uint32_t off2 = DateTime(now.year(), now.month(), now.day(), schedule[week][Off2Hour], schedule[week][Off2Min], 0).unixtime();
uint32_t on3 = DateTime(tomorrow.year(), tomorrow.month(), tomorrow.day(), schedule[week][On1Hour], schedule[week][On1Min], 0).unixtime();
uint32_t uvOn = DateTime(now.year(), now.month(), now.day(), schedule[week][UVOnHour], schedule[week][UVOnMin], 0).unixtime();
uint32_t uvOff = DateTime(now.year(), now.month(), now.day(), schedule[week][UVOffHour], schedule[week][UVOffMin], 0).unixtime();
//Temps from schedule
if (nowUnix >= off1 and nowUnix < on3)
{
//-------Night-----------
airTempHigh = schedule[week][NiteAirHi];
airTempTarget = schedule[week][NiteAirTg];
waterTempHigh = schedule[week][NiteH2OHi];
waterTempTarget = schedule[week][NiteH2OTg];
if (lightActive and (nowUnix < on2 or nowUnix >= off2))
switchLight(false);
else if (!lightActive and nowUnix >= on2 and nowUnix < off2)
switchLight(true);
}
else if (nowUnix >= on1 and nowUnix < off1)
{
//-------Day-------------
if (nowUnix >= on1 and nowUnix < off1)
{
airTempHigh = schedule[week][DayAirHi];
airTempTarget = schedule[week][DayAirTg];
waterTempHigh = schedule[week][DayH2OHi];
waterTempTarget = schedule[week][DayH2OTg];
if (!lightActive)
switchLight(true);
}
//--------UV-----------(only happens during day)
if (nowUnix >= uvOn and nowUnix < uvOff)
switchUV(true);
else if (nowUnix < uvOn or nowUnix >= uvOff)
switchUV(false);
}
}
void switchUV(bool on)
{
if (!uvActive and on)
{
digitalWrite(uvSwitchPin, HIGH);
uvActive = true;
}
else if (uvActive and !on)
{
digitalWrite(uvSwitchPin, LOW);
uvActive = false;
}
}
void switchLight(bool on)
{
if (!lightActive and on)
{
digitalWrite(lightSwitchPin, HIGH);
lightActive = true;
}
else if (lightActive and !on)
{
digitalWrite(lightSwitchPin, LOW);
lightActive = false;
}
}