Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <EEPROM.h>
- #include <MillisTimer.h>
- const int codeCount = 200;
- const long codes[codeCount] = {
- 121742, 570057, 454865, 691921, 739337,
- 993340, 192616, 248553, 932331, 173237,
- 789283, 692624, 868099, 275655, 540115,
- 444816, 274250, 909312, 347932, 293922,
- 204670, 276628, 138179, 901090, 643902,
- 404350, 286797, 913067, 285351, 157827,
- 795490, 710769, 490559, 810063, 688858,
- 279940, 850744, 468323, 183351, 585987,
- 937529, 744012, 192117, 711440, 918672,
- 463230, 865791, 248117, 638283, 718205,
- 645959, 581391, 758032, 181135, 471078,
- 628572, 416256, 309890, 698881, 594526,
- 760723, 986936, 123521, 462867, 855180,
- 785404, 611169, 648232, 768262, 714771,
- 134077, 469744, 560876, 703550, 727746,
- 512752, 138971, 131377, 216756, 180110,
- 493238, 339901, 620780, 842974, 876858,
- 303234, 875713, 661474, 306452, 583064,
- 623365, 390181, 427462, 180621, 583977,
- 358896, 236311, 483422, 570049, 609373,
- 505431, 508082, 322689, 405810, 766603,
- 371049, 487536, 596441, 984629, 524026,
- 204259, 544115, 317885, 492552, 256569,
- 610503, 812489, 434285, 144688, 971698,
- 228877, 460702, 364469, 719302, 680521,
- 248544, 721698, 980080, 949934, 204237,
- 531806, 442270, 593937, 740865, 814095,
- 171186, 131298, 943250, 174653, 608953,
- 111725, 307685, 906278, 677820, 758250,
- 972745, 744637, 945075, 371180, 761118,
- 676229, 988714, 184139, 244215, 499136,
- 231186, 387118, 779921, 285246, 180981,
- 535607, 620732, 758404, 823305, 205202,
- 842267, 692563, 338060, 760519, 427690,
- 442790, 671927, 476074, 568476, 411565,
- 782942, 939596, 816952, 252146, 191105,
- 454696, 457314, 841847, 293705, 468145,
- 638641, 825723, 463825, 744723, 223055,
- 188207, 466845, 630624, 442983, 963963,
- 727707, 259783, 721620, 305225, 535862
- };
- const byte ROWS = 4; //four rows
- const byte COLS = 4; //three columns
- char keys[ROWS][COLS] = {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- int loc, pos;
- String code;
- byte resetCodesPin = 12;
- byte ledPin = 13;
- byte unlockPin = 10;
- byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- MillisTimer timer1 = MillisTimer(5000);
- void resetEnteredCodeInactivity(MillisTimer &mt) {
- Serial.println("Inactivy Timer Triggered");
- code = "";
- timer1.stop();
- }
- void setup() {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
- pinMode(unlockPin, OUTPUT); // Sets the digital pin as output.
- pinMode(resetCodesPin, INPUT_PULLUP); // Sets the digital pin as input.
- keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
- digitalWrite(unlockPin, LOW);
- timer1.setInterval(5000);
- timer1.setRepeats(0);
- timer1.expiredHandler(resetEnteredCodeInactivity);
- }
- void loop() {
- timer1.run();
- char key = keypad.getKey();
- if (digitalRead(resetCodesPin) == LOW)
- {
- Serial.println("Resetting code statuses");
- for (int i = 0 ; i < EEPROM.length() ; i++) {
- EEPROM.write(i, 0);
- }
- while(digitalRead(resetCodesPin) == LOW);
- }
- if (key) {
- if (timer1.isRunning()) {
- Serial.println("Resetting Inactivity Timer");
- timer1.reset();
- timer1.start();
- } else {
- Serial.println("Starting Inactivity Timer");
- timer1.start();
- }
- if (key >= '0' && key <= '9' )
- code += key;
- Serial.println("Code is " + code + "-" + key);
- if (key == 'B' || key == 'A') {
- code = "";
- }
- if (key == '*') {
- Serial.print("Resetting code entered so far");
- code = "";
- }
- if (key == 'D') {
- timer1.reset();
- Serial.print("Checking Code ");
- Serial.println(code.c_str());
- for (int x = 0; x < codeCount; x++) {
- Serial.print((String)"Checking " + code + " against " + codes[x] + " index " + x + " ");
- if (atol(code.c_str()) == codes[x]) {
- Serial.println("Code matched");
- // Check if used already
- loc = x / 8;
- pos = x % 8;
- int value = EEPROM.read(loc);
- int mask = 1 << pos;
- Serial.println((String)"Loc:" + loc + " Pos:" + pos + " Mask:" + mask + " Value:" + value + " Logic:" + (value & mask));
- if ((value & mask) == mask)
- {
- Serial.println("Code already used");
- code = "";
- Serial.println("Flashing LED");
- for (int y = 0; y < 10; y++) {
- digitalWrite(ledPin, HIGH);
- delay(100);
- digitalWrite(ledPin, LOW);
- delay(100);
- }
- Serial.println("Done Flashing");
- break;
- } else {
- Serial.println("Code Unused and marking used");
- code = "";
- digitalWrite(unlockPin, HIGH);
- delay(1000);
- digitalWrite(unlockPin, LOW);
- EEPROM.write(loc, value | mask);
- break;
- }
- } else {
- Serial.println("Code not found");
- }
- }
- code = "";
- }
- }
- }
- void keypadEvent(KeypadEvent key) {
- switch (keypad.getState()) {
- case PRESSED:
- digitalWrite(ledPin, HIGH);
- break;
- case RELEASED:
- digitalWrite(ledPin, LOW);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement