This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ino
2127 lines (2016 loc) · 88.2 KB
/
main.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
#include <Arduino.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <string.h>
bool invertRelay = false; //------------------IF YOUR RELAYS OPERATE THE OPPOSITE OF HOW YOU EXPECT, CHANGE THIS TO 'TRUE'
unsigned long systemClock; //-----------------SYSTEM CLOCK [VERY IMPORTANT]
unsigned long lastPulse; //-------------------LAST void loop() PULSE
bool failsafeMode = false; // If panel fails to boot, you can enter a reduced-feature down panel that is guaranteed to work
char *firmwareRev = "v1.5"; //VERSION
int EEPROMVersion = 1; //version control to rewrite eeprom after update
int EEPROMBuild = 5;
//patents
// https://patents.google.com/patent/US5559492 - simplex
// https://patents.google.com/patent/US6281789 - simplex smartsync
// https://patents.google.com/patent/US6194994 - wheelock
// https://patents.google.com/patent/US5659287A/en - gentex strobe sync
// https://patents.google.com/patent/US5959528 - gentex smartsync
// https://patents.google.com/patent/US8928191B2/en - potter weird universal sync
//----------------------------------------------------------------------------- RUNTIME VARIABLES
bool fullAlarm = false; //bool to control if this is a full alarm that requres a panel reset to clear
bool silenced = false;
bool panelUnlocked = false; //if the control panel has a key inserted
bool horn = false; //bool to control if the horns are active
bool strobe = false; //bool to control if the strobes are active
bool trouble = false; //bool to control if the panel is in trouble
bool troubleAcked = false; //bool to control if the trouble is acknowledged
bool inConfigMenu = false; //determine if the control panel is in the configuration menu
//---------------------------- Variables that are *always* true if a button is held down at all
bool resetPressed = false;
bool silencePressed = false;
bool drillPressed = false;
//----------------------------
//---------------------------- Variables that are false the *first* iteration through, caused by the variables above being true, this is handy for detecting single button presses, and not repeating code every loop
bool resetStillPressed = false;
bool silenceStillPressed = false;
bool drillStillPressed = false;
//----------------------------
bool runVerification = false; //panel receieved 0 from pull station ciruit and is now investigating
bool walkTest = false; //is the system in walk test
bool silentWalkTest = false;
bool backlightOn = true;
bool keyRequiredVisual; //this variable makes it so the user can change the security settings, exit, return, and the setting they changed persists, but it won't apply until panel restart
bool keylessSilence = false; //can the panel be silenced without a key
bool debug = false;
bool updateLockStatus = false; //if the screen needs to be updated for the lock/unlock icon
bool secondStage = false; //if the panel is in second stage
int characters[] = {32,45,46,47,48,49,50,51,52,53,54,55,56,57,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}; //characters allowed on name
int panelNameList[16];
int clearTimer = 0; //timer for keeping track of button holding for clearing character in the name editor
int verificationTimer = 0; //number to keep track of ms for verification
int drillTimer = 0; //number to keep track of ms for drill
int buttonCheckTimer = 0; //add a slight delay to avoid double-clicking buttons
int keyCheckTimer = 0;
int troubleTimer = 0; //ms for trouble
int codeWheelTimer = 0; //code wheel timing variable
int troubleLedTimer = 0; //number to keep track of ms for trouble light
int alarmLedTimer = 0; //alarm led timer
int troubleType = 0; //trouble type 0 - general, 1 - eol resistor
int lcdTimeoutTimer = 0; //timer to keep track of how long until lcd off
int currentScreen = -1; //update display if previous screen is not the same
int configPage = 0; //config page for the config menu
int cursorPosition = 0; //which menu item the cursor is over
int zone1Count = 0; //walk test variables
int zone2Count = 0;
int zoneAlarm = 0; //which zone is in alarm 0 - none | 1 - zone 1 | 2 - zone 2 | 3 - zone 1 & 2 | 4 - drill
int zoneTrouble = 0; //which zone is in trouble 0 - none | 1 - zone 1 | 2 - zone 2 | 3 - zone 1 & 2
int powerOnMinutes = 0; //amount of hours the panel has been powered on for, kinda like an odometer for the FACP
int powerOnMinutesCounter = 0; //keep track of when to iterate the EEPROM value for uptime minutes
String configTop; //configuration menu strings for lcd
String configBottom;
String currentConfigTop; //configuration menu strings for current lcd display
String currentConfigBottom;
bool keyRequired = false; //determine if key switch is required to operate buttons
bool verificationEnabled = true; //is verification turned on
bool eolResistor = true; //is the EOL resistor enabled
bool preAlarm = false; //use pre-alarm?
bool smokeDetectorVerification = false; //should smoke detectors activate first stage
bool smokeDetectorCurrentlyInVerification = false; //Is a smoke detector currently in verification?
bool audibleSilence = true;
bool useTwoWire = false; //does the panel use 2 wire for alarms
int twoWireTimer = 0; //timer for 2 wire alarms
int smokeDetectorTimeout = 60000; //how long to wait before toggling smoke detectors back on
int smokeDetectorPostRestartTimer = 0; //variable to keep track of the 60 seconds post-power up that the panel watches the smoke detector
int smokeDetectorPostRestartVerificationTime = 120000; //time in ms that the smoke detector should be monitored
int smokeDetectorTimer = 0; //timer to keep track of the current smoke detector timeout progress
int firstStageTime = 300000; //time in minutes that first stage should last
int firstStageTimer = 0; //timer to keep track of current first stage
int codeWheel = 0; //which alarm pattern to use, code-3 default
int strobeSync = 0; //strobe sync is not on by default 0 - none | 1 - System Sensor | 2 - Wheelock | 3 - Gentex | 4 - Simplex
int strobeSyncTimer = 0; //strobe sync timer
float verificationTime = 2500;
int panelHomescreen = 0;
int lcdTimeout = 0;
String panelName = "";
LiquidCrystal_I2C lcd(0x27,16,2);
//---------------------------------------- CUSTOM LCD CHARACTERS
byte lock[] = { //lock icon
B01110,
B10001,
B10001,
B11111,
B11011,
B11011,
B11111,
B00000
};
byte check[] = { //check mark icon
B00000,
B00001,
B00011,
B10110,
B11100,
B01000,
B00000,
B00000
};
byte cross[] = { //x mark
B00000,
B11011,
B01110,
B00100,
B01110,
B11011,
B00000,
B00000
};
//---------------------------------------- CUSTOM LCD CHARACTERS
//PIN DEFINITIONS
int zone1Pin = 15;
int zone2Pin = 39; //IF YOU WANT A SINGLE ZONE, SET THIS TO 15 AS WELL, ELSE 39.
int hornRelay = 13;
int buzzerPin = 4;
int strobeRelay = 18;
int smokeDetectorRelay = 14;
int readyLed = 27;
int silenceLed = 26;
int alarmLed = 25;
int keySwitchPin = 33;
int resetButtonPin = 32;
int silenceButtonPin = 35;
int drillButtonPin = 34;
int sclPin = 22;
int sdaPin = 21;
//----------------------------------------------------------------------------- RUNTIME VARIABLES
//----------------------------------------------------------------------------- EEPROM RESET
void resetEEPROM() {
for (int i=0; i<=512; i++){ //write all 255's from 0-1024 address
EEPROM.write(i,255);
}
Serial.println("Erased EEPROM");
EEPROM.write(0,76);EEPROM.write(1,101);EEPROM.write(2,120);EEPROM.write(3,122);EEPROM.write(4,97);EEPROM.write(5,99);EEPROM.write(6,104); //write Lexzach to addresses 0-6
Serial.println("Wrote LEXZACH header");
EEPROM.write(7,0); //code-3 default
Serial.println("Configured Code-3");
EEPROM.write(8,0); //key not required by default
Serial.println("Disabled key");
EEPROM.write(9,1); //verification is turned on by default
Serial.println("Turned on verification");
EEPROM.write(10,25); //default verification time of 2.5 seconds
Serial.println("Set verification time of 2.5 seconds");
//system name "ANTIGNEOUS"
EEPROM.write(11,65);EEPROM.write(12,78);EEPROM.write(13,84);EEPROM.write(14,73);EEPROM.write(15,71);EEPROM.write(16,78);EEPROM.write(17,69);EEPROM.write(18,79);EEPROM.write(19,85);EEPROM.write(20,83);
Serial.println("Wrote system name 'ANTIGNEOUS'");
for (int i=21; i<=26; i++){ //write all 0's from 21-26 address
EEPROM.write(i,32);
}
Serial.println("Wrote 6 blank spaces");
EEPROM.write(27,0); //write keyless silence
Serial.println("Disabled keyless silence");
EEPROM.write(28,24); //post restart smoke detector verification time
Serial.println("Set smoke detector verification to 120 seconds");
EEPROM.write(29,0); //strobe sync
Serial.println("Disable strobe sync by default");
EEPROM.write(30,0); //2 wire
Serial.println("Disabled 2 wire alarms by default");
EEPROM.write(50, EEPROMVersion); //write current version and build
EEPROM.write(51, EEPROMBuild);
Serial.println("Wrote EEPROM version and build");
//EEPROM.write(72,125); //EOL lenience 500 by default (take the value stored and multiply by 4 to get actual value)
//Serial.println("Set EOL lenience to 500");
EEPROM.write(73,1); //EOL resistor is enabled by default
Serial.println("Enabled EOL resistor");
EEPROM.write(74,0); //pre-alarm disabled by default
Serial.println("Disabled pre-alarm");
EEPROM.write(75,5); //pre-alarm first-stage is 1 minute by default
Serial.println("Set pre-alarm first stage to 5 minutes");
EEPROM.write(76,0); //smoke detector verification is disable by default
Serial.println("Disabled smoke detector verification");
EEPROM.write(77,12); //smoke detector verification is 1 minute by default
Serial.println("Set smoke detector verification to 1 minute");
EEPROM.write(78,0); //homescreen is panel name by default
Serial.println("Set panel name as homescreen");
EEPROM.write(79,1); //audible silence is enabled by default
Serial.println("Enabled audible silence");
EEPROM.write(80,0); //lcd timeout is disabled by default (time in MS found by taking value and multiplying it by 15000)
Serial.println("Disabled LCD timeout");
EEPROM.write(81,0);
EEPROM.write(82,0);
EEPROM.write(83,0);
EEPROM.write(84,0);
Serial.println("Set runtime to 0 minutes");
EEPROM.commit();
Serial.println("Wrote changes to EEPROM");
}
//----------------------------------------------------------------------------- EEPROM RESET
void setup() {
EEPROM.begin(512); //allocate memory address 0-1024 for EEPROM
Serial.println("Configured EEPROM for addresses 0-512");
Serial.begin(115200); //begin serial
Serial.println("Lexzach's Low-Cost FACP");
Serial.println("Booting...");
//----------------------------------------------------------------------------- SETUP PINS
pinMode(resetButtonPin, INPUT); //reset switch
pinMode(hornRelay, OUTPUT); //horn
pinMode(strobeRelay, OUTPUT); //strobe
pinMode(smokeDetectorRelay, OUTPUT); //smoke relay
pinMode(readyLed, OUTPUT); //ready LED
pinMode(silenceLed, OUTPUT); //silence LED
pinMode(alarmLed, OUTPUT); //alarm LED
pinMode(keySwitchPin, INPUT); //key switch
pinMode(silenceButtonPin, INPUT); //silence switch
pinMode(drillButtonPin, INPUT); //drill switch
if (digitalRead(resetButtonPin) and not digitalRead(silenceButtonPin) and not digitalRead(drillButtonPin)){
failsafeMode = true;
}
pinMode(zone1Pin, INPUT); //zone 1
pinMode(zone2Pin, INPUT); //zone 2
pinMode(buzzerPin, OUTPUT); //buzzer
if (not invertRelay){
digitalWrite(hornRelay, HIGH); //horn
digitalWrite(strobeRelay, HIGH); //strobe
digitalWrite(smokeDetectorRelay, HIGH); //smoke relay
} else {
digitalWrite(hornRelay, LOW); //horn
digitalWrite(strobeRelay, LOW); //strobe
digitalWrite(smokeDetectorRelay, LOW); //smoke relay
}
pinMode(sclPin, OUTPUT); //scl
pinMode(sdaPin, OUTPUT); //sda
//----------------------------------------------------------------------------- SETUP PINS
Serial.println("Initializing LCD...");
lcd.init(); //initialize LCD
lcd.createChar(0, lock); //create the lock character
lcd.createChar(1, check); //create the lock character
lcd.createChar(2, cross); //create the lock character
lcd.backlight();
//----------------------------------------------------------------------------- EEPROM RESET BUTTONS
if (digitalRead(resetButtonPin) and digitalRead(silenceButtonPin) and digitalRead(drillButtonPin) and not failsafeMode){ //reset EEPROM if all buttons are pressed
for(int i=5; i!=0; i--){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TO FACTORY RESET");
lcd.setCursor(0,1);
lcd.print((String)"HOLD BUTTONS - "+i);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
if (digitalRead(resetButtonPin) and digitalRead(silenceButtonPin) and digitalRead(drillButtonPin)){
lcd.print("RESETTING TO");
lcd.setCursor(0,1);
lcd.print("FACTORY SETTINGS");
resetEEPROM();
} else {
lcd.print("FACTORY RESET");
lcd.setCursor(0,1);
lcd.print("CANCELLED");
}
delay(3000);
ESP.restart();
}
//----------------------------------------------------------------------------- EEPROM RESET BUTTONS
// if (digitalRead(silenceButtonPin)==HIGH){ //debug code
// debug = true;
// }
lcd.clear();
lcd.setCursor(4,0);
lcd.print("BOOTING...");
lcd.setCursor(0,1);
lcd.print("IO");
Serial.println("Configured all pins");
//EEPROM.write(0,255); //-------------------------------------------------------UNCOMMENT TO INVALIDATE EEPROM AND REFLASH IT AFTER EVERY RESTART
//EEPROM.commit();
Serial.println("Verifying EEPROM configuration integrity...");
//----------------------------------------------------------------------------- EEPROM INTEGRETY
if ((EEPROM.read(0) != 76 or EEPROM.read(6) != 104 or EEPROM.read(7) > 5 or EEPROM.read(8) > 1 or EEPROM.read(50) != EEPROMVersion or EEPROM.read(51) != EEPROMBuild) and not failsafeMode){ //completely skip eeprom verification if booting into failsafe
Serial.println("EEPROM verification failed.");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ERROR 1");
lcd.setCursor(0,1);
lcd.print("check manual");
while(not digitalRead(resetButtonPin) and not digitalRead(drillButtonPin)){ //wait until button is pressed
delay(1);
}
if(digitalRead(resetButtonPin)){
failsafeMode = true; // ----- ENTER FAILSAFE MODE
} else if (digitalRead(drillButtonPin)){
resetEEPROM();
}
} else {
Serial.println("EEPROM integrity verified");
}
//----------------------------------------------------------------------------- EEPROM INTEGRETY
if (not failsafeMode){ //continue loading if failsafe mode is not enabled
lcd.clear();
lcd.setCursor(4,0);
lcd.print("BOOTING...");
lcd.setCursor(0,1);
lcd.print("IO-SLFTST");
Serial.println("Current EEPROM dump:"); //dump EEPROM into serial monitor
for (int i=0; i<=511; i++){
Serial.print(i);
Serial.print(":");
Serial.print(EEPROM.read(i));
Serial.print(" ");
}
Serial.println();
//CONFIG LOADING ROUTINE
Serial.println("Loading config from EEPROM...");
codeWheel = EEPROM.read(7); //codeWheel setting address
//----------------------------- Panel security variables
keyRequired = EEPROM.read(8) == 1 ? true : false;
keyRequiredVisual = keyRequired ? true : false;
//----------------------------- Panel security variables
verificationEnabled = EEPROM.read(9) == 1 ? true : false;
eolResistor = EEPROM.read(73) == 1 ? true : false;
preAlarm = EEPROM.read(74) == 1 ? true : false;
smokeDetectorVerification = EEPROM.read(76) == 1 ? true : false;
audibleSilence = EEPROM.read(79) == 1 ? true : false;
keylessSilence = EEPROM.read(27) == 1 ? true : false;
useTwoWire = EEPROM.read(30) == 1 ? true : false;
smokeDetectorTimeout = EEPROM.read(77)*5000;
smokeDetectorPostRestartVerificationTime = EEPROM.read(28)*5000;
firstStageTime = EEPROM.read(75)*60000;
verificationTime = EEPROM.read(10)*100;
//resistorLenience = EEPROM.read(72)*4; DEPRECATED
panelHomescreen = EEPROM.read(78);
lcdTimeout = EEPROM.read(80)*15000;
strobeSync = EEPROM.read(29);
byte byte_array[4];
for (int i = 0; i < 4; i++) {
byte_array[i] = EEPROM.read(81+i); //read each byte from consecutive addresses
powerOnMinutes = powerOnMinutes | ((unsigned long)byte_array[i] << (i * 8)); //reconstruct the number from the bytes
}
int x=0;
for (int i=11; i<=26; i++){ //read panel name
if (EEPROM.read(i) != 0){
panelName = panelName + (char)EEPROM.read(i);
panelNameList[x] = EEPROM.read(i);
x++;
}
}
lcd.clear();
lcd.setCursor(4,0);
lcd.print("BOOTING...");
lcd.setCursor(0,1);
lcd.print("IO-SLFTST-CONFIG");
delay(100);
Serial.println("Config loaded");
digitalWrite(readyLed, HIGH); //power on ready LED on startup
updateLockStatus = true;
panelUnlocked = (digitalRead(keySwitchPin) and keyRequired) ? true : false; //check the key status on startup
digitalWrite(silenceLed, LOW);
digitalWrite(alarmLed, LOW);
if (invertRelay){
digitalWrite(smokeDetectorRelay, HIGH); //turn on smoke relay
} else {
digitalWrite(smokeDetectorRelay, LOW); //turn on smoke relay
}
Serial.println("-=- STARTUP COMPLETE -=-");
} else {
Serial.println("-=- ENTERING FAILSAFE MODE -=-");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTERING");
lcd.setCursor(0,1);
lcd.print("FAILSAFE...");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("FAILSAFE MODE");
lcd.setCursor(0,1);
lcd.print("SYSTEM NORMAL");
digitalWrite(readyLed, HIGH);
if (invertRelay){
digitalWrite(smokeDetectorRelay, HIGH); //turn on smoke relay
} else {
digitalWrite(smokeDetectorRelay, LOW); //turn on smoke relay
}
}
lastPulse = millis(); //start last pulse
}
//---------------------------------------------------------------------------------------- functions for turning certain things on and off
void tone() {
ledcSetup(0, 5000, 8); // setup beeper
ledcAttachPin(buzzerPin, 0); // attach beeper
ledcWriteTone(0, 1500); // play tone
}
void noTone() {
ledcSetup(0, 5000, 8); // setup beeper
ledcAttachPin(buzzerPin, 0); // attach beeper
ledcWriteTone(0, 0); // stop tone
}
void hornOn(bool on){
digitalWrite(hornRelay, (invertRelay ^ on) ? LOW : HIGH);
}
void strobeOn(bool on){
digitalWrite(strobeRelay, (invertRelay ^ on) ? LOW : HIGH);
}
void smokeDetectorOn(bool on){
digitalWrite(smokeDetectorRelay, (invertRelay ^ on) ? LOW : HIGH);
}
//---------------------------------------------------------------------------------------- functions for turning certain things on and off
void activateNAC(){
horn = true;
strobe = true;
fullAlarm = true;
silenced = false;
inConfigMenu = false;
codeWheelTimer = 0;
strobeSyncTimer = 0;
secondStage = (zoneAlarm == 4 or not preAlarm) ? true : false; //entirely skip first stage if it is a drill or if prealarm is turned off
tone();
digitalWrite(readyLed, HIGH);
digitalWrite(alarmLed, HIGH);
digitalWrite(silenceLed, LOW);
}
void checkKey(){
if (digitalRead(keySwitchPin)){
if (not panelUnlocked and keyRequired){
panelUnlocked = true;
updateLockStatus = true;
}
} else {
if (panelUnlocked and keyRequired){
panelUnlocked = false;
drillPressed=false;
silencePressed=false; //make sure all buttons are registered as depressed after key is removed
resetPressed=false;
drillStillPressed=false;
resetStillPressed=false;
silenceStillPressed=false;
drillTimer = 0;
updateLockStatus = true;
}
}
}
//----------------------------------------------------------------------------- CHECK ACTIVATION DEVICES [!THIS CODE MUST WORK!]
void checkDevices(){
if (not walkTest){
if (not runVerification and not fullAlarm and (analogRead(zone1Pin) == 0 or analogRead(zone2Pin) == 0)){ //reading a single zero flags as a possible alarm
runVerification = true;
}
if (runVerification and not fullAlarm){ //only execute if the panel flags a possible alarm and there isn't currently a full alarm
if (verificationTimer >= verificationTime or not verificationEnabled){ //execute the following code if verification surpasses the configured verification time OR verification is disabled
if (analogRead(zone1Pin) == 0 or analogRead(zone2Pin) == 0){ //check once again if any zeros are read
if (not smokeDetectorVerification or smokeDetectorCurrentlyInVerification){ //if smoke detector verification is disabled, or a smoke detector has tripped *after* verification, activate NACs
if (analogRead(zone1Pin) == 0 and analogRead(zone2Pin) == 0){ //read the pins once more to check which zone is in alarm
zoneAlarm = 3; //both
} else if (analogRead(zone2Pin) == 0){
zoneAlarm = 2; //z2
} else {
zoneAlarm = 1; //z1
}
activateNAC();
runVerification = false; //dismiss the possible alarm after activating the NACs
verificationTimer = 0;
} else if (smokeDetectorVerification and not smokeDetectorCurrentlyInVerification){ //if smoke detector verifcaion is turned on, run this instead
smokeDetectorOn(false); //turn off the smoke detector relay
delay(100); //wait for 100 ms
if (analogRead(zone1Pin) == 0 or analogRead(zone2Pin) == 0){ // if the alarm signal persists after turning off the smoke detectors, it is a pull station, activate the nacs
if (analogRead(zone1Pin) == 0 and analogRead(zone2Pin) == 0){
zoneAlarm = 3; //both
} else if (analogRead(zone2Pin) == 0){
zoneAlarm = 2; //z2
} else {
zoneAlarm = 1; //z1
}
activateNAC();
smokeDetectorOn(true); //re-enable the smoke detector relay after determining that a pull station was pulled.
runVerification = false;
verificationTimer = 0;
} else { //if the signal *does not* persists after disabling the smoke detector relay, it was a smoke detector, run verification.
smokeDetectorPostRestartTimer = 0;
smokeDetectorCurrentlyInVerification = true; //tell the smokeDetector() function to run code
smokeDetectorTimer = 0; //reset the smoke detector timer
currentScreen = -1; //update the screen to allow the displaying of smoke detector verification
digitalWrite(alarmLed, HIGH); //LED indicator
runVerification = false;
verificationTimer = 0;
}
}
} else { //if no zeros are read after verification, dismiss possible alarm
runVerification = false;
verificationTimer = 0;
}
} else {
verificationTimer++;
}
}
} else if (walkTest){
if (analogRead(zone1Pin) == 0 or analogRead(zone2Pin) == 0){// or analogRead(zone2Pin) == 0){
if (analogRead(zone1Pin) == 0){
zone1Count++;
smokeDetectorOn(false);
strobeOn(true);
delay(500);
digitalWrite(alarmLed, HIGH);
if (not silentWalkTest){
hornOn(true);
delay(500);
hornOn(false);
} else {
delay(500);
}
delay(3000);
digitalWrite(alarmLed, LOW);
strobeOn(false);
delay(4000);
smokeDetectorOn(true);
} else if (analogRead(zone2Pin) == 0){
zone2Count++;
smokeDetectorOn(false);
strobeOn(true);
delay(500);
digitalWrite(alarmLed, HIGH);
if (not silentWalkTest){
hornOn(true);
delay(500);
hornOn(false);
delay(500);
hornOn(true);
delay(500);
hornOn(false);
} else {
delay(1500);
}
delay(3000);
digitalWrite(alarmLed, LOW);
strobeOn(false);
delay(4000);
smokeDetectorOn(true);
}
currentScreen = -1;
}
}
if ((analogRead(zone1Pin) == 4095 or analogRead(zone2Pin) == 4095) and eolResistor) {
if (troubleTimer >= 1000){
trouble = true;
troubleType=1;
if (analogRead(zone1Pin) == 4095 and analogRead(zone2Pin) == 4095){
zoneTrouble = 3;
} else if (analogRead(zone1Pin) == 4095){
zoneTrouble = 1;
} else {
zoneTrouble = 2;
}
} else {
troubleTimer++;
}
} else {
troubleTimer = 0;
}
}
//----------------------------------------------------------------------------- CHECK ACTIVATION DEVICES [!THIS CODE MUST WORK!]
//----------------------------------------------------------------------------- TROUBLE RESPONSE
void troubleCheck(){
if (trouble and not fullAlarm and not walkTest){
if (troubleLedTimer >= 200){
if (digitalRead(readyLed)){
digitalWrite(readyLed, LOW);
if (not troubleAcked){
noTone();
}
} else {
digitalWrite(readyLed, HIGH);
if (not troubleAcked){
tone();
}
}
troubleLedTimer = 0;
} else {
troubleLedTimer++;
}
} else {
troubleLedTimer=0;
zoneTrouble=0;
}
}
//----------------------------------------------------------------------------- TROUBLE RESPONSE
//----------------------------------------------------------------------------- RESET CODE
void reboot(){
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Resetting...");
tone();
digitalWrite(readyLed, HIGH); //ready LED
digitalWrite(silenceLed, HIGH); //silence LED
digitalWrite(alarmLed, HIGH); //alarm LED
hornOn(false); //horn
strobeOn(false); //strobe
smokeDetectorOn(false); //smoke relay
lcd.backlight();
delay(2500);
noTone();
digitalWrite(readyLed, LOW); //ready LED
digitalWrite(silenceLed, LOW); //silence LED
digitalWrite(alarmLed, LOW); //alarm LED
ESP.restart();
}
//----------------------------------------------------------------------------- RESET CODE
//----------------------------------------------------------------------------- BUTTON CHECK
void checkButtons(){
if (digitalRead(resetButtonPin)){ //-----------------------------------------------------------RESET BUTTON
reboot();
}
if (digitalRead(silenceButtonPin)){ //---------------------------------------------------------SILENCE BUTTON
if (horn){ //if horns are not silenced, silence the horns
digitalWrite(silenceLed, HIGH);
digitalWrite(alarmLed, LOW);
digitalWrite(readyLed, LOW);
horn = false;
if (not audibleSilence){
strobe = false;
}
silenced=true;
noTone();
} else if (not fullAlarm and trouble and not silencePressed and not troubleAcked){
troubleAcked = true;
noTone();
} else if (not fullAlarm and not silencePressed and not inConfigMenu){
inConfigMenu = true;
resetStillPressed = true; //make sure the menu doesn't close out as soon as someone opens it
silenceStillPressed = true;
drillStillPressed = true;
char *main[] = {"Testing","Settings"}; //menu 0
configTop = (String)main[0];
configBottom = (String)main[1];
configPage = 0;
cursorPosition = 0;
currentConfigBottom = "";
currentConfigTop = "";
}
silencePressed = true;
} else {
silencePressed = false;
}
if (digitalRead(drillButtonPin) and not horn and not silenced and not fullAlarm){ //------------------------------------------DRILL BUTTON
if (drillTimer >= 237){
zoneAlarm = 4;
activateNAC();
} else {
drillTimer++;
}
drillPressed = true;
} else {
drillTimer = 0;
drillPressed = false;
}
if (digitalRead(drillButtonPin) and fullAlarm and not secondStage){
secondStage = true;
currentScreen = -1;
}
}
//----------------------------------------------------------------------------- BUTTON CHECK
//----------------------------------------------------------------------------- NAC ACTIVATION
void alarm(){
if (strobe){
if (strobeSync == 0){ //add more strobesyncs in the future
strobeOn(true);
} else {
strobeSyncTimer++;
if (strobeSync == 1 or strobeSync == 4){
if (strobeSyncTimer >= 1000){
strobeSyncTimer=0;
} else if (strobeSyncTimer >= 975){
strobeOn(false);
} else if (strobeSyncTimer <= 25){
strobeOn(true);
}
} else if (strobeSync == 2) {
if (strobeSyncTimer >= 2000){
strobeSyncTimer=0;
} else if (strobeSyncTimer >= 1968){
strobeOn(false);
} else if (strobeSyncTimer <= 32){
strobeOn(true);
}
} else if (strobeSync == 3) {
if (strobeSyncTimer >= 2000){
strobeSyncTimer=0;
} else if (strobeSyncTimer >= 1985){
strobeOn(false);
} else if (strobeSyncTimer <= 15){
strobeOn(true);
}
}
}
}else{
strobeOn(false);
strobeSyncTimer = 0;
}
if (horn and not useTwoWire){
if (not preAlarm or secondStage){ //yes, preAlarm == false is redundant but in the case that second stage == false, but pre-alarm is off, the full alarm will still sound
if (codeWheel == 0){
if (codeWheelTimer == 0){ //---------- temporal code 3
hornOn(true);
} else if (codeWheelTimer == 500) {
hornOn(false);
} else if (codeWheelTimer == 1000) {
hornOn(true);
} else if (codeWheelTimer == 1500) {
hornOn(false);
} else if (codeWheelTimer == 2000) {
hornOn(true);
} else if (codeWheelTimer == 2500) {
hornOn(false);
} else if (codeWheelTimer >= 4000) {
codeWheelTimer = -1;
}
} else if (codeWheel == 1) {
if (codeWheelTimer == 0){ //---------- marchtime
hornOn(true);
} else if (codeWheelTimer == 250){
hornOn(false);
} else if (codeWheelTimer >= 500){
codeWheelTimer = -1;
}
} else if (codeWheel == 2) { //--------- 4-4
if (codeWheelTimer == 0) {
hornOn(true);
} else if (codeWheelTimer == 300) {
hornOn(false);
} else if (codeWheelTimer == 600) {
hornOn(true);
} else if (codeWheelTimer == 900) {
hornOn(false);
} else if (codeWheelTimer == 1200) {
hornOn(true);
} else if (codeWheelTimer == 1500) {
hornOn(false);
} else if (codeWheelTimer == 1800) {
hornOn(true);
} else if (codeWheelTimer == 2100) {
hornOn(false);
} else if (codeWheelTimer == 2850) {
hornOn(true);
} else if (codeWheelTimer == 3150) {
hornOn(false);
} else if (codeWheelTimer == 3450) {
hornOn(true);
} else if (codeWheelTimer == 3750) {
hornOn(false);
} else if (codeWheelTimer == 4050) {
hornOn(true);
} else if (codeWheelTimer == 4350) {
hornOn(false);
} else if (codeWheelTimer == 4650) {
hornOn(true);
} else if (codeWheelTimer == 4950) {
hornOn(false);
} else if (codeWheelTimer >= 14950) {
codeWheelTimer = -1;
}
} else if (codeWheel == 3) { //--------- continuous
hornOn(true);
} else if (codeWheel == 5) {
if (codeWheelTimer == 0){ //---------- marchtime slower
hornOn(true);
} else if (codeWheelTimer == 500){
hornOn(false);
} else if (codeWheelTimer >= 1000){
codeWheelTimer = -1;
}
} else if (codeWheel == 4) {
if (codeWheelTimer == 0){ //---------- california code
hornOn(true);
} else if (codeWheelTimer == 10000){
hornOn(false);
} else if (codeWheelTimer >= 15000){
codeWheelTimer = -1;
}
}
codeWheelTimer++;
} else if (preAlarm and not secondStage){
if (codeWheelTimer == 0){
hornOn(true);
} else if (codeWheelTimer == 75){
hornOn(false);
} else if (codeWheelTimer >= 5075){
codeWheelTimer = -1;
}
codeWheelTimer++;
firstStageTimer++;
if (firstStageTimer>=firstStageTime){
codeWheelTimer = 0;
secondStage = true;
currentScreen = -1;
}
}
} else if (useTwoWire){ //-------------------------------- DO MORE TESTING WITH THIS!!!!
if (horn and (not preAlarm or secondStage)){ //2 wire alarm
if (twoWireTimer >= 1000){
twoWireTimer = 0;
} else if (twoWireTimer >= 975){
hornOn(false);
} else if (twoWireTimer <= 10){
hornOn(true);
}
twoWireTimer++;
} else if (not horn and silenced and audibleSilence){ //2 wire silence
if (twoWireTimer >= 1000){
twoWireTimer = 0;
} else if (twoWireTimer >= 965){
hornOn(false);
} else if (twoWireTimer <= 10){
hornOn(true);
}
twoWireTimer++;
} else if (horn and preAlarm and not secondStage){ //2 wire pre-alarm
if (codeWheelTimer == 0){
hornOn(true);
} else if (codeWheelTimer == 75){
hornOn(false);
} else if (codeWheelTimer >= 5075){
codeWheelTimer = -1;
}
codeWheelTimer++;
firstStageTimer++;
if (firstStageTimer>=firstStageTime){
codeWheelTimer = 0;
secondStage = true;
currentScreen = -1;
}
}
} else {
hornOn(false);
codeWheelTimer = 0;
twoWireTimer = 0;
}
if (fullAlarm){
alarmLedTimer++;
if (alarmLedTimer >= 750){
if (not digitalRead(alarmLed)){
if (silenced){
tone();
}
digitalWrite(alarmLed, HIGH);
} else {
if (silenced){
noTone();
}
digitalWrite(alarmLed, LOW);
}
alarmLedTimer = 0;
}
}
}
//----------------------------------------------------------------------------- NAC ACTIVATION
void lcdUpdate(){
if (not trouble and not fullAlarm and not walkTest and currentScreen != 0 and drillTimer == 0){
lcd.noAutoscroll();
lcd.clear();
lcd.setCursor(2,0);
if (smokeDetectorCurrentlyInVerification){
lcd.print("Smoke Verif.");
} else {
lcd.print("System Normal");
}
lcd.setCursor(0,1);
if (panelHomescreen == 0){
lcd.print(panelName);
} else if (panelHomescreen == 1){
lcd.print(analogRead(zone1Pin));
}
currentScreen = 0;
updateLockStatus = true;
} else if (fullAlarm and not silenced and currentScreen != 3){
lcd.clear();
lcd.setCursor(1,0);
if (secondStage){ //print pre-alarm if it is first stage
lcd.print("* FIRE ALARM *");
} else {
lcd.print("* PRE ALARM *");
}
lcd.setCursor(0,1);
if (zoneAlarm == 1){
lcd.print("Zone 1");
} else if (zoneAlarm == 2){
lcd.print("Zone 2");
} else if (zoneAlarm == 3){
lcd.print("Zone 1 & Zone 2");
} else if (zoneAlarm == 4){
lcd.print("Fire Drill");
}
currentScreen = 3;
updateLockStatus = true;
} else if (silenced and currentScreen != 4){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("-- SILENCED --");
lcd.setCursor(0,1);
if (zoneAlarm == 1){
lcd.print("Zone 1");
} else if (zoneAlarm == 2){
lcd.print("Zone 2");
} else if (zoneAlarm == 3){
lcd.print("Zone 1 & Zone 2");
} else if (zoneAlarm == 4){
lcd.print("Fire Drill");
}
currentScreen = 4;
updateLockStatus = true;
} else if (walkTest and currentScreen != 5) {
lcd.clear();
lcd.setCursor(1,0);
lcd.print("* Supervisory *");
lcd.setCursor(0,1);
lcd.print("Z1:"+(String)zone1Count+" Z2:"+(String)zone2Count);
currentScreen = 5;
updateLockStatus = true;
digitalWrite(readyLed, LOW); //ready led off for walk test
} else if (drillPressed and not fullAlarm and not walkTest and currentScreen != 6) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CONTINUE HOLDING");
lcd.setCursor(1,1);
lcd.print("TO START DRILL");
currentScreen = 6;
} else if (trouble and not fullAlarm and not drillPressed and not walkTest and currentScreen != 1){