Advertisement
Szerelo

Locked box keypad

Apr 27th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. // v 0.1
  2.  
  3. #include <Keypad.h>
  4. #include <EEPROM.h>
  5.  
  6. const byte password_length = 4; // max. 511!
  7. int relay_pin = 13;
  8. int setkey_pin = 12;
  9. int tone_pin = A0;
  10. int time_over = 3000;
  11.  
  12. char password[password_length];
  13. char initial_password[password_length];
  14. int i=0;
  15. long defTime;
  16. char key_pressed=0;
  17. bool f1=false;
  18. const byte rows = 4;
  19. const byte columns = 3;
  20. char Keys[rows][columns] = {
  21. {'1','2','3'},
  22. {'4','5','6'},
  23. {'7','8','9'},
  24. {'*','0','#'}
  25. };
  26.  
  27. byte row_pins[rows] = {5,6,7,8};
  28. byte column_pins[columns] = {2,3,4};  
  29.  
  30. Keypad keypad_key = Keypad( makeKeymap(Keys), row_pins, column_pins, rows, columns);
  31.  
  32. void setup() {
  33.   Serial.begin(115200);
  34.   Serial.println("Start ...");
  35.   if (password_length<2 or password_length>511) {
  36.     Serial.println("Wrong Password length! Terminated program");
  37.     while(1);
  38.   }
  39.   pinMode(tone_pin, OUTPUT);
  40.   pinMode(relay_pin, OUTPUT);
  41.   digitalWrite(relay_pin, LOW);
  42.   pinMode(setkey_pin, INPUT_PULLUP);
  43.   delay(500);
  44.   Serial.println("Enter Password");
  45.   //initialPassword(); // If not remember the password, run this once!
  46.   readPassword();
  47.   tone(tone_pin, 500, 200);
  48.   delay(100);
  49.   tone(tone_pin, 700, 200);
  50.   delay(100);
  51.   tone(tone_pin, 900, 200);
  52. }
  53.  
  54. void loop() {
  55.   if (defTime+time_over<millis() and f1==true) {
  56.     f1=false;
  57.     i=0;
  58.     Serial.println("Time over");
  59.     tone(tone_pin, 400, 200);
  60.     delay(300);
  61.     tone(tone_pin, 400, 200);
  62.     delay(300);
  63.     tone(tone_pin, 400, 200);
  64.     Serial.println("Enter Password");
  65.   }
  66.   key_pressed = keypad_key.getKey();
  67.   if(!digitalRead(setkey_pin)) change();
  68.   if (key_pressed) {
  69.     tone(tone_pin, 1000, 100);
  70.     Serial.println(key_pressed);
  71.     defTime=millis();
  72.     f1=true;
  73.     password[i++]=key_pressed;
  74.   }
  75.   if(i==password_length) {
  76.     f1=false;
  77.     delay(200);
  78.     if(!(strncmp(password, initial_password,4))) {
  79.       Serial.println("Password OK");
  80.       tone(tone_pin, 500, 200);
  81.       delay(100);
  82.       tone(tone_pin, 700, 200);
  83.       delay(100);
  84.       tone(tone_pin, 900, 200);
  85.       delay(100);
  86.       tone(tone_pin, 700, 200);
  87.       delay(100);
  88.       tone(tone_pin, 500, 200);
  89.       digitalWrite(relay_pin, HIGH);
  90.       delay(1000);
  91.       digitalWrite(relay_pin, LOW);
  92.       Serial.println("Enter Password");
  93.       i=0;
  94.     }
  95.     else
  96.     {
  97.       Serial.println("Password Fail");
  98.       tone(tone_pin, 300, 500);
  99.       delay(1000);
  100.       Serial.println("Enter Password");
  101.       i=0;
  102.     }
  103.   }
  104. }
  105.  
  106. void change() {
  107.   Serial.println("Change Password");
  108.   delay(200);
  109.   int j=0;
  110.   while(!digitalRead(setkey_pin)) {
  111.     char key_pressed=keypad_key.getKey();
  112.     if (key_pressed) {
  113.       tone(tone_pin, 1000, 100);
  114.       Serial.println(key_pressed);
  115.       password[i++]=key_pressed;
  116.       if (i==password_length) {
  117.         writePassword();
  118.         i=0;
  119.         Serial.println("Password done.");
  120.         tone(tone_pin, 500, 200);
  121.         delay(100);
  122.         tone(tone_pin, 700, 200);
  123.         delay(100);
  124.         tone(tone_pin, 900, 200);
  125.       }
  126.     }
  127.   }
  128.   Serial.println("While end");
  129. }
  130.  
  131. void initialPassword(){
  132.   for(int j=0;j<password_length;j++) {
  133.     EEPROM.write(j, 1); // The new password many 1 number. e.a. if length=4, the pass=1111.
  134.   }
  135. }
  136.  
  137. void readPassword(){
  138.   for(int j=0;j<password_length;j++) {
  139.     initial_password[j]=EEPROM.read(j);
  140.   }
  141. }
  142.  
  143. void writePassword(){
  144.   for(int j=0;j<password_length;j++) {
  145.     EEPROM.write(j, password[j]);
  146.     initial_password[j]=password[j];
  147.   }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement