-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstantKeypressVerify.ino
473 lines (409 loc) · 15.8 KB
/
instantKeypressVerify.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
/*
* HID RED PIN: D12
* HID GREEN PIN: D11
* BEEP PIN: D10
*/
#define MAX_BITS 100 // max number of bits
#define WEIGAND_WAIT_TIME 3000 // time to wait for another weigand pulse.
unsigned char databits[MAX_BITS]; // stores all of the data bits
unsigned char bitCount; // number of bits currently captured
unsigned char flagDone; // goes low when data is currently being captured
unsigned int weigand_counter; // countdown until we assume there are no more bits
unsigned long facilityCode = 0; // decoded facility code
unsigned long cardCode = 0; // decoded card code
int LED_GREEN = 11;
int LED_RED = 12;
int BEEP_BEEP = 10;
// interrupt that happens when INTO goes low (0 bit)
void ISR_INT0()
{
//Serial.print("0"); // uncomment this line to display raw binary
bitCount++;
flagDone = 0;
weigand_counter = WEIGAND_WAIT_TIME;
}
// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
//Serial.print("1"); // uncomment this line to display raw binary
databits[bitCount] = 1;
bitCount++;
flagDone = 0;
weigand_counter = WEIGAND_WAIT_TIME;
}
void setup()
{
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(BEEP_BEEP, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(LED_RED, HIGH); // High = Off
digitalWrite(BEEP_BEEP, HIGH); // High = off
digitalWrite(LED_GREEN, LOW); // Low = On
pinMode(2, INPUT); // DATA0 (INT0)
pinMode(3, INPUT); // DATA1 (INT1)
Serial.begin(9600);
Serial.println("RFID Readers");
// binds the ISR functions to the falling edge of INTO and INT1
attachInterrupt(0, ISR_INT0, FALLING);
attachInterrupt(1, ISR_INT1, FALLING);
weigand_counter = WEIGAND_WAIT_TIME;
//Set Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
digitalWrite(6, HIGH);
}
String keySequence;
bool acceptCard;
bool acceptKey;
void loop()
{
// This waits to make sure that there have been no more data pulses before processing data
if (!flagDone) {
if (--weigand_counter == 0)
flagDone = 1;
}
// if we have bits and we the weigand counter went out
if (bitCount > 0 && flagDone) {
unsigned char i;
Serial.print("Read ");
Serial.print(bitCount);
Serial.print(" bits. ");
if (bitCount == 35) {
// 35 bit HID Corporate 1000 format
// facility code = bits 2 to 14
for (i = 2; i < 14; i++) {
facilityCode <<= 1;
facilityCode |= databits[i];
}
// card code = bits 15 to 34
for (i = 14; i < 34; i++) {
cardCode <<= 1;
cardCode |= databits[i];
}
printBits();
}
else if (bitCount == 26) {
// standard 26 bit format
// facility code = bits 2 to 9
for (i = 1; i < 9; i++) {
facilityCode <<= 1;
facilityCode |= databits[i];
}
// card code = bits 10 to 23
for (i = 9; i < 25; i++) {
cardCode <<= 1;
cardCode |= databits[i];
}
printBits();
}
else if (bitCount == 8) {
Serial.println("Detected Key Press");
String dbits;
//Concat String
for (i = 1; i < 9; i++) {
dbits += databits[i];
// Serial.println(databits[i]);
}
Serial.println("Registered Key: " + dbits);
//Prepare switch statement
enum PossibleBits {
UNDEF,
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE
};
String passwordKey = "12345";
if (dbits == "11000010") {
// do Thing A
Serial.println("Registered Key Press: 1");
keySequence += "1";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "10100100") {
Serial.println("Registered Key Press: 2");
keySequence += "2";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "10000110") {
Serial.println("Registered Key Press: 3");
keySequence += "3";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "01101000") {
Serial.println("Registered Key Press: 4");
keySequence += "4";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "01001010") {
Serial.println("Registered Key Press: 5");
keySequence += "5";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "00101100") {
Serial.println("Registered Key Press: 6");
keySequence += "6";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "00001110") {
Serial.println("Registered Key Press: 7");
keySequence += "7";
}
else if (dbits == "11110000") {
Serial.println("Registered Key Press: 8");
keySequence += "8";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "11010010") {
Serial.println("Registered Key Press: 9");
keySequence += "9";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "11100000") {
Serial.println("Registered Key Press: 0");
keySequence += "0";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else if (dbits == "10110100") {
Serial.println("Registered Key Press: * ");
acceptCard = false;
keySequence = "";
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
}
else if (dbits == "10010110") {
Serial.println("Registered Key Press: # ");
keySequence += "#";
if (keySequence == passwordKey && acceptCard == true) {
//Code is correct
Serial.println("### User Authenticated");
digitalWrite(5, HIGH);
keySequence = "";
digitalWrite(BEEP_BEEP, LOW);
//Make Color Green
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(BEEP_BEEP, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
//Make Color Red
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
acceptCard = false;
}
}
else {
Serial.println("unrecognized pinpad press");
}
}
else {
// you can add other formats if you want!
Serial.println("Unable to decode.");
}
// cleanup and get ready for the next card
bitCount = 0;
facilityCode = 0;
cardCode = 0;
for (i = 0; i < MAX_BITS; i++) {
databits[i] = 0;
}
}
}
void printBits()
{
Serial.print("FC = ");
Serial.print(facilityCode);
Serial.print(", CC = ");
Serial.println(cardCode);
// Now lets play with some LED's for fun:
digitalWrite(LED_GREEN, LOW); // Red
if (cardCode == 00000) {
// If this one "bad" card, turn off green
// so it's just red. Otherwise you get orange-ish
//digitalWrite(LED_GREEN, HIGH);
// digitalWrite(LED_GREEN, LOW); // Red
acceptCard = true;
Serial.println("Card Accepted");
//Will stay yellow until pin is entered or reset (*) Key is pushed.
}
else {
acceptCard = false;
Serial.println("Card not accepted. Reset Vars");
delay(500);
digitalWrite(LED_GREEN, HIGH); // Green Off
digitalWrite(LED_RED, LOW); // Red Back On
}
// delay(5500);
//Yellow to Red
// digitalWrite(LED_GREEN, HIGH); // Green Off
//digitalWrite(LED_RED, LOW); // Red Back On
// Lets be annoying and beep more
}