-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patholed_rotary_encoder_thermostat.ino
1208 lines (978 loc) · 35.4 KB
/
oled_rotary_encoder_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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// https://github.com/ldijkman/arduino_w1209_thermostat_clone
// version 27 june 2019
// should become a safe arduino version of w1209 thermostat
//
// but far from finished
// i think in this rotary encoder i2c oled 128x64 version all is working now
// should become a safe arduino version of w1209 thermostat
// but far from finished
// parts of code used from
// http://www.circuitbasics.com/arduino-thermistor-temperature-sensor-tutorial/
// https://github.com/tehniq3/DS18B20_thermostat_4digit_7segment_led_display/blob/master/4dig7segm_ac_18b20_thermostat_ver4m7.ino
// Robust Rotary encoder reading
// Copyright John Main - best-microcontroller-projects.com
// https://www.best-microcontroller-projects.com/rotary-encoder.html
//
// this code came from
// http://sticker.tk/forum/index.php?action=view&id=296
// copyright then, now, and forever, luberth dijkman bangert 30 andijk the netherlands
// bob-a-job
// a nickle or dime would be apreciated Http://paypal.me/LDijkman
// if you gave me 5 cents for every device you program with this code
// i will be verry happy
// it doesnt hurt
// Http://paypal.me/LDijkman
//
// I Feel so lonely in this Universe ;-(
// if you change the program to make it do more or better
// would like to know/share
// https://m.facebook.com/luberth.dijkman
// http://sticker.tk/forum/index.php?action=view&id=296
#include <EEPROM.h>
//#include <LiquidCrystal_I2C.h> https://github.com/fmalpartida/New-LiquidCrystal
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
#include <Arduino.h>
#include <U8x8lib.h> //Using u8g2 library => search librarymanager for u8g2 and install
#include "pitches.h" //https://www.arduino.cc/en/Tutorial/toneMelody
// die zien we nooit meer terug cocktail trio vlooiencircus 1965 https://www.youtube.com/watch?v=DizwZ0uOX5A
// notes in the melody: //from #include "pitches.h"
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
//U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(12, 11, U8X8_PIN_NONE);
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE);
float Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
float CalibrationOffset = 0; //w1209 thermostat has -7 to +7 0.1 steps
float SwitchOnTemp = 30.0f;
byte TempByte;
float TempFloat;
int TempInt;
unsigned long TempLong;
byte val;
int RelaisState = 0;
byte CoolorHeat = 2; //1=cool 2=heat
int delayval;
float MaxTemp = 110;
float MinTemp = -40;
int LowTempAlarmVal = 0 ;
int HighTempAlarmVal = 100;
float relayonpointbelowsetpoint = -0.2;
float relayoffabovesetpoint = 0.1;
int menu = 0;
unsigned long starttime;
unsigned long timeon;
int MaxTimeRelayMayBeonInSeconds = 600;
int MaxTime2SetPoint = 300;
// Robust Rotary encoder reading
// Copyright John Main - best-microcontroller-projects.com
#define CLK 3
#define DATA 4
static uint8_t prevNextCode = 0;
static uint16_t store = 0;
int yesorno; //factory reset
void setup() {
//Serial.begin(115200);
Serial.begin(9600);
//u8x8.begin(20, 4);
// u8x8.setBacklightPin(3, POSITIVE);
// u8x8.setBacklight(HIGH);
u8x8.begin();
u8x8.setPowerSave(0);
u8x8.setFont(u8x8_font_pxplusibmcgathin_f);
// first time i use eeprom, thought it would be very hard to use
// i make steps of 5 in eeprom adres because i did not know what to use
// 5 is a nice step when i look with the eeprom read example from arduino
// not optimal use of eeprom but have eeprom more then i use
/*
// next is for test => normally commented
// erase eeprom all to 0
for (int i = 0 ; i < EEPROM.length() ; i++){EEPROM.put(i, 0);
//was eeprom.write but i think put is better put only writes when val not the same
u8x8.setCursor(2, 1);u8x8.print(i);u8x8.print(" Erase EEPROM");
} // erase eeprom all to 0
for (int i = 30 ; i > 0 ; i--){u8x8.setCursor(9, 3);u8x8.print(i);u8x8.print(" ");delay(500);}
// erase eeprom all to 0
*/
// first run ??? write some val to eeprom if value at eepromadres 666 not is 666
//if this is first run then val will not be 666 at eeprom adres 666 so next will be run
EEPROM.get(666, TempInt);
if (TempInt != 666) { // IF this is the first run THEN val at eeprom adres 666 is -1???
EEPROM.put(0, 31.00); // setpoint
EEPROM.put(5, 0.00); // callibration offset
EEPROM.put(10, -0.3); // below on
EEPROM.put(15, 0.3); // above off
EEPROM.put(20, 600); // max time in seconds relay on
EEPROM.put(25, 2); // 1 cool 2 heat
EEPROM.put(30, 90); // high temp alarm
EEPROM.put(35, 20); // low temp alarm
EEPROM.put(40, 300);
EEPROM.put(666, 666); // set eepromadres 666 to val 666 no need to call / run this anymore in future
u8x8.clear();
u8x8.setCursor(0, 0);
u8x8.print(F("Hi There! First run"));
u8x8.setCursor(0, 1);
u8x8.print(F("Have written value's"));
u8x8.setCursor(0, 2);
u8x8.print(F("to EEPROM "));
u8x8.setCursor(0, 3);
u8x8.print(F("Thanks for trying"));
for (int i = 30 ; i > 0 ; i--) {
u8x8.setCursor(17, 2);
u8x8.print(i);
u8x8.print(" ");
delay(500);
}
u8x8.clear();
}
// is written in eeprom like this
// 665 255
// 666 154 2x256+154=666
// 667 2
// 668 255
/*
0 30
1 0
2 240
3 65
4 255
5 0
6 0
7 128
8 178
9 255
10 154
11 153
12 153
13 190
14 255
15 154
16 153
17 153
18 62
19 255
20 88
21 2
22 255
23 255
24 255
25 255
26 255
27 255
28 255
29 255
30 90
31 0
32 255
33 255
34 255
35 20
36 0
37 255
38 255
39 255
40 255
*/
// read stored valeus from eeprom
EEPROM.get(0, SwitchOnTemp);
EEPROM.get(5, CalibrationOffset);
EEPROM.get(10, relayonpointbelowsetpoint);
EEPROM.get(15, relayoffabovesetpoint);
EEPROM.get(20, MaxTimeRelayMayBeonInSeconds);
EEPROM.get(25, CoolorHeat);
EEPROM.get(30, HighTempAlarmVal);
EEPROM.get(35, LowTempAlarmVal);
EEPROM.get(40, MaxTime2SetPoint);
// A0 NTC thermistor
// pinMode(2, INPUT_PULLUP); // set button
// pinMode(3, INPUT_PULLUP); // + button
// pinMode(4, INPUT_PULLUP); // - button
//rotary encoder
pinMode(2, INPUT_PULLUP); // D2 rotary encoder button
pinMode(CLK, INPUT); // D3
pinMode(CLK, INPUT_PULLUP);
pinMode(DATA, INPUT); // D4
pinMode(DATA, INPUT_PULLUP);
pinMode (8, OUTPUT); // D8 passive buzzer module
pinMode(10, OUTPUT); // relais
pinMode(11, OUTPUT); // lowtemp alarm relais
pinMode(12, OUTPUT); // hightemp alarm relais
pinMode(13, OUTPUT); // another relais
}
void loop() {
// int SetButton() = SetButton();
// int PlusButton = PlusButton();
// int MinButton = MinButton();
if (!SetButton()) { //if !=not setbutton pressed
menu = 1;
while (SetButton() == LOW) {
// loop until button released
// maybe a timer here
// alarm if button never released
u8x8.setCursor(0, 0);
u8x8.print(F(" "));
u8x8.setCursor(0, 1);
u8x8.print(F(" In Menu Now "));
u8x8.setCursor(0, 2);
u8x8.print(F(" Release Button "));
u8x8.setCursor(0, 3);
u8x8.print(F(" "));
u8x8.setCursor(0, 4);
u8x8.print(F(" "));
}
u8x8.clear();
}
//1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
//setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint setpoint
TempLong = millis(); //reset innactive time counter
if (menu == 1) {
u8x8.setCursor(0, 0);
u8x8.print(F("1 SetTemp"));
u8x8.setCursor(0, 1);
u8x8.print(SwitchOnTemp);
}
while (menu == 1) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
SwitchOnTemp = SwitchOnTemp + (rval / 10);
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(SwitchOnTemp);
u8x8.print(F(" "));
if (SwitchOnTemp > MaxTemp) SwitchOnTemp = MaxTemp;
if (SwitchOnTemp < MinTemp) SwitchOnTemp = MinTemp;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 2;
u8x8.clear();
delay(250);
}
}
EEPROM.get(0, TempFloat); // limmited write to eeprom = read is unlimmited
if (SwitchOnTemp != TempFloat) { // only write to eeprom if value is different
EEPROM.put(0, SwitchOnTemp); // put already checks if val is needed to write
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempFloat);
u8x8.print(F(" new= "));
u8x8.print(SwitchOnTemp);
for (int i = 0; i < 100; i++)Serial.println(F("SwitchOnTemp DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
//calibration calibration calibration calibration calibration calibration calibration calibration calibration calibration calibration
TempLong = millis(); //reset innactive time counter
if (menu == 2) {
u8x8.setCursor(0, 0);
u8x8.print(F("2 Cal. Offset"));
u8x8.setCursor(0, 1);
u8x8.print(CalibrationOffset);
}
while (menu == 2) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
CalibrationOffset = CalibrationOffset + (rval / 10);
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(CalibrationOffset);
u8x8.print(F(" "));
if (CalibrationOffset > 7)CalibrationOffset = 7;
if (CalibrationOffset < -7)CalibrationOffset = -7;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 3;
u8x8.clear();
delay(250);
}
}
EEPROM.get(5, TempFloat); // limmited write to eeprom = read is unlimmited
if (CalibrationOffset != TempFloat) { // only write to eeprom if value is different
EEPROM.put(5, CalibrationOffset); // i have no idea wat eeprom adress to use just jump to 5
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempFloat);
u8x8.print(F(" new= "));
u8x8.print(CalibrationOffset);
for (int i = 0; i < 100; i++)Serial.println(F("CalibrationOffset DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
//below set below set below set below set below set below set below set below set below set below set below set below set below set
TempLong = millis(); //reset innactive time counter
if (menu == 3) {
u8x8.setCursor(0, 0);
u8x8.print(F("3 Below Set"));
u8x8.setCursor(0, 1);
u8x8.print(relayonpointbelowsetpoint);
}
while (menu == 3) {
if ((millis() - TempLong) / 1000 > 20) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
relayonpointbelowsetpoint = relayonpointbelowsetpoint + (rval / 10);
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(relayonpointbelowsetpoint);
u8x8.print(F(" "));
if (relayonpointbelowsetpoint > -0.1)relayonpointbelowsetpoint = -0.1;
if (relayonpointbelowsetpoint < -2)relayonpointbelowsetpoint = -2;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 4;
u8x8.clear();
delay(250);
}
}
EEPROM.get(10, TempFloat); // limmited write to eeprom = read is unlimmited
if (relayonpointbelowsetpoint != TempFloat) { // only write to eeprom if value is different
EEPROM.put(10, relayonpointbelowsetpoint); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempFloat);
u8x8.print(F(" new= "));
u8x8.print(relayonpointbelowsetpoint);
for (int i = 0; i < 100; i++)Serial.println(F("relayonpointbelowsetpoint DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
//above set above set above set above set above set above set above set above set above set above set above set above set
TempLong = millis(); //reset innactive time counter
if (menu == 4) {
u8x8.setCursor(0, 0);
u8x8.print(F("4 Above Set"));
u8x8.setCursor(0, 1);
u8x8.print(relayoffabovesetpoint);
}
while (menu == 4) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
relayoffabovesetpoint = relayoffabovesetpoint + (rval / 10);
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(relayoffabovesetpoint);
u8x8.print(F(" "));
if (relayoffabovesetpoint > 2)relayoffabovesetpoint = 2;
if (relayoffabovesetpoint < 0)relayoffabovesetpoint = 0;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 5;
u8x8.clear();
delay(250);
}
}
EEPROM.get(15, TempFloat); // limmited write to eeprom = read is unlimmited
if (relayoffabovesetpoint != TempFloat) { // only write to eeprom if value is different
EEPROM.put(15, relayoffabovesetpoint); // i have no idea wat eeprom adress to use just jump to 5
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempFloat);
u8x8.print(F(" new= "));
u8x8.print(relayoffabovesetpoint);
for (int i = 0; i < 100; i++)Serial.println(F("relayoffabovesetpoint DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-5-
TempLong = millis(); //reset innactive time counter
if (menu == 5) {
u8x8.setCursor(0, 0);
u8x8.print(F("5 MaxT 2SetPoint"));
u8x8.setCursor(0, 1);
u8x8.print(MaxTime2SetPoint );
u8x8.print(F(" "));
}
while (menu == 5) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
MaxTime2SetPoint = MaxTime2SetPoint + rval;
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(MaxTime2SetPoint );
u8x8.print(F(" "));
if (MaxTime2SetPoint > MaxTimeRelayMayBeonInSeconds)MaxTime2SetPoint = MaxTimeRelayMayBeonInSeconds;
if (MaxTime2SetPoint < 0)MaxTime2SetPoint = 0;
}
if (!SetButton()) {
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 6;
u8x8.clear();
delay(250);
}
}
EEPROM.get(40, TempInt); // limmited write to eeprom = read is unlimmited
if (MaxTime2SetPoint != TempInt) { // only write to eeprom if value is different
EEPROM.put(40, MaxTime2SetPoint); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempInt);
u8x8.print(F(" new= "));
u8x8.print(MaxTime2SetPoint);
for (int i = 0; i < 100; i++)Serial.println(F("MaxTime2SetPoint DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-6-
//max t relay max t relay max t relay max t relay max t relay max t relay max t relay max t relay max t relay max t relay
TempLong = millis(); //reset innactive time counter
if (menu == 6) {
u8x8.setCursor(0, 0);
u8x8.print(F("6 Max T Relay on"));
u8x8.setCursor(0, 1);
u8x8.print(MaxTimeRelayMayBeonInSeconds);
}
while (menu == 6) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
MaxTimeRelayMayBeonInSeconds = MaxTimeRelayMayBeonInSeconds + rval;
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(MaxTimeRelayMayBeonInSeconds);
u8x8.print(F(" "));
if (MaxTimeRelayMayBeonInSeconds < 30)MaxTimeRelayMayBeonInSeconds = 30;
if (MaxTimeRelayMayBeonInSeconds > 3600)MaxTimeRelayMayBeonInSeconds = 3600;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 7;
u8x8.clear();
delay(250);
}
}
EEPROM.get(20, TempInt); // limmited write to eeprom = read is unlimmited
if (MaxTimeRelayMayBeonInSeconds != TempInt) { // only write to eeprom if value is different
EEPROM.put(20, MaxTimeRelayMayBeonInSeconds); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempInt);
u8x8.print(F(" new= "));
u8x8.print(MaxTimeRelayMayBeonInSeconds);
for (int i = 0; i < 100; i++)Serial.println(F("MaxTimeRelayMayBeonInSeconds DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-7-
//CoolorHeat CoolorHeat CoolorHeat CoolorHeat CoolorHeat CoolorHeat
TempLong = millis(); //reset innactive time counter
if (menu == 7) {
u8x8.setCursor(0, 0);
u8x8.print(F("7 Cool / Heat"));
u8x8.setCursor(0, 1);
if (CoolorHeat == 1)u8x8.print(F(" COOL "));
if (CoolorHeat == 2)u8x8.print(F(" HEAT "));
}
while (menu == 7) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
CoolorHeat = CoolorHeat + rval;
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(CoolorHeat);
u8x8.print(F(" "));
if (CoolorHeat < 1)CoolorHeat = 2;
if (CoolorHeat > 2)CoolorHeat = 1;
u8x8.setCursor(0, 1);
if (CoolorHeat == 1)u8x8.print(F(" COOL "));
if (CoolorHeat == 2)u8x8.print(F(" HEAT "));
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 8;
u8x8.clear();
delay(250);
}
}
EEPROM.get(25, TempByte); // limmited write to eeprom = read is unlimmited
if (CoolorHeat != TempByte) { // only write to eeprom if value is different
EEPROM.put(25, CoolorHeat); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempByte);
u8x8.print(F(" new= "));
if (CoolorHeat == 1)u8x8.print(F("COOL"));
if (CoolorHeat == 2)u8x8.print(F("HEAT"));
for (int i = 0; i < 100; i++)Serial.println(F("Cool/Heat DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-
TempLong = millis(); //reset innactive time counter
if (menu == 8) {
u8x8.setCursor(0, 0);
u8x8.print(F("8 Low Temp Alarm"));
u8x8.setCursor(0, 1);
u8x8.print(LowTempAlarmVal );
u8x8.print(F(" "));
}
while (menu == 8) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
LowTempAlarmVal = LowTempAlarmVal + rval;
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(LowTempAlarmVal );
u8x8.print(F(" "));
if (LowTempAlarmVal > MaxTemp)LowTempAlarmVal = MaxTemp;
if (LowTempAlarmVal < MinTemp)LowTempAlarmVal = MinTemp;
}
if (!SetButton()) {
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 9;
u8x8.clear();
delay(250);
}
}
EEPROM.get(35, TempInt); // limmited write to eeprom = read is unlimmited
if (LowTempAlarmVal != TempInt) { // only write to eeprom if value is different
EEPROM.put(35, LowTempAlarmVal); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempInt);
u8x8.print(F(" new= "));
u8x8.print(LowTempAlarmVal);
for (int i = 0; i < 100; i++)Serial.println(F("LowTempAlarmVal DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-
TempLong = millis(); //reset innactive time counter
if (menu == 9) {
u8x8.setCursor(0, 0);
u8x8.print(F("9 HighTemp Alarm"));
u8x8.setCursor(0, 1);
u8x8.print(HighTempAlarmVal );
}
while (menu == 9) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
HighTempAlarmVal = HighTempAlarmVal + rval;
TempLong = millis(); //reset innactive time counter
u8x8.setCursor(0, 1);
u8x8.print(HighTempAlarmVal );
u8x8.print(F(" "));
if (HighTempAlarmVal > MaxTemp)HighTempAlarmVal = MaxTemp;
if (HighTempAlarmVal < MinTemp)HighTempAlarmVal = MinTemp;
}
if (!SetButton()) { //if !=not setbutton pressed
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 10;
u8x8.clear();
delay(250);
}
}
EEPROM.get(30, TempInt); // limmited write to eeprom = read is unlimmited
if (HighTempAlarmVal != TempInt) { // only write to eeprom if value is different
EEPROM.put(30, HighTempAlarmVal); // i have no idea wat eeprom adress to use just jump to 10
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
u8x8.setCursor(0, 2);
u8x8.print(TempInt);
u8x8.print(F(" new= "));
u8x8.print(HighTempAlarmVal);
for (int i = 0; i < 100; i++)Serial.println(F("HighTempAlarmVal DATA WRITEN / SAVED TO EEPROM "));
u8x8.clear();
}
//10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10
TempLong = millis(); //reset innactive time counter
yesorno = 2;
if (menu == 10) {
u8x8.setCursor(0, 0);
u8x8.print(F("10 Factory Reset"));
u8x8.setCursor(6, 2);
if (yesorno == 1)u8x8.print(F("YES"));
if (yesorno == 2)u8x8.print(F("NO "));
}
while (menu == 10) {
if ((millis() - TempLong) > 10000) {
TimeOut();
break;
}
float rval;
if ( rval = read_rotary() ) {
yesorno = yesorno + rval;
TempLong = millis(); //reset innactive time counter
if (yesorno < 1)yesorno = 2;
if (yesorno > 2)yesorno = 1;
u8x8.setCursor(6, 2);
if (yesorno == 1)u8x8.print(F("YES"));
if (yesorno == 2)u8x8.print(F("NO "));
}
if (!digitalRead(2) && yesorno == 1) {
EEPROM.put(0, 30.00); // setpoint
EEPROM.put(5, 0.00); // callibration offset
EEPROM.put(10, -0.3); // below on
EEPROM.put(15, 0.3); // above off
EEPROM.put(20, 600); // max time in seconds relay on
EEPROM.put(25, 2); // 1=cool 2=heat
EEPROM.put(30, 90); // high temp alarm
EEPROM.put(35, 20); // low temp alarm
EEPROM.put(40, 300);
u8x8.clear();
u8x8.setCursor(0, 0);
u8x8.print(F("Saving to EEPROM"));
for (int i = 0; i < 100; i++)Serial.println(F(" valeus restored to factory settings "));
// read stored valeus from eeprom
EEPROM.get(0, SwitchOnTemp);
EEPROM.get(5, CalibrationOffset);
EEPROM.get(10, relayonpointbelowsetpoint);
EEPROM.get(15, relayoffabovesetpoint);
EEPROM.get(20, MaxTimeRelayMayBeonInSeconds);
EEPROM.get(25, CoolorHeat);
EEPROM.get(30, HighTempAlarmVal);
EEPROM.get(35, LowTempAlarmVal);
EEPROM.get(40, MaxTime2SetPoint);
delay(1000);
menu = 11;
u8x8.clear();
}
if (!digitalRead(2) && yesorno == 2) { //no + button
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 11;
u8x8.clear();
delay(250);
}
}
//11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11-11
TempLong = millis(); //reset innactive time counter
while (menu == 11) {
if ((millis() - TempLong) > 1200000) {
TimeOut();
break;
}
// Arduino UpTime RunTime
// runtime since boot/start days hours minutes seconds
// micros has a 70 minut overflow faster for testing
// millis has a 50 day overflow
// ISR timer 1 overflow counter?
// interrupt service routine to count the number of overflows of timer 0 1 2 ???
// is there something to count the number of overflows
// i do not know i am a beginner newbie
Serial.println(F(" rumtime menu"));
u8x8.setCursor(0, 0);
u8x8.print(F("11 RunTime"));
u8x8.setCursor(3, 2);
u8x8.print((micros() / 86400000) % 365); //should be millis() micros overflow is faster
u8x8.print(" ");
u8x8.print((micros() / 3600000) % 24); //should be millis() micros overflow is faster
u8x8.print(":");
u8x8.print((micros() / 60000) % 60); //should be millis() micros overflow is faster
u8x8.print(":");
u8x8.print((micros() / 1000) % 60); //should be millis() micros overflow is faster
u8x8.print(" ");
u8x8.setCursor(0, 4);
u8x8.print(micros());
u8x8.setCursor(0, 5);
u8x8.print(F("Copyright DLD"));
u8x8.setCursor(0, 6);
u8x8.print(F("Luberth Dijkman"));
u8x8.setCursor(0, 7);
u8x8.print(F("The Netherlands"));
if (SetButton() == LOW) {
while (SetButton() == LOW); // wait for release button do not fly trough all the menus
menu = 0;
u8x8.clear();
delay(250);
}
}
Read_NTC_Thermistor();
//Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15 + CalibrationOffset;
//Tf = (Tc * 9.0)/ 5.0 + 32.0; // fahrenheit
// compare actualtemp to switchpoint with offsets
//COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL COOL
if (CoolorHeat == 1) { // 1=cool 2=heat
u8x8.setCursor(0, 7);
u8x8.print(F(" Cool Mode "));
if (Tc < SwitchOnTemp + relayonpointbelowsetpoint) {
RelaisState = 0;
}
if (Tc > SwitchOnTemp + relayoffabovesetpoint) {
RelaisState = 1;
}
}
//HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT HEAT
if (CoolorHeat == 2) { // 1=cool 2=heat
u8x8.setCursor(0, 7);
u8x8.print(F(" Heat Mode "));
if (Tc < SwitchOnTemp + relayonpointbelowsetpoint) {
RelaisState = 1;
}
if (Tc > SwitchOnTemp + relayoffabovesetpoint) {
RelaisState = 0;
}
}
if (Tc < LowTempAlarmVal) {
for (int i = 0; i < 10; i++)Serial.println(F(" Alarm Temperature LOW "));
u8x8.setCursor(0, 2);
u8x8.print(F("Alarm Temp. LOW"));
buzzer(); //passive buzzer on D8
u8x8.setCursor(0, 2);
u8x8.print(F(" "));
digitalWrite(11, 1);
} else {
digitalWrite(11, 0);
}
if (Tc > HighTempAlarmVal) {
for (int i = 0; i < 10; i++)Serial.println(F(" Alarm Temperature HIGH "));
digitalWrite(12, 1);
u8x8.setCursor(0, 2);
u8x8.print(F("Alarm Temp. High"));
buzzer(); //passive buzzer on D8
u8x8.setCursor(0, 2);
u8x8.print(F(" "));
} else {
digitalWrite(12, 0);
}
Serial.print(F("D10 "));
Serial.print(RelaisState);
Serial.print(F(" A0 "));
Serial.print(Vo);
Serial.print(F(" Temp "));
Serial.print(Tc, 1); // 1 decimal
u8x8.setCursor(2, 1);
u8x8.print(Tc, 1);
u8x8.print(" C");//\337C Cdegree sign
if (Vo <= 60) {
for (int i = 0; i < 10; i++)Serial.println(F(" analogread < 60 ERROR LOW "));
RelaisState = 0; //turn off relays
u8x8.setCursor(0, 2);
u8x8.print(F(" Sensor Loss "));
buzzer(); //passive buzzer on D8
u8x8.setCursor(0, 2);
u8x8.print(F(" "));
}
if (Vo >= 960) {
for (int i = 0; i < 10; i++)Serial.println(F(" analogread > 960 ERROR HIGH "));
RelaisState = 0; //turn off relays
u8x8.setCursor(0, 2);
u8x8.print(F(" Sensor Shorted "));
buzzer(); //passive buzzer on D8
u8x8.setCursor(0, 2);
u8x8.print(F(" "));
}
//Serial.print("menu nr ");
//Serial.print(menu);
Serial.print(F(" Set "));
Serial.print(SwitchOnTemp);
u8x8.setCursor(10, 1);
u8x8.print(SwitchOnTemp, 1);
u8x8.print(" C"); //\337C Cdegree sign
//Serial.print(" TempFloat ");
//Serial.print(TempFloat);
//Serial.print(" TempInt ");
//Serial.print(TempInt);
//Serial.print(" TempLong ");
//Serial.print(TempLong);
Serial.print(F(" Below "));
Serial.print(relayonpointbelowsetpoint);
Serial.print(F(" Above "));
Serial.print(relayoffabovesetpoint);
Serial.print(F(" Offset "));
Serial.print(CalibrationOffset);