SHARE
TWEET

OPEN DOOR WITH KEYPAD ARDUINO

CreadPag Oct 30th, 2016 (edited) 342 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  Password Door Unlock with Arduino uno  */
  2.  
  3. //Libraries
  4. #include <Wire.h>
  5. #include <LCD.h>
  6. #include <Servo.h>
  7. #include <LiquidCrystal_I2C.h>
  8.  
  9. #include <Keypad.h>
  10. /*-------------------------------KEYPAD---------------------------------------*/
  11. const byte numRows= 4; //number of rows on the keypad
  12. const byte numCols= 4; //number of columns on the keypad
  13. char keypressed;
  14. char keymap[numRows][numCols]=
  15. {
  16. {'1', '2', '3', 'A'},
  17. {'4', '5', '6', 'B'},
  18. {'7', '8', '9', 'C'},
  19. {'*', '0', '#', 'D'}
  20. };
  21. //Code that shows the the keypad connections to the arduino terminals
  22. byte rowPins[numRows] = {7, 6, 5, 4};//Rows 0 to 3
  23. byte colPins[numCols] = {3, 2, 1, 0};//Columns 0 to 3            
  24. //initializes an instance of the Keypad class
  25. Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
  26.  
  27. /*-------------------------------CONSTANTS------------------------------------*/
  28. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  29. const int buzzer = A1;        //Buzzer/small speaker
  30.  
  31. const int lock = 9;       //Electric door opener
  32.  
  33.  
  34. /*-------------------------------VARIABLES------------------------------------*/
  35. String password="726127323"; //Variable to store the current password
  36. String tempPassword=""; //Variable to store the input password
  37. int doublecheck;    //Check twice the new passoword
  38. boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
  39. boolean input_pass;   //Variable for input password (correct:true / wrong:false)
  40. boolean storedPassword = true;
  41. boolean changedPassword = false;
  42. boolean checkPassword = false;
  43. int i = 1; //variable to index an array
  44. /*----------------------------------------------------------------------------*/
  45.  
  46. void setup(){
  47.   pinMode(lock,OUTPUT);
  48.   lcd.begin(16, 2); //Setup the LCD's number of columns and rows
  49.   //Print welcome message...
  50.   lcd.setCursor(0,0);
  51.   lcd.print("Arduino Security");
  52.   lcd.setCursor(0,1);
  53.   lcd.print(" electric door  ");
  54.   delay(2000);
  55.   lcd.clear();
  56.   lcd.setCursor(0,0);
  57.   lcd.print(" opener system  ");
  58.   lcd.setCursor(0,1);
  59.   lcd.print(" with password  ");
  60.   delay(2000);
  61.   lcd.clear();
  62. }
  63.  
  64. void loop() { //Main loop
  65.   unlockTheDoor();
  66. }
  67. /********************************FUNCTIONS*************************************/
  68.  
  69. void unlockTheDoor(){
  70.   lockAgain: //goto label
  71.   tempPassword="";
  72.   lcd.clear();
  73.   i=6;
  74.   noTone(buzzer);
  75.   digitalWrite(lock, LOW);
  76.   while(!checkPassword){
  77.     lcd.setCursor(0,0);
  78.     lcd.print("Open the door:  ");
  79.     lcd.setCursor(0,1);
  80.     lcd.print("PASS>");
  81.     keypressed = myKeypad.getKey();   //Read pressed keys
  82.     if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
  83.       if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  84.       keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  85.       keypressed == '8' || keypressed == '9' ){
  86.         tempPassword += keypressed;
  87.         lcd.setCursor(i,1);
  88.         lcd.print("*");       //Put * on lcd
  89.         i++;
  90.         tone(buzzer,800,200);   //Button tone
  91.       }
  92.       else if (keypressed == 'A'){
  93.         changePassword();
  94.         goto lockAgain;
  95.       }
  96.       else if (keypressed=='#'){
  97.         break;
  98.       }
  99.       else if (keypressed == '*'){  //Check for password
  100.         if (password==tempPassword){//If it's correct...
  101.           lcd.clear();      
  102.           lcd.setCursor(0,0);
  103.           lcd.print("Correct password");
  104.           lcd.setCursor(0,1);
  105.           lcd.print("Door is unlocked");
  106.           tone(buzzer,100);    //Play a tone while door is unlocked
  107.           digitalWrite(lock, HIGH);//unlock the door for 5 seconds
  108.           delay(5000);
  109.           goto lockAgain;
  110.         }
  111.         else{           //if it's false, retry
  112.           tempPassword="";
  113.           tone(buzzer,500,200);
  114.           delay(300);
  115.           tone(buzzer,500,200);
  116.           delay(300);
  117.           goto lockAgain;
  118.         }
  119.       }
  120.     }
  121.   }
  122. }
  123.  
  124. //Change current password
  125. void changePassword(){
  126.   retry: //label for goto
  127.   tempPassword="";
  128.   lcd.clear();
  129.   i=1;
  130.   while(!changedPassword){        //Waiting for current password
  131.     keypressed = myKeypad.getKey();   //Read pressed keys
  132.     lcd.setCursor(0,0);
  133.     lcd.print("CURRENT PASSWORD");
  134.     lcd.setCursor(0,1);
  135.     lcd.print(">");
  136.     if (keypressed != NO_KEY){
  137.       if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  138.       keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  139.       keypressed == '8' || keypressed == '9' ){
  140.         tempPassword += keypressed;
  141.         lcd.setCursor(i,1);
  142.         lcd.print("*");
  143.         i++;
  144.         tone(buzzer,800,200);      
  145.       }
  146.       else if (keypressed=='#'){
  147.         break;
  148.       }
  149.       else if (keypressed == '*'){
  150.         i=1;
  151.         if (password==tempPassword){
  152.           storedPassword=false;
  153.           tone(buzzer,500,200);
  154.           newPassword();          //Password is corrent, so call the newPassword function
  155.           break;
  156.         }
  157.         else{               //Try again
  158.           tempPassword="";
  159.           tone(buzzer,500,200);
  160.           delay(300);
  161.           tone(buzzer,500,200);
  162.           delay(300);
  163.           goto retry;
  164.         }
  165.       }
  166.     }
  167.   }
  168. }
  169. String firstpass;
  170. //Setup new password
  171. void newPassword(){
  172.   tempPassword="";
  173.   changedPassword=false;
  174.   lcd.clear();
  175.   i=1;
  176.   while(!storedPassword){
  177.     keypressed = myKeypad.getKey();   //Read pressed keys
  178.     if (doublecheck==0){
  179.       lcd.setCursor(0,0);
  180.       lcd.print("SET NEW PASSWORD");
  181.       lcd.setCursor(0,1);
  182.       lcd.print(">");
  183.     }
  184.     else{
  185.       lcd.setCursor(0,0);
  186.       lcd.print("One more time...");
  187.       lcd.setCursor(0,1);
  188.       lcd.print(">");
  189.     }
  190.     if (keypressed != NO_KEY){
  191.       if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  192.       keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  193.       keypressed == '8' || keypressed == '9' ){
  194.         tempPassword += keypressed;
  195.         lcd.setCursor(i,1);
  196.         lcd.print("*");
  197.         i++;
  198.           tone(buzzer,800,200);
  199.       }
  200.       else if (keypressed=='#'){
  201.         break;
  202.       }
  203.       else if (keypressed == '*'){
  204.         if (doublecheck == 0){
  205.           firstpass=tempPassword;
  206.           doublecheck=1;
  207.           newPassword();
  208.         }
  209.         if (doublecheck==1){
  210.           doublecheck=0;
  211.           if (firstpass==tempPassword){
  212.             i=1;
  213.             firstpass="";
  214.             password = tempPassword; // New password saved
  215.             tempPassword="";//erase temp password
  216.             lcd.setCursor(0,0);
  217.             lcd.print("PASSWORD CHANGED");
  218.             lcd.setCursor(0,1);
  219.             lcd.print("----------------");
  220.               storedPassword=true;
  221.               tone(buzzer,500,400);
  222.               delay(2000);
  223.               lcd.clear();
  224.               break;
  225.           }
  226.           else{
  227.             firstpass="";
  228.             newPassword();
  229.           }
  230.         }
  231.       }
  232.     }
  233.   }
  234. }
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