forked from reefangel/Libraries
/
ReefAngel.cpp
4156 lines (3967 loc) · 122 KB
/
ReefAngel.cpp
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
/*
* Copyright 2010 Reef Angel / Roberto Imai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Updated by: Curt Binder
* Updates Released under Apache License, Version 2.0
*/
#include <Globals.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include "ReefAngel.h"
#include <RA_Wifi.h>
byte ButtonPress = 0;
SIGNAL(PCINT0_vect) {
if (millis()-ButtonDebounce>600)
{
ButtonDebounce=millis();
ButtonPress++;
}
}
// Menu Headings
const prog_char Menu_0_label[] PROGMEM = "Main:";
#if !defined SIMPLE_MENU && !defined CUSTOM_MENU
const prog_char Menu_1_label[] PROGMEM = "Setup:";
#ifndef RemoveAllLights
const prog_char Menu_2_label[] PROGMEM = "Lights:";
#endif // RemoveAllLights
const prog_char Menu_3_label[] PROGMEM = "Temp:";
#if defined SetupExtras || defined ATOSetup
const prog_char Menu_4_label[] PROGMEM = "Timeouts:";
#endif // defined SetupExtras || defined ATOSetup
#endif // !defined SIMPLE_MENU && !defined CUSTOM_MENU
PROGMEM const char *menulabel_items[] = {
Menu_0_label,
#if !defined SIMPLE_MENU && !defined CUSTOM_MENU
Menu_1_label,
#ifndef RemoveAllLights
Menu_2_label,
#endif // RemoveAllLights
Menu_3_label,
#if defined SetupExtras || defined ATOSetup
Menu_4_label,
#endif // defined SetupExtras || defined ATOSetup
#endif // !defined SIMPLE_MENU && !defined CUSTOM_MENU
};
// Return menu entries
const prog_char Return_0_label[] PROGMEM = "<- Prev Menu";
const prog_char Return_1_label[] PROGMEM = "Exit";
//const prog_char Return_2_label[] PROGMEM = "Press to exit...";
PROGMEM const char *return_items[] = { Return_0_label, Return_1_label/*, Return_2_label*/ };
// NOTE for nested menus
// NOTE Menu labels can be a max of 20 characters
// Associate a menu name to a numeric value
// the total number must match the max number of menus
enum Menus {
MainMenu,
#if !defined SIMPLE_MENU && !defined CUSTOM_MENU
SetupMenu,
#ifndef RemoveAllLights
LightsMenu,
#endif // RemoveAllLights
TempsMenu,
#if defined SetupExtras || defined ATOSetup
TimeoutsMenu,
#endif // if defined SetupExtras || defined ATOSetup
// PreviousMenu,
#endif // !defined SIMPLE_MENU && !defined CUSTOM_MENU
// ExitMenu,
};
#ifdef CUSTOM_MENU
enum CustomMenuItem {
CustomMenu_1,
#if CUSTOM_MENU_ENTRIES >= 2
CustomMenu_2,
#endif // CUSTOM_MENU_ENTRIES >= 2
#if CUSTOM_MENU_ENTRIES >= 3
CustomMenu_3,
#endif // CUSTOM_MENU_ENTRIES >= 3
#if CUSTOM_MENU_ENTRIES >= 4
CustomMenu_4,
#endif // CUSTOM_MENU_ENTRIES >= 4
#if CUSTOM_MENU_ENTRIES >= 5
CustomMenu_5,
#endif // CUSTOM_MENU_ENTRIES >= 5
#if CUSTOM_MENU_ENTRIES >= 6
CustomMenu_6,
#endif // CUSTOM_MENU_ENTRIES >= 6
#if CUSTOM_MENU_ENTRIES >= 7
CustomMenu_7,
#endif // CUSTOM_MENU_ENTRIES >= 7
#if CUSTOM_MENU_ENTRIES >= 8
CustomMenu_8,
#endif // CUSTOM_MENU_ENTRIES >= 8
#if CUSTOM_MENU_ENTRIES >= 9
CustomMenu_9,
#endif // CUSTOM_MENU_ENTRIES >= 9
};
#else // CUSTOM_MENU
#ifdef SIMPLE_MENU
// This is the simplified menu
// Main Menu
const prog_char mainmenu_0_label[] PROGMEM = "Feeding";
const prog_char mainmenu_1_label[] PROGMEM = "Water Change";
const prog_char mainmenu_2_label[] PROGMEM = "ATO Clear";
const prog_char mainmenu_3_label[] PROGMEM = "Overheat Clear";
const prog_char mainmenu_4_label[] PROGMEM = "PH Calibration";
#ifdef SALINITYEXPANSION
const prog_char mainmenu_5_label[] PROGMEM = "Sal Calibration";
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
const prog_char mainmenu_6_label[] PROGMEM = "ORP Calibration";
#endif // ORPEXPANSION
#ifdef DateTimeSetup
const prog_char mainmenu_7_label[] PROGMEM = "Date / Time";
#endif // DateTimeSetup
#ifdef VersionMenu
const prog_char mainmenu_8_label[] PROGMEM = "Version";
#endif // VersionMenu
PROGMEM const char *mainmenu_items[] = {
mainmenu_0_label,
mainmenu_1_label,
mainmenu_2_label,
mainmenu_3_label,
mainmenu_4_label,
#ifdef SALINITYEXPANSION
mainmenu_5_label,
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
mainmenu_6_label,
#endif // ORPEXPANSION
#ifdef DateTimeSetup
mainmenu_7_label,
#endif // DateTimeSetup
#ifdef VersionMenu
mainmenu_8_label
#endif // VersionMenu
};
enum MainMenuItem {
MainMenu_FeedingMode,
MainMenu_WaterChangeMode,
MainMenu_ATOClear,
MainMenu_OverheatClear,
MainMenu_PHCalibration,
#ifdef SALINITYEXPANSION
MainMenu_SalinityCalibration,
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
MainMenu_ORPCalibration,
#endif // ORPEXPANSION
#ifdef DateTimeSetup
MainMenu_DateTime,
#endif // DateTimeSetup
#ifdef VersionMenu
MainMenu_Version
#endif // VersionMenu
};
#else // SIMPLE_MENU
// This is the standard menu
// Main Menu
const prog_char mainmenu_0_label[] PROGMEM = "Feeding";
const prog_char mainmenu_1_label[] PROGMEM = "Water Change";
#ifndef RemoveAllLights
const prog_char mainmenu_2_label[] PROGMEM = "Lights ->";
#endif // RemoveAllLights
const prog_char mainmenu_3_label[] PROGMEM = "Temps ->";
#if defined SetupExtras || defined ATOSetup
const prog_char mainmenu_4_label[] PROGMEM = "Timeouts ->";
#endif // if defined SetupExtras || defined ATOSetup
const prog_char mainmenu_5_label[] PROGMEM = "Setup ->";
#ifdef VersionMenu
const prog_char mainmenu_6_label[] PROGMEM = "Version";
#endif // VersionMenu
PROGMEM const char *mainmenu_items[] = {
mainmenu_0_label,
mainmenu_1_label,
#ifndef RemoveAllLights
mainmenu_2_label,
#endif // RemoveAllLights
mainmenu_3_label,
#if defined SetupExtras || defined ATOSetup
mainmenu_4_label,
#endif // if defined SetupExtras || defined ATOSetup
mainmenu_5_label,
#ifdef VersionMenu
mainmenu_6_label
#endif // VersionMenu
};
enum MainMenuItem {
MainMenu_FeedingMode,
MainMenu_WaterChangeMode,
#ifndef RemoveAllLights
MainMenu_Lights,
#endif // RemoveAllLights
MainMenu_Temps,
#if defined SetupExtras || defined ATOSetup
MainMenu_Timeouts,
#endif // if defined SetupExtras || defined ATOSetup
MainMenu_Setup,
#ifdef VersionMenu
MainMenu_Version
#endif // VersionMenu
};
// Setup MenuWavemakerSetup
#ifdef WavemakerSetup
const prog_char setupmenu_0_label[] PROGMEM = "Wavemaker";
#endif // WavemakerSetup
#ifdef DosingPumpSetup
const prog_char setupmenu_1_label[] PROGMEM = "Single Dose";
#endif // DosingPumpSetup
#ifdef DosingPumpIntervalSetup
const prog_char setupmenu_2_label[] PROGMEM = "Multi Dose";
#endif // DosingPumpIntervalSetup
const prog_char setupmenu_3_label[] PROGMEM = "Calibrate pH";
#ifdef SALINITYEXPANSION
const prog_char setupmenu_4_label[] PROGMEM = "Calibrate Sal";
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
const prog_char setupmenu_5_label[] PROGMEM = "Calibrate ORP";
#endif // ORPEXPANSION
#ifdef DateTimeSetup
const prog_char setupmenu_6_label[] PROGMEM = "Date / Time";
#endif // DateTimeSetup
PROGMEM const char *setupmenu_items[] = {
#ifdef WavemakerSetup
setupmenu_0_label,
#endif // WavemakerSetup
#ifdef DosingPumpSetup
setupmenu_1_label,
#endif // DosingPumpSetup
#ifdef DosingPumpIntervalSetup
setupmenu_2_label,
#endif // DosingPumpIntervalSetup
setupmenu_3_label,
#ifdef SALINITYEXPANSION
setupmenu_4_label,
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
setupmenu_5_label,
#endif // ORPEXPANSION
#ifdef DateTimeSetup
setupmenu_6_label
#endif // DateTimeSetup
};
enum SetupMenuItem {
#ifdef WavemakerSetup
SetupMenu_Wavemaker,
#endif // WavemakerSetup
#ifdef DosingPumpSetup
SetupMenu_DosingPump,
#endif // DosingPumpSetup
#ifdef DosingPumpIntervalSetup
SetupMenu_DosingPumpInterval,
#endif // DosingPumpIntervalSetup
SetupMenu_CalibratePH,
#ifdef SALINITYEXPANSION
SetupMenu_CalibrateSalinity,
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
SetupMenu_CalibrateORP,
#endif // ORPEXPANSION
#ifdef DateTimeSetup
SetupMenu_DateTime
#endif // DateTimeSetup
};
#ifndef RemoveAllLights
// Lights Menu
const prog_char lightsmenu_0_label[] PROGMEM = "Lights On";
const prog_char lightsmenu_1_label[] PROGMEM = "Lights Off";
#ifdef MetalHalideSetup
const prog_char lightsmenu_2_label[] PROGMEM = "Metal Halides";
const prog_char lightsmenu_3_label[] PROGMEM = "MH On Delay";
#endif // MetalHalideSetup
#ifdef StandardLightSetup
const prog_char lightsmenu_4_label[] PROGMEM = "Standard Lights";
#endif // StandardLightSetup
#ifdef DisplayLEDPWM
const prog_char lightsmenu_5_label[] PROGMEM = "LED PWM";
#endif // DisplayLEDPWM
PROGMEM const char *lightsmenu_items[] = {
lightsmenu_0_label, lightsmenu_1_label,
#ifdef MetalHalideSetup
lightsmenu_2_label,
lightsmenu_3_label,
#endif // MetalHalideSetup
#ifdef StandardLightSetup
lightsmenu_4_label,
#endif // StandardLightSetup
#ifdef DisplayLEDPWM
lightsmenu_5_label
#endif // DisplayLEDPWM
};
enum LightsMenuItem {
LightsMenu_On,
LightsMenu_Off,
#ifdef MetalHalideSetup
LightsMenu_MetalHalides,
LightsMenu_MetalHalideDelay,
#endif // MetalHalideSetup
#ifdef StandardLightSetup
LightsMenu_StandardLights,
#endif // StandardLightSetup
#ifdef DisplayLEDPWM
LightsMenu_LEDPWM
#endif // DisplayLEDPWM
};
#endif // RemoveAllLights
// Temps Menu
const prog_char tempsmenu_0_label[] PROGMEM = "Heater";
const prog_char tempsmenu_1_label[] PROGMEM = "Chiller";
#ifdef OverheatSetup
const prog_char tempsmenu_2_label[] PROGMEM = "Overheat Set";
#endif // OverheatSetup
const prog_char tempsmenu_3_label[] PROGMEM = "Overheat Clear";
PROGMEM const char *tempsmenu_items[] = {
tempsmenu_0_label,
tempsmenu_1_label,
#ifdef OverheatSetup
tempsmenu_2_label,
#endif // OverheatSetup
tempsmenu_3_label};
enum TempsMenuItem {
TempsMenu_Heater,
TempsMenu_Chiller,
#ifdef OverheatSetup
TempsMenu_Overheat,
#endif // OverheatSetup
TempsMenu_OverheatClr
};
// Timeouts Menu
#if defined SetupExtras || defined ATOSetup
#ifdef ATOSetup
const prog_char timeoutsmenu_0_label[] PROGMEM = "ATO Set";
#ifdef SingleATOSetup
const prog_char timeoutsmenu_1_label[] PROGMEM = "ATO Interval";
#endif // SingleATOSetup
const prog_char timeoutsmenu_2_label[] PROGMEM = "ATO Clear";
#endif // ATOSetup
#ifdef SetupExtras
const prog_char timeoutsmenu_3_label[] PROGMEM = "Feeding";
const prog_char timeoutsmenu_4_label[] PROGMEM = "LCD";
#endif // SetupExtras
PROGMEM const char *timeoutsmenu_items[] = {
#ifdef ATOSetup
timeoutsmenu_0_label,
#ifdef SingleATOSetup
timeoutsmenu_1_label,
#endif // SingleATOSetup
timeoutsmenu_2_label,
#endif // ATOSetup
#ifdef SetupExtras
timeoutsmenu_3_label,
timeoutsmenu_4_label
#endif // SetupExtras
};
enum TimeoutsMenuItem {
#ifdef ATOSetup
TimeoutsMenu_ATOSet,
#ifdef SingleATOSetup
TimeoutsMenu_ATOHrInterval,
#endif // SingleATOSetup
TimeoutsMenu_ATOClear,
#endif // ATOSetup
#ifdef SetupExtras
TimeoutsMenu_Feeding,
TimeoutsMenu_LCD
#endif // SetupExtras
};
#endif // if defined SetupExtras || defined ATOSetup
#endif // SIMPLE_MENU
#endif // CUSTOM_MENU
ReefAngelClass::ReefAngelClass()
{
#if defined(__AVR_ATmega2560__)
PCMSK0 |= 128;
#else // __AVR_ATmega2560__
PCMSK0 |= 32;
#endif // __AVR_ATmega2560__
PCICR |= 1;
}
void ReefAngelClass::Init()
{
Wire.begin();
Serial.begin(57600);
#ifdef __PLUS_SPECIAL_WIFI__
Serial1.begin(57600);
#endif // __PLUS_SPECIAL_WIFI__
// issue #2 - Piezo Not needed anymore
// pinMode(Piezo, OUTPUT);
digitalWrite(lowATOPin,HIGH); //pull up resistor on lowATOPin
digitalWrite(highATOPin,HIGH); //pull up resistor on highATOPin
LCD.Init();
Joystick.Init();
TempSensor.Init();
setSyncProvider(RTC.get); // the function to get the time from the RTC
setSyncInterval(SECS_PER_HOUR); // Changed to sync every hour.
RAStart=now();
LastStart = RAStart; // Set the time normal mode is started
LCD.BacklightOn();
Relay.AllOff();
OverheatProbe = T2_PROBE;
TempProbe = T1_PROBE;
if (InternalMemory.IMCheck_read()!=0x5241494D) //0x5241494D
{
char temptext[25];
while(1)
{
strcpy_P(temptext, NoIMCheck);
LCD.DrawText(ModeScreenColor,DefaultBGColor,13,50,temptext);
strcpy_P(temptext, NoIMCheck1);
LCD.DrawText(ModeScreenColor,DefaultBGColor,50,75,temptext);
}
}
#ifdef ENABLE_ATO_LOGGING
AtoEventCount = 0;
#endif // ENABLE_ATO_LOGGING
#ifdef ENABLE_EXCEED_FLAGS
InternalMemory.write(Overheat_Exceed_Flag, 0);
InternalMemory.write(ATO_Exceed_Flag, 0);
#endif // ENABLE_EXCEED_FLAGS
PHMin = InternalMemory.PHMin_read();
PHMax = InternalMemory.PHMax_read();
#ifdef SALINITYEXPANSION
SalMax = InternalMemory.SalMax_read();
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
ORPMin = InternalMemory.ORPMin_read();
ORPMax = InternalMemory.ORPMax_read();
#endif // ORPEXPANSION
taddr = InternalMemory.T1Pointer_read();
Params.Salinity=0;
Params.ORP=0;
if ((taddr>120) || (taddr<0))
{
InternalMemory.T1Pointer_write(0);
taddr = 0;
}
Timer[FEEDING_TIMER].SetInterval(InternalMemory.FeedingTimer_read()); // Default Feeding timer
if ( InternalMemory.LCDTimer_read() < 60 ) InternalMemory.LCDTimer_write(60); // if it's less than 60, force it to 60
Timer[LCD_TIMER].SetInterval(InternalMemory.LCDTimer_read()); // LCD Sleep Mode timer
Timer[LCD_TIMER].Start(); // start timer
Timer[PORTAL_TIMER].SetInterval(300); // Portal
Timer[PORTAL_TIMER].Start(); // start timer
Timer[STORE_PARAMS_TIMER].SetInterval(720); // Store Params
Timer[STORE_PARAMS_TIMER].ForceTrigger();
#if defined DisplayLEDPWM && ! defined RemoveAllLights
// Restore PWM values
PWM.SetActinic(InternalMemory.LEDPWMActinic_read());
PWM.SetDaylight(InternalMemory.LEDPWMDaylight_read());
#endif // DisplayLEDPWM && ! defined RemoveAllLights
// Set the default ports to be turned on & off during the 2 modes
// To enable a port to be toggled, place a 1 in the appropriate position
// Default to have ports 4, 5, & 8 toggled
// Override in Setup function of PDE
// Port 87654321
FeedingModePorts = B10110000;
WaterChangePorts = B10110000;
// Set the ports that get shutoff when the overheat value is reached
// Default to have port 3 shutoff
// Port 87654321
OverheatShutoffPorts = B00000100;
// DelayedOn ports, do not manually modify this variable, let the DelayedOn function modify it
DelayedOnPorts = 0;
#ifdef RelayExp
// Expansion Module ports to toggle, defaults to not toggle any ports
for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
{
FeedingModePortsE[i] = 0;
WaterChangePortsE[i] = 0;
OverheatShutoffPortsE[i] = 0;
DelayedOnPortsE[i] = 0;
#ifndef RemoveAllLights
LightsOnPortsE[i] = 0;
#endif // RemoveAllLights
}
#endif // RelayExp
#ifndef RemoveAllLights
// Set the ports that get turned on when you select the Lights On
// Default to have ports 2 & 3 turn on
// Port 87654321
LightsOnPorts = B00000110;
#endif // RemoveAllLights
#ifndef CUSTOM_MENU
// Initialize the Nested Menus
InitMenus();
#endif // CUSTOM_MENU
#if defined WDT || defined WDT_FORCE
// enable watchdog timer for 1 second. consider allowing this option to be configured.
if ( wdtenabled ) wdt_enable(WDTO_1S);
#if defined(__AVR_ATmega2560__)
wdt_enable(WDTO_1S);
#endif // __AVR_ATmega2560__
#endif // defined WDT || defined WDT_FORCE
#ifdef wifi
EM = PWMEbit + RFEbit + AIbit + Salbit + ORPbit + IObit;
#ifdef RelayExp
for (byte a=0;a<InstalledRelayExpansionModules;a++)
{
REM+=1<<a;
}
#else // RelayExp
REM = 0;
#endif // RelayExp
#endif // wifi
#ifdef CUSTOM_VARIABLES
for ( byte EID = 0; EID < 8; EID++ )
{
CustomVar[EID]=0;
}
#endif //CUSTOM_VARIABLES
}
void ReefAngelClass::Refresh()
{
pingSerial();
#if defined WDT || defined WDT_FORCE
wdt_reset();
#endif // defined WDT || defined WDT_FORCE
#ifdef DisplayLEDPWM
boolean LightRelayOn=false;
for (int l=0;l<8;l++)
{
if (LightsOnPorts & 1<<l)
if (ReefAngel.Relay.RelayMaskOn & 1<<l) LightRelayOn=true;
}
if (PWM.LightsOverride) LightRelayOn=true;
if (LightRelayOn)
{
PWM.SetActinic(InternalMemory.LEDPWMActinic_read());
PWM.SetDaylight(InternalMemory.LEDPWMDaylight_read());
}
// issue #3: Redundant code
// issue #12: Revert back
analogWrite(actinicPWMPin, PWM.GetActinicValue()*2.55);
analogWrite(daylightPWMPin, PWM.GetDaylightValue()*2.55);
#endif // DisplayLEDPWM
#ifdef RFEXPANSION
byte RFRecv=0;
RFRecv=RF.RFCheck();
if (RFRecv==1)
{
ClearScreen(DefaultBGColor);
FeedingModeStart();
}
if (RFRecv==2)
{
Timer[FEEDING_TIMER].ForceTrigger();
}
if (DisplayedMenu!=FEEDING_MODE && RF.UseMemory) RF.SetMode(InternalMemory.RFMode_read(),InternalMemory.RFSpeed_read(),InternalMemory.RFDuration_read());
#endif // RFEXPANSION
#ifdef AI_LED
if (millis()-AI.AImillis>AI.StreamDelay)
{
AI.Send();
AI.AImillis=millis();
}
#endif // AI_LED
#if defined PWMEXPANSION && defined DisplayLEDPWM
PWM.ExpansionWrite();
#endif // PWMEXPANSION
#ifdef IOEXPANSION
IO.GetChannel();
#endif // IOEXPANSION
if (ds.read_bit()==0) return; // ds for OneWire TempSensor
now();
#ifdef DirectTempSensor
LCD.Clear(DefaultBGColor,0,0,1,1);
Params.Temp[T1_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT1);
LCD.Clear(DefaultBGColor,0,0,1,1);
Params.Temp[T2_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT2);
LCD.Clear(DefaultBGColor,0,0,1,1);
Params.Temp[T3_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT3);
LCD.Clear(DefaultBGColor,0,0,1,1);
Params.PH=analogRead(PHPin);
Params.PH=map(Params.PH, PHMin, PHMax, 700, 1000); // apply the calibration to the sensor reading
Params.PH=constrain(Params.PH,100,1400);
LCD.Clear(DefaultBGColor,0,0,1,1);
#if defined SALINITYEXPANSION
Params.Salinity=Salinity.Read();
Params.Salinity=map(Params.Salinity, 0, SalMax, 60, 350); // apply the calibration to the sensor reading
// Salinity Compensation was contributed by ahmedess
// http://forum.reefangel.com/viewtopic.php?p=7386#p7386
if (Salinity.TemperatureCompensation)
{
double SalCompensation;
if (TempSensor.unit && Params.Temp[T1_PROBE])
SalCompensation=Params.Salinity/(1+((Params.Temp[T1_PROBE]-250)*0.0024));
else
SalCompensation=Params.Salinity/(1+((Params.Temp[T1_PROBE]-770)*0.001333));
Params.Salinity=round(SalCompensation);
}
LCD.Clear(DefaultBGColor,0,0,1,1);
#endif // defined SALINITYEXPANSION
#if defined ORPEXPANSION
Params.ORP=ORP.Read();
if (Params.ORP!=0)
{
Params.ORP=map(Params.ORP, ORPMin, ORPMax, 0, 470); // apply the calibration to the sensor reading
Params.ORP=constrain(Params.ORP,0,550);
}
LCD.Clear(DefaultBGColor,0,0,1,1);
#endif // defined ORPEXPANSION
TempSensor.RequestConversion();
LCD.Clear(DefaultBGColor,0,0,1,1);
#else // DirectTempSensor
int x = TempSensor.ReadTemperature(TempSensor.addrT1);
LCD.Clear(DefaultBGColor,0,0,1,1);
int y;
y = x - Params.Temp[T1_PROBE];
// check to make sure the temp readings aren't beyond max allowed
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T1_PROBE] == 0 || ~x) Params.Temp[T1_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT2);
LCD.Clear(DefaultBGColor,0,0,1,1);
y = x - Params.Temp[T2_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T2_PROBE] == 0 || ~x) Params.Temp[T2_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT3);
LCD.Clear(DefaultBGColor,0,0,1,1);
y = x - Params.Temp[T3_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T3_PROBE] == 0 || ~x) Params.Temp[T3_PROBE] = x;
Params.PH=0;
for (int a=0;a<20;a++)
{
Params.PH+=analogRead(PHPin);
}
Params.PH/=20;
LCD.Clear(DefaultBGColor,0,0,1,1);
Params.PH=map(Params.PH, PHMin, PHMax, 700, 1000); // apply the calibration to the sensor reading
Params.PH=constrain(Params.PH,100,1400);
#if defined SALINITYEXPANSION
Params.Salinity=Salinity.Read();
Params.Salinity=map(Params.Salinity, 0, SalMax, 60, 350); // apply the calibration to the sensor reading
LCD.Clear(DefaultBGColor,0,0,1,1);
#endif // defined SALINITYEXPANSION
#if defined ORPEXPANSION
Params.ORP=ORP.Read();
if (Params.ORP!=0)
{
Params.ORP=map(Params.ORP, ORPMin, ORPMax, 0, 470); // apply the calibration to the sensor reading
Params.ORP=constrain(Params.ORP,0,550);
}
LCD.Clear(DefaultBGColor,0,0,1,1);
#endif // defined ORPEXPANSION
TempSensor.RequestConversion();
#endif // DirectTempSensor
}
void ReefAngelClass::SetTemperatureUnit(byte unit)
{
// 0 (or DEGREE_F) for farenheit
// 1 (or DEGREE_C) for celsius
TempSensor.unit = unit;
}
void ReefAngelClass::ConvertTempUnit()
{
// check to see if the internal memory values are set correctly
// if they are not, convert them from F to C or vice versa
int x;
if ( TempSensor.unit )
{
// C
// if the values are larger than the highest temp, then we know we have F stored
bool fConvert = false;
x = InternalMemory.HeaterTempOn_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.HeaterTempOn_write(x);
}
x = InternalMemory.HeaterTempOff_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.HeaterTempOff_write(x);
}
x = InternalMemory.ChillerTempOn_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.ChillerTempOn_write(x);
}
x = InternalMemory.ChillerTempOff_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.ChillerTempOff_write(x);
}
x = InternalMemory.OverheatTemp_read();
if ( (x > DEGREE_C_OVERHEAT_HIGH_TEMP) || fConvert )
{
x = CONVERT_TO_C(x);
InternalMemory.OverheatTemp_write(x);
}
}
else
{
// F
// if the values are smaller than lowest temp, then we know we have C stored
bool fConvert = false;
x = InternalMemory.HeaterTempOn_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.HeaterTempOn_write(x);
}
x = InternalMemory.HeaterTempOff_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.HeaterTempOff_write(x);
}
x = InternalMemory.ChillerTempOn_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.ChillerTempOn_write(x);
}
x = InternalMemory.ChillerTempOff_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.ChillerTempOff_write(x);
}
x = InternalMemory.OverheatTemp_read();
if ( (x < DEGREE_F_OVERHEAT_LOW_TEMP) || fConvert )
{
x = CONVERT_TO_F(x);
InternalMemory.OverheatTemp_write(x);
}
}
}
void ReefAngelClass::StandardLights(byte LightsRelay, byte OnHour, byte OnMinute, byte OffHour, byte OffMinute)
{
if (NumMins(OffHour,OffMinute) > NumMins(OnHour,OnMinute))
{
if (NumMins(hour(),minute()) >= NumMins(OnHour,OnMinute)) Relay.On(LightsRelay); else Relay.Off(LightsRelay);
if (NumMins(hour(),minute()) >= NumMins(OffHour,OffMinute)) Relay.Off(LightsRelay);
}
else
{
if (NumMins(hour(),minute()) >= NumMins(OffHour,OffMinute)) Relay.Off(LightsRelay); else Relay.On(LightsRelay);
if (NumMins(hour(),minute()) >= NumMins(OnHour,OnMinute)) Relay.On(LightsRelay);
}
}
void ReefAngelClass::MHLights(byte LightsRelay, byte OnHour, byte OnMinute, byte OffHour, byte OffMinute, byte MHDelay)
{
unsigned int MHTimer = MHDelay;
MHTimer *= SECS_PER_MIN;
if ( now()-RAStart > MHTimer )
StandardLights(LightsRelay, OnHour, OnMinute, OffHour, OffMinute);
}
void ReefAngelClass::StandardHeater(byte HeaterRelay, int LowTemp, int HighTemp)
{
if (Params.Temp[TempProbe] == 0) return; // Don't turn the heater on if the temp is reading 0
if (Params.Temp[TempProbe] <= LowTemp && Params.Temp[TempProbe] > 0) Relay.On(HeaterRelay); // If sensor 1 temperature <= LowTemp - turn on heater
if (Params.Temp[TempProbe] >= HighTemp) Relay.Off(HeaterRelay); // If sensor 1 temperature >= HighTemp - turn off heater
}
void ReefAngelClass::StandardFan(byte FanRelay, int LowTemp, int HighTemp)
{
if (Params.Temp[TempProbe] == 0) return; // Don't turn the fan/chiller on if the temp is reading 0
if (Params.Temp[TempProbe] >= HighTemp) Relay.On(FanRelay); // If sensor 1 temperature >= HighTemp - turn on fan
if (Params.Temp[TempProbe] <= LowTemp) Relay.Off(FanRelay); // If sensor 1 temperature <= LowTemp - turn off fan
}
void ReefAngelClass::CO2Control(byte CO2Relay, int LowPH, int HighPH)
{
if (Params.PH <= LowPH) Relay.Off(CO2Relay); // If PH <= LowPH - turn on CO2
if (Params.PH >= HighPH) Relay.On(CO2Relay); // If sensor 1 PH >= HighPH - turn off CO2
}
void ReefAngelClass::PHControl(byte PHControlRelay, int LowPH, int HighPH)
{
if (Params.PH <= LowPH) Relay.On(PHControlRelay); // If PH <= LowPH - turn on PHControlRelay
if (Params.PH >= HighPH) Relay.Off(PHControlRelay); // If sensor 1 PH >= HighPH - turn off PHControlRelay
}
void ReefAngelClass::StandardATO(byte ATORelay, int ATOTimeout)
{
// Input: Relay port and timeout value (max number of seconds that ATO pump is allowed to run)
unsigned long TempTimeout = ATOTimeout;
TempTimeout *= 1000;
/*
Is the low switch active (meaning we need to top off) and are we not currently topping off
Then we set the timer to be now and start the topping pump
*/
if ( LowATO.IsActive() && ( !LowATO.IsTopping()) )
{
LowATO.Timer = millis();
LowATO.StartTopping();
Relay.On(ATORelay);
}
// If the high switch is activated, this is a safeguard to prevent over running of the top off pump
if ( HighATO.IsActive() )
{
LowATO.StopTopping(); // stop the low ato timer
Relay.Off(ATORelay);
}
/*
If the current time minus the start time of the ATO pump is greater than the specified timeout value
AND the ATO pump is currently running:
We turn on the status LED and shut off the ATO pump
This prevents the ATO pump from contniously running.
*/
if ( (millis()-LowATO.Timer > TempTimeout) && LowATO.IsTopping() )
{
LED.On();
#ifdef ENABLE_EXCEED_FLAGS
InternalMemory.write(ATO_Exceed_Flag, 1);
#endif // ENABLE_EXCEED_FLAGS
Relay.Off(ATORelay);
#ifdef ENABLE_ATO_LOGGING
// bump the counter if a timeout occurs
AtoEventCount++;
if ( AtoEventCount >= MAX_ATO_LOG_EVENTS ) { AtoEventCount = 0; }
#endif // ENABLE_ATO_LOGGING
}
}
void ReefAngelClass::SingleATO(bool bLow, byte ATORelay, int intTimeout, byte byteHrInterval)
{
/*
If the switch is active, the float is opposite of the 2 wires,
Check if we are not currently topping, if we are not check if we can run
If we have an hour interval, check if we can run
If we can run, activate the pump because we need water
Otherwise the switch is not active, we need to see if we are currently topping
If we are topping, then we need to stop the pump because we are topped off
*/
bool bCanRun = true;
static int iLastTop = -1;
if ( byteHrInterval )
{
int iSafeTop = NumMins(hour(), minute()) - iLastTop;
if ( iSafeTop < 0 )
{
iSafeTop += 1440;
}
if ( (iSafeTop < (byteHrInterval * 60)) && (iLastTop >= 0) )
{
bCanRun = false;
}
}
RA_ATOClass *ato;
if ( bLow )
{
ato = &LowATO;
}
else
{
ato = &HighATO;
}
unsigned long t = intTimeout;
t *= 1000;
if ( ato->IsActive() )
{
if ( (! ato->IsTopping()) && bCanRun )
{
ato->Timer = millis();
ato->StartTopping();
Relay.On(ATORelay);
}
}
else
{
// not active
if ( ato->IsTopping() )
{
iLastTop = NumMins(hour(), minute());
ato->StopTopping();
Relay.Off(ATORelay);
}
}
if ( ((millis() - ato->Timer) > t) && ato->IsTopping() )
{
LED.On();
#ifdef ENABLE_EXCEED_FLAGS
InternalMemory.write(ATO_Single_Exceed_Flag, 1);
#endif // ENABLE_EXCEED_FLAGS
Relay.Off(ATORelay);
#ifdef ENABLE_ATO_LOGGING
// bump the counter if a timeout occurs
AtoEventCount++;
if ( AtoEventCount >= MAX_ATO_LOG_EVENTS ) { AtoEventCount = 0; }
#endif // ENABLE_ATO_LOGGING
}
}
void ReefAngelClass::DosingPump(byte DPRelay, byte DPTimer, byte OnHour, byte OnMinute, int RunTime)
{
/*
This function configures and sets up the dosing pump and turns it on at the appropriate time
Once the timer has expired for the dosing pump, it shuts it off
DPRelay - relay dosing pump is plugged into (0-8)
Timer - timer to control dosing pump
OnHour & OnMinute - time to turn on the dosing pump (in 24hr based time)
RunTime - duration to run the pump
*/
// Let's see if it's supposed to start running the timer now
if ( (NumMins(hour(), minute()) == NumMins(OnHour, OnMinute)) && (second() == 0) )
{
Relay.On(DPRelay);
//LED.On();
Timer[DPTimer].SetInterval(RunTime);
Timer[DPTimer].Start();
}
// is the timer expired?
if ( Timer[DPTimer].IsTriggered() )
{
// timer expired, so let's shut off the pump
Relay.Off(DPRelay);
//LED.Off();
}
}
void ReefAngelClass::DosingPumpRepeat(byte DPRelay, int OffsetMinute, int RepeatMinute, int RunTime)
{
// Old code has been replaced with dedvalson (Don) - 01/06/2012
/*
This function runs the specified relay for the RunTime seconds every RepeatMinute minutes.
So you can run the relay for 10 seconds every 60 minutes (1 hour)
This function bases the RepeatMinute off of Midnight (00:00) of the current day. It uses midnight to
compute when the pump will run.
DPRelay - Relay that contains the dosing pump
Timer - number of the timer in the timer array to use
RepeatMinute - number of minutes to wait before running the pump again
RunTime - duration (in seconds) to run the pump
*/