-
Notifications
You must be signed in to change notification settings - Fork 1
/
Open_Access_Control_v4_std.ino
executable file
·1821 lines (1447 loc) · 61.7 KB
/
Open_Access_Control_v4_std.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
/*
* Open Source RFID Access Controller - v3 Standard Beta hardware
*
* 10/23/2013 Version 1.36
* Last build test with Arduino v1.05
* Arclight - [email protected]
* Danozano - [email protected]
*
* Notice: This is free software and is probably buggy. Use it at
* at your own peril. Use of this software may result in your
* doors being left open, your stuff going missing, or buggery by
* high seas pirates. No warranties are expressed on implied.
* You are warned.
*
*
* For latest downloads, see the Wiki at:
* http://www.accxproducts.com/wiki/index.php?title=Open_Access_4.0
*
* For the SVN repository and alternate download site, see:
* http://code.google.com/p/open-access-control/downloads/list
*
* Latest update puts configuration variables in user.h
* This version supports the new Open Access v4 hardware.
*
*
* This program interfaces the Arduino to RFID, PIN pad and all
* other input devices using the Wiegand-26 Communications
* Protocol. It is recommended that the keypad inputs be
* opto-isolated in case a malicious user shorts out the
* input device.
* Outputs go to a Darlington relay driver array for door hardware/etc control.
* Analog inputs are used for alarm sensor monitoring. These should be
* isolated as well, since many sensors use +12V. Note that resistors of
* different values can be used on each zone to detect shorting of the sensor
* or wiring.
*
* Version 4.x of the hardware implements these features and emulates an
* Arduino Duemilanova.
* The "standard" hardware uses the MC23017 i2c 16-channel I/O expander.
* I/O pins are addressed in two banks, as GPA0..7 and GPB0..7
*
* Relay outpus on digital pins: GPA6, GPA7, GPB0, GPB1
* DS1307 Real Time Clock (I2C): A4 (SDA), A5 (SCL)
* Analog pins (for alarm): A0,A1,A2,A3
* Digital input in (tamper): D9
* Reader 1: D2,D3
* Reader 2: D4,D5
* RS485 TX enable / RX disable: D8
* RS485 RX, TX: D6,D7
* Reader1 LED: GPB2
* Reader1 Buzzer: GPB3
* Reader2 LED: GPB4
* Reader2 Buzzer: GPB5
* Status LED: GPB6
* LCD RS: GPA0
* LCD EN: GPA1
* LCD D4..D7: GPA2..GPA5
* Ethernet/SPI: D10..D1313 (Not used, reserved for the Ethernet shield)
*
* Quickstart tips:
* Set the console password(PRIVPASSWORD) value to a numeric DEC or HEX value.
* Define the static user list by swiping a tag and copying the value received into the #define values shown below
* Compile and upload the code, then log in via serial console at 57600,8,N,1
*
*/
#include "user.h" // User preferences file. Use this to select hardware options, passwords, etc.
#include <Wire.h> // Needed for I2C Connection to the DS1307 date/time chip
#include <EEPROM.h> // Needed for saving to non-voilatile memory on the Arduino.
#include <avr/pgmspace.h> // Allows data to be stored in FLASH instead of RAM
#include <DS1307.h> // DS1307 RTC Clock/Date/Time chip library
#include <WIEGAND26.h> // Wiegand 26 reader format libary
#ifdef MCU328
#include <PCATTACH.h> // Pcint.h implementation, allows for >2 software interupts.
#endif
#ifdef MCPIOXP
#include <Adafruit_MCP23017.h> // Library for the MCP23017 i2c I/O expander
#endif
#ifdef AT24EEPROM
#include <E24C1024.h> // AT24C i2C EEPOROM library
#define MIN_ADDRESS 0
#define MAX_ADDRESS 4096 // 1x32K device
#endif
#define EEPROM_ALARM 0 // EEPROM address to store alarm triggered state between reboots (0..511)
#define EEPROM_ALARMARMED 1 // EEPROM address to store alarm armed state between reboots
#define EEPROM_ALARMZONES 20 // Starting address to store "normal" analog values for alarm zone sensor reads.
#define EEPROM_FIRSTUSER 24
#define EEPROM_LASTUSER 1024
#define NUMUSERS ((EEPROM_LASTUSER - EEPROM_FIRSTUSER)/5) //Define number of internal users (200 for UNO/Duemillanova)
#ifdef HWV3STD // Use these pinouts for the v3 Standard hardware
#define DOORPIN1 6 // Define the pin for electrified door 1 hardware. (MCP)
#define DOORPIN2 7 // Define the pin for electrified door 2 hardware (MCP)
#define ALARMSTROBEPIN 8 // Define the "non alarm: output pin. Can go to a strobe, small chime, etc. Uses GPB0 (MCP pin 8).
#define ALARMSIRENPIN 9 // Define the alarm siren pin. This should be a LOUD siren for alarm purposes. Uses GPB1 (MCP pin9).
#define READER1GRN 10
#define READER1BUZ 11
#define READER2GRN 12
#define READER2BUZ 13
#define RS485ENA 6 // Arduino Pin D6
#define STATUSLED 14 // MCP pin 14
#define R1ZERO 2
#define R1ONE 3
#define R2ZERO 4
#define R2ONE 5
#endif
uint8_t reader1Pins[]={R1ZERO, R1ONE}; // Reader 1 pin definition
uint8_t reader2Pins[]={R2ZERO, R2ONE}; // Reade 2 pin definition
const uint8_t analogsensorPins[] = {0,1,2,3}; // Alarm Sensors connected to other analog pins
/* Global Boolean values
*
*/
bool door1Locked=true; // Keeps track of whether the doors are supposed to be locked right now
bool door2Locked=true;
boolean doorChime=false; // Keep track of when door chime last activated
boolean doorClosed=false; // Keep track of when door last closed for exit delay
boolean sensor[4]={false}; // Keep track of tripped sensors, do not log again until reset.
/* Global Timers
*
*/
unsigned long door1locktimer=0; // Keep track of when door is supposed to be relocked
unsigned long door2locktimer=0; // after access granted.
unsigned long alarmDelay=0; // Keep track of alarm delay. Used for "delayed activation" or level 2 alarm.
unsigned long alarmSirenTimer=0; // Keep track of how long alarm has gone off
unsigned long consolefailTimer=0; // Console password timer for failed logins
unsigned long sensorDelay[2]={0}; // Used with sensor[] above, but sets a timer for 2 of them. Useful for logging
// motion detector hits for "occupancy check" functions.
#define numUsers (sizeof(superUserList)/sizeof(long)) //User access array size (used in later loops/etc)
#define NUMDOORS (sizeof(doorPin)/sizeof(uint8_t))
#define numAlarmPins (sizeof(analogsensorPins)/sizeof(uint8_t))
//Other global variables
uint8_t second, minute, hour, dayOfWeek, dayOfMonth, month, year; // Global RTC clock variables. Can be set using DS1307.getDate function.
uint8_t alarmActivated = EEPROM.read(EEPROM_ALARM); // Read the last alarm state as saved in eeprom.
uint8_t alarmArmed = EEPROM.read(EEPROM_ALARMARMED); // Alarm level variable (0..5, 0==OFF)
uint8_t consoleFail=0; // Tracks failed console logins for lockout
/* Global values for the Wiegand RFID readers
*
*/
volatile long reader1 = 0; // Reader1 buffer
long reader1dec=0; // Separate value for decoded reader1 values
long reader2dec=0; // Separate value for decoded reader1 values
volatile int reader1Count = 0; // Reader1 received bits counter
volatile long reader2 = 0;
volatile int reader2Count = 0;
int userMask1=0;
int userMask2=0;
boolean keypadGranted=0; // Variable that is set for authenticated users to use keypad after login
//volatile long reader3 = 0; // Uncomment if using a third reader.
//volatile int reader3Count = 0;
unsigned long keypadTime = 0; // Timeout counter for reader with key pad
unsigned long keypadValue=0;
// Serial terminal buffer (needs to be global)
char inString[64]={0}; // Size of command buffer (<=128 for Arduino)
uint8_t inCount=0;
boolean privmodeEnabled = false; // Switch for enabling "priveleged" commands
/* Create an instance of the various C++ libraries we are using.
*/
DS1307 ds1307; // RTC Instance
WIEGAND26 wiegand26; // Wiegand26 (RFID reader serial protocol) library
#ifdef MCPIOXP
Adafruit_MCP23017 mcp;
#endif
#ifdef MCU328
PCATTACH pcattach; // Software interrupt library
#endif
#ifdef LCDBOARD
cLCD lcd;
#endif
/* Set up some strings that will live in flash instead of memory. This saves our precious 2k of
* RAM for something else.
*/
const prog_uchar rebootMessage[] PROGMEM = {"Access Control System rebooted."};
const prog_uchar doorChimeMessage[] PROGMEM = {"Front Door opened."};
const prog_uchar doorslockedMessage[] PROGMEM = {"All Doors relocked"};
const prog_uchar alarmtrainMessage[] PROGMEM = {"Alarm Training performed."};
const prog_uchar privsdeniedMessage[] PROGMEM = {"Access Denied. Priveleged mode is not enabled."};
const prog_uchar privsenabledMessage[] PROGMEM = {"Priveleged mode enabled."};
const prog_uchar privsdisabledMessage[] PROGMEM = {"Priveleged mode disabled."};
const prog_uchar privsAttemptsMessage[] PROGMEM = {"Too many failed attempts. Try again later."};
const prog_uchar consolehelpMessage1[] PROGMEM = {"Valid commands are:"};
const prog_uchar consolehelpMessage2[] PROGMEM = {"(d)ate, (s)show user, (m)odify user <num> <usermask> <tagnumber>"};
const prog_uchar consolehelpMessage3[] PROGMEM = {"(a)ll user dump,(r)emove_user <num>,(o)open door <num>"};
const prog_uchar consolehelpMessage4[] PROGMEM = {"(u)nlock all doors,(l)lock all doors"};
const prog_uchar consolehelpMessage5[] PROGMEM = {"(1)disarm_alarm, (2)arm_alarm,(3)train_alarm (9)show_status"};
const prog_uchar consolehelpMessage6[] PROGMEM = {"(t)ime set <sec 0..59> <min 0..59> <hour 0..23> <day of week 1..7>"};
const prog_uchar consolehelpMessage7[] PROGMEM = {" <day 0..31> <mon 0..12> <year 0.99>"};
const prog_uchar consolehelpMessage8[] PROGMEM = {"(e)nable <password> - enable or disable priveleged mode"};
const prog_uchar consolehelpMessage9[] PROGMEM = {"(h)ardware Test <iterations> - Run the hardware test"};
const prog_uchar consoledefaultMessage[] PROGMEM = {"Invalid command. Press '?' for help."};
const prog_uchar statusMessage1[] PROGMEM = {"Alarm armed state (1=armed):"};
const prog_uchar statusMessage2[] PROGMEM = {"Alarm siren state (1=activated):"};
const prog_uchar statusMessage3[] PROGMEM = {"Front door open state (0=closed):"};
const prog_uchar statusMessage4[] PROGMEM = {"Roll up door open state (0=closed):"};
const prog_uchar statusMessage5[] PROGMEM = {"Door 1 unlocked state(1=locked):"};
const prog_uchar statusMessage6[] PROGMEM = {"Door 2 unlocked state(1=locked):"};
void setup(){ // Runs once at Arduino boot-up
Wire.begin(); // start Wire library as I2C-Bus Master
mcp.begin(); // use default address 0
pinMode(2,INPUT); // Initialize the Arduino built-in pins
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
mcp.pinMode(DOORPIN1, OUTPUT);
mcp.pinMode(DOORPIN2, OUTPUT);
pinMode(6,OUTPUT);
for(int i=0; i<=15; i++) // Initialize the I/O expander pins
{
mcp.pinMode(i, OUTPUT);
}
digitalWrite(RS485ENA, HIGH); // Set the RS485 chip to HIGH (not asserted)
/* Attach pin change interrupt service routines from the Wiegand RFID readers
*/
#ifdef MCU328
pcattach.PCattachInterrupt(reader1Pins[0], callReader1Zero, CHANGE);
pcattach.PCattachInterrupt(reader1Pins[1], callReader1One, CHANGE);
pcattach.PCattachInterrupt(reader2Pins[1], callReader2One, CHANGE);
pcattach.PCattachInterrupt(reader2Pins[0], callReader2Zero, CHANGE);
#endif
//Clear and initialize readers
wiegand26.initReaderOne(); //Set up Reader 1 and clear buffers.
wiegand26.initReaderTwo();
mcp.digitalWrite(DOORPIN1, LOW); // Sets the relay outputs to LOW (relays off)
mcp.digitalWrite(DOORPIN2, LOW);
mcp.digitalWrite(ALARMSTROBEPIN, LOW);
mcp.digitalWrite(ALARMSIRENPIN, LOW);
//Initialize LEDs
//ds1307.setDateDs1307(0,57,0,7,21,9,13);
/* Sets the date/time (needed once at commissioning)
uint8_t second, // 0-59
uint8_t minute, // 0-59
uint8_t hour, // 1-23
uint8_t dayOfWeek, // 1-7
uint8_t dayOfMonth, // 1-28/29/30/31
uint8_t month, // 1-12
uint8_t year); // 0-99
*/
Serial.begin(UBAUDRATE); // Set up Serial output at 8,N,1,57600bps
logReboot();
chirpAlarm(1); // Chirp the alarm to show system ready.
#ifdef LCDBOARD
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Open Access ");
lcd.print(VERSION);
delay(500);
lcd.clear();
#endif
//Set up the MCP23017 IO expander and initialize
#ifdef MCPIOXP
mcp.digitalWrite(STATUSLED, LOW); // Turn the status LED green
#endif
}
void loop() // Main branch, runs over and over again
{
readCommand(); // Check for commands entered at serial console
/* Check if doors are supposed to be locked and lock/unlock them
* if needed. Uses global variables that can be set in other functions.
*/
if(((millis() - door1locktimer) >= DOORDELAY) && (door1locktimer !=0))
{
if(door1Locked==true){
doorLock(1);
door1locktimer=0;
}
else {
doorUnlock(1);
door1locktimer=0;
}
}
if(((millis() - door2locktimer) >= DOORDELAY) && (door2locktimer !=0))
{
if(door2Locked==true) {
doorLock(2);
door2locktimer=0;
}
else {
doorUnlock(2);
door2locktimer=0;
}
}
#ifdef LCDBOARD
lcdStatus(1,door1Locked);
lcdStatus(2,door2Locked);
#endif
/* Set optional "failsafe" time to lock up every night.
*/
ds1307.getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // Get the current date/time
if(hour==23 && minute==59 && door1Locked==false){
doorLock(1);
door1Locked==true;
Serial.println("Door 1 locked for 2359 bed time.");
}
// Notes: RFID polling is interrupt driven, just test for the reader1Count value to climb to the bit length of the key
// change reader1Count & reader1 et. al. to arrays for loop handling of multiple reader output events
// later change them for interrupt handling as well!
// currently hardcoded for a single reader unit
/* This code checks a reader with a 26-bit keycard input. Use the second routine for readers with keypads.
* A 5-second window for commands is opened after each successful key access read.
*/
if(reader1Count >= 26){ // When tag presented to reader1 (No keypad on this reader)
reader1dec=decodeCard(reader1); // Format the card data (format can be defined in user.h)
logTagPresent(reader1dec,1); // Write log entry to serial port
reader1=0;
reader1Count=0;
/* Check a user's security level and take action as needed. The
* usermask is a variable from 0..255. By default, 0 and 255 are for
* locked out users or uninitialized records.
* Modify these for each door as needed.
*/
userMask1=checkUser(reader1dec);
if(userMask1>=0) {
switch(userMask1) {
case 0: // No outside privs, do not log denied.
{ // authenticate only.
logAccessGranted(reader1dec, 1);
break;
}
case 20: // Example Limited hours user
{ // Can enter from 5:00pm to 11:00pm
ds1307.getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if((hour >=17) && (hour <=23)){
logAccessGranted(reader1dec, 1); // Log and unlock door 2
alarmState(0);
armAlarm(0); // Deactivate Alarm
// chirpAlarm(1);
door1locktimer=millis();
doorUnlock(1); // Unlock the door.
}
break;
}
case 255: // Locked out user
{
Serial.print("User ");
Serial.print(userMask1,DEC);
Serial.println(" locked out.");
break;
}
default:
{
logAccessGranted(reader1dec, 1); // Log and unlock door 1
alarmState(0);
armAlarm(0); // Deactivate Alarm
door1locktimer=millis();
doorUnlock(1); // Unlock the door.
break;
}
}
}
else
{
if(checkSuperuser(reader1dec) >= 0) { // Check if a superuser, grant access.
logAccessGranted(reader1dec, 1); // Log and unlock door 1
alarmState(0);
armAlarm(0); // Deactivate Alarm
door1locktimer=millis();
doorUnlock(1); // Unlock the door.
}
else{
logAccessDenied(reader1dec,1); // No tickee, no laundree
chirpAlarm(1);
}
}
wiegand26.initReaderOne(); // Reset for next tag scan
}
if(reader2Count >= 26){ // Tag presented to reader 2
reader2dec=decodeCard(reader2); // Format the card data (format can be defined in user.h)
logTagPresent(reader2dec,2); // Write log entry to serial port
reader2=0;
reader2Count=0;
//chirpAlarm(1); // Chirp alarm to show that tag input done
// CHECK TAG IN OUR LIST OF USERS. -1 = no match
keypadGranted=false; // Reset the keypad authorized variable
userMask2=checkUser(reader2dec);
if(userMask2>=0){
switch(userMask2) {
case 0: // No outside privs, do not log denied.
{ // authenticate and log only.
logAccessGranted(reader2dec, 2);
break;
}
case 10: // Authenticating immediately locks up and arms alarm
{ //
logAccessGranted(reader2dec, 2);
runCommand(0x2);
break;
}
case 20: //Limited hours user
{
ds1307.getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if((hour >=17) && (hour <=23)){
logAccessGranted(reader2dec, 2); // Log and unlock door 2
alarmState(0);
armAlarm(0); // Deactivate Alarm
door2locktimer=millis();
doorUnlock(2); // Unlock the door.
keypadGranted=1;
}
break;
}
case 255: // Locked out
{
Serial.print("User ");
Serial.print(userMask2,DEC);
Serial.println(" locked out.");
break;
}
default:
{
logAccessGranted(reader2dec, 2); // Log and unlock door 2
alarmState(0);
armAlarm(0); // Deactivate Alarm
door2locktimer=millis();
doorUnlock(2); // Unlock the door.
keypadGranted=1;
break;
}
}
}
else
{
if(checkSuperuser(reader2dec) >= 0) { // Check if a superuser, grant access.
logAccessGranted(reader2dec, 2); // Log and unlock door 2
alarmState(0);
armAlarm(0); // Deactivate Alarm
chirpAlarm(1);
door1locktimer=millis();
doorUnlock(1); // Unlock the door.
keypadGranted=1;
}
else{
logAccessDenied(reader2dec,2); // no tickee, no laundree
}
}
wiegand26.initReaderTwo(); // Reset for next tag scan
if(READER2KEYPAD == 1) // If Reader2 has a keypad, users can also enter commands
{
unsigned long keypadTime=0; // Timeout counter for reader with key pad
long keypadValue=0;
keypadTime=millis();
if(keypadGranted==1)
{
while((millis() - keypadTime) <=KEYPADTIMEOUT){
// If access granted, open 5 second window for pin pad commands.
if(reader2Count >=4){
if(reader2 !=0xB){ // Pin pad command can be any length, terminated with '#' on the keypad.
if(keypadValue ==0){ // This 0..9, A..F encoding works with many Wiegand-format keypad or reader
keypadValue = reader2; // plus keypad units.
}
else if(keypadValue !=0) {
keypadValue = keypadValue <<4;
keypadValue |= reader2;
}
wiegand26.initReaderTwo(); //Reset reader one and move on.
}
else break;
}
}
logkeypadCommand(2,keypadValue);
runCommand(keypadValue); // Run any commands entered at the keypads.
wiegand26.initReaderTwo();
}
wiegand26.initReaderTwo();
}
} // end of keypad check
/* Check physical sensors with
the logic below. Behavior is based on
the current alarmArmed value.
0=disarmed
1=armed
2=
3=
4=door chime only (Unlock DOOR1, Check zone 0/chirp alarm if active)
Modify the alarm sequence to meet your needs.
*/
switch(alarmArmed) {
case 0:
{
break; // Alarm is not armed, do nothing.
}
case 1: // Alarm is armed
{
if(alarmActivated==0){ // If alarm is armed but not currently alarming, check sensor zones.
if(pollAlarm(0) == 1 ){ // If this zone is tripped, immediately set Alarm State to 2 (alarm delay).
alarmState(2); // Also starts the delay timer
alarmDelay=millis();
if(sensor[0]==false) { // Only log and save if sensor activation is new.
logalarmSensor(0);
EEPROM.write(EEPROM_ALARM,0); // Save the alarm sensor tripped to eeprom
sensor[0]=true; // Set value to not log this again
}
}
if(pollAlarm(1) == 1 ){ // If this zone is tripped, immediately set Alarm State to 1 (alarm immediate).
alarmState(1);
if(sensor[1]==false) { // Only log and save if sensor activation is new.
logalarmSensor(1);
EEPROM.write(EEPROM_ALARM,1); // Save the alarm sensor tripped to eeprom
sensor[1]=true; // Set value to not log this again
}
}
if(pollAlarm(2) == 1 ){ // If this zone is tripped, immediately set Alarm State to 1 (alarm immediate).
alarmState(1);
if(sensor[2]==false) { // Only log and save if sensor activation is new.
logalarmSensor(2);
EEPROM.write(EEPROM_ALARM,2); // Save the alarm sensor tripped to eeprom
sensor[2]=true; // Set value to not log this again
}
}
if(pollAlarm(3) == 1 ){ // If this zone is tripped, immediately set Alarm State to 2 (alarm delay).
alarmState(2); // Also starts the delay timer
alarmDelay=millis();
if(sensor[3]==false) { // Only log and save if sensor activation is new.
logalarmSensor(3);
EEPROM.write(EEPROM_ALARM,3); // Save the alarm sensor tripped to eeprom
sensor[3]=true; // Set value to not log this again
}
}
}
if(alarmActivated==1) { // If alarm is actively going off (siren/strobe) for 10 min (6e5=10min)
if(millis()-alarmSirenTimer >=3.6e6) // Check for alarm interval expired and turn off if needed
{
mcp.digitalWrite(ALARMSIRENPIN,LOW); // Turn on the chime instead
mcp.digitalWrite(ALARMSTROBEPIN,HIGH);
}
}
if(alarmActivated==2) { // If alarm is activated on delay, take this action
if(millis()-alarmDelay >=60000) // Turn on the siren once delay exceeds 60sec.
{
alarmState(1);
}
}
break;
}
case 4:
{ // Door chime mode
if((pollAlarm(3) !=0) && (doorChime==false)) { // Only activate door chime once per opening
chirpAlarm(3);
logChime();
doorChime=true;
}
if(pollAlarm(3) ==0){
doorChime=false; }
break;
}
default:
{
break;
}
}
// Log all motion detector activations regardless of alarm armed state. Useful for "occupancy detection"
if(pollAlarm(0) == 1 ){ // If this zone is tripped, log the action only
// if(sensor[0]==false)
if((millis() - sensorDelay[0]) >=7500) {
logalarmSensor(0);
sensorDelay[0]=millis();
sensor[0]=true; } // Set value to not log this again for 7.5s
}
if(pollAlarm(1) == 1 ){ // If this zone is tripped, log the action only
// if(sensor[1]==false)
if((millis() - sensorDelay[1]) >=7500) {
logalarmSensor(1);
sensorDelay[1]=millis();
sensor[1]=true; // Set value to not log this again for 7.5s
}
}
} // End of loop()
void runCommand(long command) { // Run any commands entered at the pin pad.
switch(command) {
case 0x1:
{ // If command = 1, deactivate alarm
alarmState(0); // Set global alarm level variable
armAlarm(0);
chirpAlarm(1);
break;
}
case 0x2:
{ // If command =2, activate alarm with delay.
doorUnlock(1); // Set global alarm level variable
door1Locked=false;
doorClosed=false; // 200 chirps = ~30 seconds delay
if((pollAlarm(3) == 0) && (pollAlarm(2) == 0)) { // Do not arm the alarm if doors are open
for(uint8_t i=0; i<30; i++) {
if((pollAlarm(3) !=0) && doorClosed==false) { // Set door to be unlocked until alarm timeout or user exits
lockall();
doorClosed=true;
}
mcp.digitalWrite(ALARMSTROBEPIN, HIGH);
delay(500);
mcp.digitalWrite(ALARMSTROBEPIN, LOW);
delay(500);
}
chirpAlarm(2);
armAlarm(1);
lockall(); // Lock all doors on exit
}
else { // Beep the alarm once and exit if attempt made to arm alarm with doors open
mcp.digitalWrite(ALARMSTROBEPIN, HIGH);
delay(500);
mcp.digitalWrite(ALARMSTROBEPIN, LOW);
delay(500);
lockall(); // Lock all doors anyway
}
break;
}
case 0x3:
{
doorLock(1); // Set door 2 to stay unlocked, and door 1 to be locked
doorUnlock(2);
door1Locked=true;
door2Locked=false;
chirpAlarm(3);
break;
}
case 0x4: // Set doors to remain open
{
armAlarm(4);
doorUnlock(1);
doorUnlock(2);
door1Locked=false;
door2Locked=false;
chirpAlarm(4);
break;
}
case 0x5: // Relock all doors
{
lockall();
chirpAlarm(5);
break;
}
case 0x911:
{
chirpAlarm(9); // Emergency
armAlarm(1);
alarmState(1);
break;
}
case 0x20:
{ // If command = 20, do nothing
break;
}
default:
{
break;
}
}
}
/* Alarm System Functions - Modify these as needed for your application.
Sensor zones may be polled with digital or analog pins. Unique reader2
resistors can be used to check more zones from the analog pins.
*/
void alarmState(uint8_t alarmLevel) { //Changes the alarm status based on this flow
logalarmState(alarmLevel);
switch (alarmLevel) {
case 0:
{ // If alarmLevel == 0 turn off alarm.
mcp.digitalWrite(ALARMSIRENPIN, LOW);
mcp.digitalWrite(ALARMSTROBEPIN, LOW);
alarmActivated = alarmLevel; //Set global alarm level variable
break;
}
case 1:
{
mcp.digitalWrite(ALARMSIRENPIN, HIGH); // If alarmLevel == 1 turn on strobe lights and siren
// mcp.digitalWrite(ALARMSTROBEPIN, HIGH); // Optionally activate yoru strobe/chome
alarmSirenTimer=millis();
alarmActivated = alarmLevel; //Set global alarm level variable
logalarmTriggered();
break;
}
case 2:
{
mcp.digitalWrite(ALARMSTROBEPIN, HIGH);
alarmActivated = alarmLevel;
break;
}
case 3:
{
alarmActivated = alarmLevel;
break;
}
/*
case 4: {
vaporize_intruders(STUN);
break;
}
case 5: {
vaporize_intruders(MAIM);
} etc. etc. etc.
break;
*/
default:
{ // Exceptional cases kill alarm outputs
mcp.digitalWrite(ALARMSIRENPIN, LOW); // Turn off siren and strobe
// mcp.digitalWrite(ALARMSTROBEPIN, LOW);
break;
}
}
if(alarmActivated != EEPROM.read(EEPROM_ALARM)){ // Update eeprom value
EEPROM.write(EEPROM_ALARM,alarmActivated);
}
} //End of alarmState()
void chirpAlarm(uint8_t chirps){ // Chirp the siren pin or strobe to indicate events.
for(uint8_t i=0; i<chirps; i++) {
mcp.digitalWrite(ALARMSTROBEPIN, HIGH);
delay(100);
mcp.digitalWrite(ALARMSTROBEPIN, LOW);
delay(200);
}
}
void chirpReader(uint8_t chirps, uint8_t reader){ // Chirp the siren pin or strobe to indicate events.
for(uint8_t i=0; i<chirps; i++) {
if(reader==1)
{
mcp.digitalWrite(READER1BUZ, LOW);
delay(100);
mcp.digitalWrite(READER1BUZ, HIGH);
delay(200);
}
if(reader==2)
{
mcp.digitalWrite(READER2BUZ, LOW);
delay(100);
mcp.digitalWrite(READER2BUZ, HIGH);
delay(200);
}
}
}
uint8_t pollAlarm(uint8_t input){
// Return 1 if sensor shows < pre-defined voltage.
delay(20);
if(abs((analogRead(analogsensorPins[input])/4) - EEPROM.read(EEPROM_ALARMZONES+input)) >SENSORTHRESHOLD){
return 1;
}
else return 0;
}
void trainAlarm(){ // Train the system about the default states of the alarm pins.
armAlarm(0); // Disarm alarm first
alarmState(0);
int temp[5]={0};
int avg;
for(int i=0; i<numAlarmPins; i++) {
for(int j=0; j<5;j++){
temp[j]=analogRead(analogsensorPins[i]);
delay(50); // Give the readings time to settle
}
avg=((temp[0]+temp[1]+temp[2]+temp[3]+temp[4])/20); // Average the results to get best values
Serial.print("Sensor ");
Serial.print(i);
Serial.print(" ");
Serial.print("value:");
Serial.println(avg);
EEPROM.write((EEPROM_ALARMZONES+i),uint8_t(avg)); //Save results to EEPROM
avg=0;
}
logDate();
PROGMEMprintln(alarmtrainMessage);
}
void armAlarm(uint8_t level){ // Arm the alarm and set to level
alarmArmed = level;
logalarmArmed(level);
sensor[0] = false; // Reset the sensor tripped values
sensor[1] = false;
sensor[2] = false;
sensor[3] = false;
if(level != EEPROM.read(EEPROM_ALARMARMED)){
EEPROM.write(EEPROM_ALARMARMED,level);
}
}
/* Access System Functions - Modify these as needed for your application.
These function control lock/unlock and user lookup.
*/
int checkSuperuser(long input){ // Check to see if user is in the user list. If yes, return their index value.
int found=-1;
for(int i=0; i<=numUsers; i++){
if((input == superUserList[i]) && (input !=0xFFFFFFFF) && (input !=0x0)){ //Check if user is in the superuser list AND they are not a default user.
logDate();
Serial.print("Superuser ");
Serial.print(i,DEC);
Serial.println(" found.");
found=i;
return found;
}
}
return found; //If no, return -1
}
void doorUnlock(int input) { //Send an unlock signal to the door and flash the Door LED
uint8_t dp=1;
uint8_t dpLED=1;
if(input == 1)
{
dp=DOORPIN1;