-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrf_thermostat.ino
894 lines (776 loc) · 22.5 KB
/
rf_thermostat.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
// PI(D) logic hugely inspired by: https://github.com/GeorgeDewar/pid-thermostat/blob/master/thermostat/thermostat.ino
#include <Wire.h>
#include <PID_v1.h>
#include "tx433_Nexa.h"
#include <Time.h>
#include <EEPROM.h>
#include <Button.h> // https://github.com/JChristensen/Button
#include <LiquidCrystal_I2C.h>
#include <RtcDS3231.h> // https://github.com/Makuna/Rtc
#define ROT_CW_PIN 2
#define ROT_CCW_PIN 3
#define ROT_SW_PIN 4
#define TX_PIN 5
// RTC setup
RtcDS3231<TwoWire> Rtc(Wire);
// Create a menu button instance
Button menuButton(ROT_SW_PIN, true, true, 25);
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
double Kp = 45;
double Ki = 0.05;
double Kd = 0;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
unsigned long windowSize = 600000; // 10 Minutes
unsigned long windowStartTime;
float tempC;
unsigned long lastTempRead = 0;
unsigned long lastAlarmCheck = 0;
bool heaterState = false;
// Updated by the ISR (Interrupt Service Routine)
volatile uint8_t virtualInt;
volatile float virtualFloat;
// Alarms for when we want heating
uint8_t alarms[4][2];
/*uint8_t alarms[4][2] = {
{23, 30}, {5, 30}, //night
{8, 30}, {15, 0} //work
};*/
// Run modes
enum modes {
normal,
daytime,
night,
allwayson,
allwaysoff,
tweakmode
};
modes mode = normal;
// Menu categories in tweak mode
enum menuroots {
SpNml, // Setpoint Normal
AlwOn, // Always On, keep temperature at setpoint
AlwOf, // Always Off, keep heater off at all times
AlNOn, // Alarm Night On, when night time begins and heater is off
AlNOf, // Alarm Night Off, when night time ends and heating begins
AlDOn, // Alarm Day On, When daytime heating begins (lower since you're not there)
AlDOf, // Alarm Day Off, When to begin heating for your return
SetTm // Set Time if needed.
};
menuroots menuroot = SpNml;
// Variables used in menu interrupt function
volatile uint8_t menurootPos = 0;
volatile bool rootLevel = true;
volatile uint8_t h = 0;
volatile uint8_t m = 0;
volatile uint8_t s = 0;
// Cursor placement lookup
uint8_t setpointPos = 0;
uint8_t spMenuOrder[5] = {0, 1, NULL, NULL, NULL};
uint8_t alarmDayPos = 0;
uint8_t alarmDayMenuOrder[5] = {0, 2, 3, 1, NULL};
uint8_t alarmNightPos = 0;
uint8_t alarmNightMenuOrder[5] = {0, 2, 3, NULL, NULL};
uint8_t setTimePos = 0;
uint8_t setTimeMenuOrder[5] = {0, 2, 3, NULL, NULL};
// LCD Cursor coordinates referred to with the lookup above
const uint8_t coordinates[4][2] = {
{11, 1}, // M:<
{3, 1}, // Sp:<
{0, 0}, // >HH
{3, 0} // >MM
};
// Settings struct for storing in EEPROM
struct Settings {
uint8_t magic;
float setpointHigh;
float setpointLow;
uint8_t alarmNightIn[2];
uint8_t alarmNightOut[2];
uint8_t alarmDayIn[2];
uint8_t alarmDayOut[2];
};
bool settingsLoaded = false;
Settings settings;
// Nexa ID
String tx_nexa = "1010101010101010101010101010101010101010101010101010";
String ch_nexa = "1010";
// RF transmit Pin 3
Tx433_Nexa Nexa(TX_PIN, tx_nexa, ch_nexa);
void setup() {
pinMode(ROT_CW_PIN, INPUT);
pinMode(ROT_CCW_PIN, INPUT);
pinMode(ROT_SW_PIN, INPUT_PULLUP);
pinMode(TX_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(ROT_CW_PIN), rotationInterrupt, LOW);
Serial.begin(115200);
// initialize the LCD
lcd.init();
lcd.noCursor();
lcd.noBlink();
lcd.backlight();
// RTC setup
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid()) {
// Common Cuases:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
// Make sure relay is OFF
Nexa.Device_Off(0);
//tell the PID to range between 0 and 100 (%)
myPID.SetOutputLimits(0, 100);
myPID.SetSampleTime(1000);
//turn the PID off
myPID.SetMode(MANUAL);
}
void loop() {
if (!settingsLoaded) {
loadSettings();
if (!settingsLoaded) {
setDefaultSettings();
saveSettings();
}
}
if (settingsLoaded && myPID.GetMode() == MANUAL){
windowStartTime = millis();
myPID.SetMode(AUTOMATIC);
}
checkButton();
readTemp();
myPID.Compute();
checkAlarms();
if (mode == daytime){
Setpoint = settings.setpointLow;
} else {
Setpoint = settings.setpointHigh;
}
adjustHeater();
refreshLCD();
}
// Interrupt function to handle menu changes and setting of values
void rotationInterrupt () {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
// If interrupts come faster than 5ms, assume it's a bounce and ignore
if (interruptTime - lastInterruptTime > 5) {
if (mode != tweakmode)
return;
if (rootLevel) {
if (menurootPos >= 8) {
menuroot = SpNml;
menurootPos = 1;
} else {
menuroot = (menuroots) menurootPos;
if (digitalRead(ROT_CCW_PIN) == LOW) {
menurootPos-- ; // Could be -5 or -10
} else {
menurootPos++;
}
menurootPos = min(8, max(0, menurootPos));
Serial.print("menu root pos: ");
Serial.println(menurootPos);
}
} else {
switch (menuroot) {
case SpNml:
virtualFloat = settings.setpointHigh;
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualFloat -= 0.5 ; // Could be -5 or -10
} else {
virtualFloat += 0.5;
}
settings.setpointHigh = min(30.0, max(15.0, virtualFloat));
break;
case AlDOn:
switch (alarmDayPos){
case 1:
virtualInt = settings.alarmDayIn[0];
break;
case 2:
virtualInt = settings.alarmDayIn[1];
break;
case 3:
virtualFloat = settings.setpointLow;
break;
}
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualInt--;
virtualFloat -= 0.5;
} else {
virtualInt++;
virtualFloat += 0.5;
}
switch (alarmDayPos){
case 1:
settings.alarmDayIn[0] = min(23, max(0, virtualInt));
alarms[2][0] = settings.alarmDayIn[0];
break;
case 2:
settings.alarmDayIn[1] = min(59, max(0, virtualInt));
alarms[2][1] = settings.alarmDayIn[1];
break;
case 3:
settings.setpointLow = min(30.0, max(15.0, virtualFloat));
break;
}
break;
case AlDOf:
switch (alarmDayPos){
case 1:
virtualInt = settings.alarmDayOut[0];
break;
case 2:
virtualInt = settings.alarmDayOut[1];
break;
case 3:
virtualFloat = settings.setpointLow;
break;
}
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualInt--;
virtualFloat -= 0.5;
} else {
virtualInt++;
virtualFloat += 0.5;
}
switch (alarmDayPos){
case 1:
settings.alarmDayOut[0] = min(23, max(0, virtualInt));
alarms[3][0] = settings.alarmDayOut[0];
break;
case 2:
settings.alarmDayOut[1] = min(59, max(0, virtualInt));
alarms[3][1] = settings.alarmDayOut[1];
break;
case 3:
settings.setpointLow = min(30.0, max(15.0, virtualFloat));
break;
}
break;
case AlNOn:
switch (alarmNightPos){
case 1:
virtualInt = settings.alarmNightIn[0];
break;
case 2:
virtualInt = settings.alarmNightIn[1];
break;
}
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualInt--;
} else {
virtualInt++;
}
switch (alarmNightPos){
case 1:
settings.alarmNightIn[0] = min(23, max(0, virtualInt));
alarms[0][0] = settings.alarmNightIn[0];
break;
case 2:
settings.alarmNightIn[1] = min(59, max(0, virtualInt));
alarms[0][1] = settings.alarmNightIn[1];
break;
}
break;
case AlNOf:
switch (alarmNightPos){
case 1:
virtualInt = settings.alarmNightOut[0];
break;
case 2:
virtualInt = settings.alarmNightOut[1];
break;
}
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualInt--;
} else {
virtualInt++;
}
switch (alarmNightPos){
case 1:
settings.alarmNightOut[0] = min(23, max(0, virtualInt));
alarms[1][0] = settings.alarmNightOut[0];
break;
case 2:
settings.alarmNightOut[1] = min(59, max(0, virtualInt));
alarms[1][1] = settings.alarmNightOut[1];
break;
}
break;
case SetTm:
switch (setTimePos){
case 1:
virtualInt = h > 0 ? h : 0;
break;
case 2:
virtualInt = m > 0 ? m : 0;
break;
}
if (digitalRead(ROT_CCW_PIN) == LOW) {
virtualInt--;
} else {
virtualInt++;
}
switch (setTimePos){
case 1:
h = min(23, max(0, virtualInt));
break;
case 2:
m = min(59, max(0, virtualInt));
break;
}
s = 0;
break;
}
}
// Keep track of when we were here last (no more than every 5ms)
lastInterruptTime = interruptTime;
}
}
// Place cursor on LCD to indicate current value to change. (Need work..)
void placeCursor(uint8_t (& order)[5], uint8_t & pos) {
uint8_t coord_index = order[pos];
if (coord_index == NULL){
pos = 0;
coord_index = order[pos];
}
if (pos == 0) {
rootLevel = true;
} else {
rootLevel = false;
}
uint8_t x = coordinates[coord_index][0];
uint8_t y = coordinates[coord_index][1];
lcd.setCursor(x, y);
}
// Check if button is pressed and tak action accordingly
void checkButton() {
// Long press while not in tweakmode enters tweakmode
// Long press while in tweakmode saves settings.
// Short press while in tweakmode iterates over values to change
static long lastLongPress = 0;
static uint8_t prevMode;
menuButton.read();
if (mode == tweakmode && menuButton.wasReleased() && millis() - lastLongPress > 500) {
// Iterater over menu roots/items
Serial.println("Button On");
if (mode == tweakmode) {
// iterate over menu items
Serial.print("rootlevel: ");
Serial.println(rootLevel ? "true" : "false");
switch (menuroot) {
case SpNml:
setpointPos++;
Serial.print("setpointPos: ");
Serial.println(setpointPos);
break;
case AlNOn:
case AlNOf:
alarmNightPos++;
Serial.print("alarmNightPos: ");
Serial.println(alarmNightPos);
break;
case AlDOn:
case AlDOf:
alarmDayPos++;
Serial.print("alarmDayPos: ");
Serial.println(alarmDayPos);
break;
case SetTm:
setTimePos++;
Serial.print("setTimePos: ");
Serial.println(setTimePos);
break;
case AlwOn:
if (prevMode == allwayson){
mode = normal;
} else {
mode = allwayson;
}
break;
case AlwOf:
if (prevMode == allwaysoff){
mode = normal;
} else {
mode = allwaysoff;
}
break;
default:
break;
}
}
} else if (menuButton.pressedFor(1000) && millis() - lastLongPress > 1000) {
// Initiate or exit tweak mode
Serial.println("Button Hold");
if (mode == tweakmode){
mode = prevMode;
//rootLevel = true;
setpointPos = 0;
alarmNightPos = 0;
alarmDayPos = 0;
setTimePos = 0;
lcd.noCursor();
lcd.noBlink();
// Set Time if last menu was SetTm
if (menuroot == SetTm) {
RtcDateTime timeNow = Rtc.GetDateTime();
RtcDateTime newTime = RtcDateTime(
timeNow.Year(),
timeNow.Month(),
timeNow.Day(),
h,
m,
s
);
Rtc.SetDateTime(newTime);
}
// Save updated settings to EEPROM
saveSettings();
} else {
prevMode = mode;
mode = tweakmode;
//rootLevel = true;
lcd.setCursor(11, 1); //M:< position of LCD
lcd.cursor();
lcd.blink();
}
Serial.print("mode: ");
Serial.println(mode == tweakmode ? "tweakmode" : "normal");
lastLongPress = millis();
}
}
// A set of default values to start with if nothing is found in EEPROM
void setDefaultSettings() {
settings.magic = 42;
settings.setpointHigh = 24.5;
settings.setpointLow = 18;
settings.alarmNightIn[0] = 23;
settings.alarmNightIn[1] = 30;
settings.alarmNightOut[0] = 5;
settings.alarmNightOut[1] = 30;
settings.alarmDayIn[0] = 8;
settings.alarmDayIn[1] = 30;
settings.alarmDayOut[0] = 15;
settings.alarmDayOut[1] = 30;
settingsLoaded = true;
Serial.println("Default settings applied");
}
// Save settings to EEPROM if needed
void saveSettings() {
Settings oldSettings = EEPROM.get(0, oldSettings);
bool update = false;
if (oldSettings.setpointHigh != settings.setpointHigh)
update = true;
else if (oldSettings.setpointLow != settings.setpointLow)
update = true;
else if (oldSettings.alarmNightIn[0] != settings.alarmNightIn[0])
update = true;
else if (oldSettings.alarmNightIn[1] != settings.alarmNightIn[1])
update = true;
else if (oldSettings.alarmNightOut[0] != settings.alarmNightOut[0])
update = true;
else if (oldSettings.alarmNightOut[1] != settings.alarmNightOut[1])
update = true;
else if (oldSettings.alarmDayIn[0] != settings.alarmDayIn[0])
update = true;
else if (oldSettings.alarmDayIn[1] != settings.alarmDayIn[1])
update = true;
else if (oldSettings.alarmDayOut[0] != settings.alarmDayOut[0])
update = true;
else if (oldSettings.alarmDayOut[1] != settings.alarmDayOut[1])
update = true;
if (update) {
EEPROM.put(0, settings);
Serial.println("Settings saved");
} else {
Serial.println("No changes");
}
}
// Load and apply settings from EEPROM if found
void loadSettings() {
EEPROM.get(0, settings);
// Check if we have settings stored
if (settings.magic != 42) {
settingsLoaded = false;
} else {
alarms[0][0] = settings.alarmNightIn[0];
alarms[0][1] = settings.alarmNightIn[1];
alarms[1][0] = settings.alarmNightOut[0];
alarms[1][1] = settings.alarmNightOut[1];
alarms[2][0] = settings.alarmDayIn[0];
alarms[2][1] = settings.alarmDayIn[1];
alarms[3][0] = settings.alarmDayOut[0];
alarms[3][1] = settings.alarmDayOut[1];
settingsLoaded = true;
Serial.println("Settings loaded");
}
}
// Determin if heater should be on or off depending on PID output
void adjustHeater() {
unsigned long now = millis();
if (now - windowStartTime > windowSize) {
//time to shift the Relay Window
windowStartTime += windowSize;
}
if (Output * windowSize > ((now - windowStartTime) * 100)) {
// Make sure heater is ON
switchHeater(true);
} else {
// Turn OFF heater
switchHeater(false);
}
}
// Switch on/off heater according to mode and PID output
void switchHeater(bool state) {
// Override state depending on modes
switch (mode) {
case allwaysoff: state = false;
break;
case night: state = false;
break;
default:
break;
}
if (state && heaterState) {
return;
} else if (!state && !heaterState) {
return;
} else if (!state && heaterState) {
Serial.println("Heater OFF");
Nexa.Device_Off(0);
heaterState = false;
} else if (state && !heaterState) {
Serial.println("Heater ON");
Nexa.Device_On(0);
heaterState = true;
}
}
// Convert alarm time array {HH, MM} to minutes for easier comparison
uint16_t timeToMin(uint8_t (& t)[2]) {
uint16_t hours_as_minutes = t[0] * 60;
return uint16_t(t[1] + hours_as_minutes);
}
// Check if time is within an alarm's in and out point
bool checkTimeRange(uint8_t (& in)[2], uint8_t (& out)[2]) {
RtcDateTime timeNow = Rtc.GetDateTime();
uint16_t on = timeToMin(in);
uint16_t off = timeToMin(out);
uint8_t n[2] = {timeNow.Hour(), timeNow.Minute()};
uint16_t now = timeToMin(n);
if (on < off)
if (now >= on && now < off)
return true;
else
return false;
if (on > off)
if (off == 0)
if (now >= on)
return true;
else
return false;
else
if (now >= on || now < off)
return true;
else
return false;
}
// Check if we should change mode due to an alarm
void checkAlarms() {
unsigned long now = millis();
if (lastAlarmCheck > 0 && now - lastAlarmCheck < 1000) {
return;
}
if (mode == allwayson || mode == allwaysoff || mode == tweakmode)
return;
// Night?
if (checkTimeRange(alarms[0], alarms[1])) {
mode = night;
// Daytime ?
} else if (checkTimeRange(alarms[2], alarms[3])) {
mode = daytime;
} else {
mode = normal;
}
}
// Read temperature from RTC module and feed it to the PID
void readTemp() {
unsigned long now = millis();
if (lastTempRead == 0 || now - lastTempRead >= 1000) {
RtcTemperature temp = Rtc.GetTemperature();
tempC = temp.AsFloatDegC();
// Update PID Input
Input = tempC;
// Update timer
lastTempRead = now;
}
}
// Draw information on the LCD
void refreshLCD() {
static unsigned long lastStatus = 0;
static uint16_t statusTimeout = 1000;
unsigned long now = millis();
if (mode == tweakmode) {
//Set lower timeout to display menu/tweak changes quicker
statusTimeout = 50;
} else {
statusTimeout = 1000;
}
//if (lastStatus == 0 || now - lastStatus >= statusTimeout) {
if (true) {
lcd.setCursor(0, 0);
printTime();
lcd.print(" T:");
lcd.print(tempC);
lcd.setCursor(0, 1);
//Setpoint
lcd.print("Sp:");
if (mode == tweakmode) {
switch (menuroot) {
case SpNml: lcd.print(settings.setpointHigh);
break;
case AlDOn: lcd.print(settings.setpointLow);
break;
case AlDOf: lcd.print(settings.setpointLow);
break;
default: lcd.print(Setpoint);
}
} else {
lcd.print(Setpoint);
}
// Mode
lcd.print(" M:");
switch (mode) {
case allwayson: lcd.print("AllOn");
break;
case allwaysoff: lcd.print("AlOff");
break;
case night: lcd.print("Night");
break;
case daytime: lcd.print("Day ");
break;
case normal: lcd.print("Norml");
break;
case tweakmode:
lcd.cursor();
lcd.blink();
switch (menuroot) {
case SpNml:
lcd.print("SpNml");
placeCursor(spMenuOrder, setpointPos);
break;
case AlwOn: lcd.print("AlwOn");
break;
case AlwOf: lcd.print("AlwOf");
break;
case AlNOn:
lcd.print("AlNOn");
placeCursor(alarmNightMenuOrder, alarmNightPos);
break;
case AlNOf:
lcd.print("AlNOf");
placeCursor(alarmNightMenuOrder, alarmNightPos);
break;
case AlDOn:
lcd.print("AlDOn");
placeCursor(alarmDayMenuOrder, alarmDayPos);
break;
case AlDOf:
lcd.print("AlDOf");
placeCursor(alarmDayMenuOrder, alarmDayPos);
break;
case SetTm:
lcd.print("SetTm");
placeCursor(setTimeMenuOrder, setTimePos);
break;
}
}
lastStatus = now;
}
}
// Format the time to represent now or an alarm in tweakmode
void printTime() {
if (mode == tweakmode) {
switch (menuroot){
case AlNOn:
printDigits(settings.alarmNightIn[0]);
lcd.print(":");
printDigits(settings.alarmNightIn[1]);
lcd.print(":");
printDigits(0);
return;
case AlNOf:
printDigits(settings.alarmNightOut[0]);
lcd.print(":");
printDigits(settings.alarmNightOut[1]);
lcd.print(":");
printDigits(0);
return;
case AlDOn:
printDigits(settings.alarmDayIn[0]);
lcd.print(":");
printDigits(settings.alarmDayIn[1]);
lcd.print(":");
printDigits(0);
return;
case AlDOf:
printDigits(settings.alarmDayOut[0]);
lcd.print(":");
printDigits(settings.alarmDayOut[1]);
lcd.print(":");
printDigits(0);
return;
case SetTm:
printDigits(h);
lcd.print(":");
printDigits(m);
lcd.print(":");
printDigits(s);
return;
}
}
// digital clock display of the time
RtcDateTime timeNow = Rtc.GetDateTime();
printDigits(timeNow.Hour());
lcd.print(":");
printDigits(timeNow.Minute());
lcd.print(":");
printDigits(timeNow.Second());
}
// Make sure we always have two digits in the time representation
void printDigits(int digits)
{
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}