SHARE
TWEET

Untitled

a guest May 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Keypad.h>
  2. #include <EEPROM.h>
  3. #include <MillisTimer.h>
  4.  
  5. const int codeCount = 200;
  6.  
  7. const long codes[codeCount] = {
  8.   121742, 570057, 454865, 691921, 739337,
  9.   993340, 192616, 248553, 932331, 173237,
  10.   789283, 692624, 868099, 275655, 540115,
  11.   444816, 274250, 909312, 347932, 293922,
  12.   204670, 276628, 138179, 901090, 643902,
  13.   404350, 286797, 913067, 285351, 157827,
  14.   795490, 710769, 490559, 810063, 688858,
  15.   279940, 850744, 468323, 183351, 585987,
  16.   937529, 744012, 192117, 711440, 918672,
  17.   463230, 865791, 248117, 638283, 718205,
  18.   645959, 581391, 758032, 181135, 471078,
  19.   628572, 416256, 309890, 698881, 594526,
  20.   760723, 986936, 123521, 462867, 855180,
  21.   785404, 611169, 648232, 768262, 714771,
  22.   134077, 469744, 560876, 703550, 727746,
  23.   512752, 138971, 131377, 216756, 180110,
  24.   493238, 339901, 620780, 842974, 876858,
  25.   303234, 875713, 661474, 306452, 583064,
  26.   623365, 390181, 427462, 180621, 583977,
  27.   358896, 236311, 483422, 570049, 609373,
  28.   505431, 508082, 322689, 405810, 766603,
  29.   371049, 487536, 596441, 984629, 524026,
  30.   204259, 544115, 317885, 492552, 256569,
  31.   610503, 812489, 434285, 144688, 971698,
  32.   228877, 460702, 364469, 719302, 680521,
  33.   248544, 721698, 980080, 949934, 204237,
  34.   531806, 442270, 593937, 740865, 814095,
  35.   171186, 131298, 943250, 174653, 608953,
  36.   111725, 307685, 906278, 677820, 758250,
  37.   972745, 744637, 945075, 371180, 761118,
  38.   676229, 988714, 184139, 244215, 499136,
  39.   231186, 387118, 779921, 285246, 180981,
  40.   535607, 620732, 758404, 823305, 205202,
  41.   842267, 692563, 338060, 760519, 427690,
  42.   442790, 671927, 476074, 568476, 411565,
  43.   782942, 939596, 816952, 252146, 191105,
  44.   454696, 457314, 841847, 293705, 468145,
  45.   638641, 825723, 463825, 744723, 223055,
  46.   188207, 466845, 630624, 442983, 963963,
  47.   727707, 259783, 721620, 305225, 535862
  48. };
  49.  
  50. const byte ROWS = 4; //four rows
  51. const byte COLS = 4; //three columns
  52. char keys[ROWS][COLS] = {
  53.   {'1', '2', '3', 'A'},
  54.   {'4', '5', '6', 'B'},
  55.   {'7', '8', '9', 'C'},
  56.   {'*', '0', '#', 'D'}
  57. };
  58.  
  59. int loc, pos;
  60.  
  61. String code;
  62.  
  63. byte resetCodesPin = 12;
  64. byte ledPin = 13;
  65. byte unlockPin = 10;
  66.  
  67. byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
  68. byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
  69.  
  70. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  71.  
  72. MillisTimer timer1 = MillisTimer(5000);
  73.  
  74. void resetEnteredCodeInactivity(MillisTimer &mt) {
  75.   Serial.println("Inactivy Timer Triggered");
  76.   code = "";
  77.   timer1.stop();
  78. }
  79.  
  80. void setup() {
  81.   Serial.begin(9600);
  82.   pinMode(ledPin, OUTPUT);              // Sets the digital pin as output.
  83.   pinMode(unlockPin, OUTPUT);              // Sets the digital pin as output.
  84.   pinMode(resetCodesPin, INPUT_PULLUP); // Sets the digital pin as input.
  85.   keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
  86.   digitalWrite(unlockPin, LOW);
  87.   timer1.setInterval(5000);
  88.   timer1.setRepeats(0);
  89.   timer1.expiredHandler(resetEnteredCodeInactivity);
  90. }
  91.  
  92. void loop() {
  93.   timer1.run();
  94.   char key = keypad.getKey();
  95.  
  96.   if (digitalRead(resetCodesPin) == LOW)
  97.   {
  98.     Serial.println("Resetting code statuses");
  99.     for (int i = 0 ; i < EEPROM.length() ; i++) {
  100.     EEPROM.write(i, 0);
  101.   }
  102.  
  103.       while(digitalRead(resetCodesPin) == LOW);
  104.   }
  105.  
  106.   if (key) {
  107.     if (timer1.isRunning()) {
  108.       Serial.println("Resetting Inactivity Timer");
  109.       timer1.reset();
  110.       timer1.start();
  111.     } else {
  112.       Serial.println("Starting Inactivity Timer");
  113.       timer1.start();
  114.     }
  115.     if (key >= '0' && key <= '9' )
  116.       code += key;
  117.  
  118.     Serial.println("Code is " + code + "-" + key);
  119.  
  120.     if (key == 'B' || key == 'A') {
  121.       code = "";
  122.     }
  123.     if (key == '*') {
  124.       Serial.print("Resetting code entered so far");
  125.       code = "";
  126.      
  127.     }
  128.     if (key == 'D') {
  129.       timer1.reset();
  130.       Serial.print("Checking Code ");
  131.       Serial.println(code.c_str());
  132.       for (int x = 0; x < codeCount; x++) {
  133.         Serial.print((String)"Checking " + code + " against " + codes[x] + " index " + x + " ");
  134.         if (atol(code.c_str()) == codes[x]) {
  135.           Serial.println("Code matched");
  136.           // Check if used already
  137.           loc = x / 8;
  138.           pos = x % 8;
  139.  
  140.           int value = EEPROM.read(loc);
  141.  
  142.           int mask = 1 << pos;
  143.  
  144.           Serial.println((String)"Loc:" + loc + " Pos:" + pos + " Mask:" + mask + " Value:" + value + " Logic:" + (value & mask));
  145.  
  146.           if ((value & mask) == mask)
  147.           {
  148.             Serial.println("Code already used");
  149.             code = "";
  150.             Serial.println("Flashing LED");
  151.             for (int y = 0; y < 10; y++) {
  152.               digitalWrite(ledPin, HIGH);
  153.               delay(100);
  154.               digitalWrite(ledPin, LOW);
  155.               delay(100);
  156.             }
  157.             Serial.println("Done Flashing");
  158.             break;
  159.           } else {
  160.             Serial.println("Code Unused and marking used");
  161.             code = "";
  162.             digitalWrite(unlockPin, HIGH);
  163.             delay(1000);
  164.             digitalWrite(unlockPin, LOW);
  165.             EEPROM.write(loc, value | mask);
  166.             break;
  167.           }
  168.         } else {
  169.           Serial.println("Code not found");
  170.         }
  171.       }
  172.       code = "";
  173.     }
  174.   }
  175. }
  176.  
  177.  
  178. void keypadEvent(KeypadEvent key) {
  179.   switch (keypad.getState()) {
  180.     case PRESSED:
  181.       digitalWrite(ledPin, HIGH);
  182.       break;
  183.     case RELEASED:
  184.       digitalWrite(ledPin, LOW);
  185.       break;
  186.   }
  187. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top