bangnaga

Door Lock Keypad LCD

Aug 23rd, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. /* This code works with 4x4 Keypad Matrix, LCD ic, IRF510N transistor and a push button
  2.  * It's a lock where the transistor drives a solenoid lock you can open either with correct code
  3.  * or by the push button from inside
  4.  * The code can be changed directly by the keypad and doesn't require uploading code again
  5.  * Code is stored in EEPROM
  6.  * Refer to www.surtrtech.com for more details
  7.  */
  8.  
  9. #include <Keypad.h>
  10. #include <EEPROM.h>
  11. #include <LiquidCrystal_I2C.h>
  12.  
  13. #define Solenoid 11             //Actually the Gate of the transistor that controls the solenoid
  14. #define O_Button 10             //Push Button
  15.  
  16. #define I2C_ADDR 0x27           //I2C adress, you should use the code to scan the adress first (0x27) here
  17. #define BACKLIGHT_PIN 3         // Declaring LCD Pins
  18. #define En_pin 2
  19. #define Rw_pin 1
  20. #define Rs_pin 0
  21. #define D4_pin 4
  22. #define D5_pin 5
  23. #define D6_pin 6
  24. #define D7_pin 7
  25.  
  26. const byte numRows= 4;          //number of rows on the keypad
  27. const byte numCols= 4;          //number of columns on the keypad
  28.  
  29. char keymap[numRows][numCols]=
  30. {
  31. {'1', '2', '3', 'A'},
  32. {'4', '5', '6', 'B'},
  33. {'7', '8', '9', 'C'},
  34. {'*', '0', '#', 'D'}
  35. };
  36.  
  37. char keypressed;                 //Where the keys are stored it changes very often
  38. char code[]= {'6','6','0','1'};  //The default code, you can change it or make it a 'n' digits one
  39.  
  40. char code_buff1[sizeof(code)];  //Where the new key is stored
  41. char code_buff2[sizeof(code)];  //Where the new key is stored again so it's compared to the previous one
  42.  
  43. short a=0,i=0,s=0,j=0;          //Variables used later
  44.  
  45. byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3 //if you modify your pins you should modify this too
  46. byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
  47.  
  48. LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
  49. Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
  50.  
  51. void setup()
  52.          {
  53.           lcd.begin (16,2);
  54.           lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  55.           lcd.setBacklight(HIGH); //Lighting backlight
  56.           lcd.home ();
  57.           lcd.print("Standby");      //What's written on the LCD you can change
  58.          
  59.           pinMode(Solenoid,OUTPUT);
  60.           pinMode(O_Button,INPUT);
  61.                      
  62. //          for(i=0 ; i<sizeof(code);i++){        //When you upload the code the first time keep it commented
  63. //            EEPROM.get(i, code[i]);             //Upload the code and change it to store it in the EEPROM
  64. //             }                                  //Then uncomment this for loop and reupload the code (It's done only once)
  65.  
  66.          }
  67.  
  68.  
  69. void loop()
  70. {
  71.  
  72.   keypressed = myKeypad.getKey();               //Constantly waiting for a key to be pressed
  73.     if(keypressed == '*'){                      // * to open the lock
  74.             lcd.clear();
  75.             lcd.setCursor(0,0);
  76.             lcd.print("Enter code");            //Message to show
  77.             GetCode();                          //Getting code function
  78.                   if(a==sizeof(code))           //The GetCode function assign a value to a (it's correct when it has the size of the code array)
  79.                   OpenDoor();                   //Open lock function if code is correct
  80.                   else{
  81.                   lcd.clear();              
  82.                   lcd.print("Wrong");          //Message to print when the code is wrong
  83.                   }
  84.             delay(2000);
  85.             lcd.clear();
  86.             lcd.print("Standby");             //Return to standby mode it's the message do display when waiting
  87.         }
  88.  
  89.      if(keypressed == '#'){                  //To change the code it calls the changecode function
  90.       ChangeCode();
  91.       lcd.clear();
  92.       lcd.print("Standby");                 //When done it returns to standby mode
  93.      }
  94.  
  95.      if(digitalRead(O_Button)==HIGH){      //Opening by the push button
  96.        digitalWrite(Solenoid,HIGH);
  97.        delay(3000);                        //Opens for 3s you can change
  98.        digitalWrite(Solenoid,LOW);
  99.      
  100.      }
  101.          
  102. }
  103.  
  104. void GetCode(){                  //Getting code sequence
  105.        i=0;                      //All variables set to 0
  106.        a=0;
  107.        j=0;
  108.                                      
  109.      while(keypressed != 'A'){                                     //The user press A to confirm the code otherwise he can keep typing
  110.            keypressed = myKeypad.getKey();                        
  111.              if(keypressed != NO_KEY && keypressed != 'A' ){       //If the char typed isn't A and neither "nothing"
  112.               lcd.setCursor(j,1);                                  //This to write "*" on the LCD whenever a key is pressed it's position is controlled by j
  113.               lcd.print("*");
  114.               j++;
  115.             if(keypressed == code[i]&& i<sizeof(code)){            //if the char typed is correct a and i increments to verify the next caracter
  116.                  a++;                                              //Now I think maybe I should have use only a or i ... too lazy to test it -_-'
  117.                  i++;
  118.                  }
  119.             else
  120.                 a--;                                               //if the character typed is wrong a decrements and cannot equal the size of code []
  121.             }
  122.             }
  123.     keypressed = NO_KEY;
  124.  
  125. }
  126.  
  127. void ChangeCode(){                      //Change code sequence
  128.       lcd.clear();
  129.       lcd.print("Changing code");
  130.       delay(1000);
  131.       lcd.clear();
  132.       lcd.print("Enter old code");
  133.       GetCode();                      //verify the old code first so you can change it
  134.      
  135.             if(a==sizeof(code)){      //again verifying the a value
  136.             lcd.clear();
  137.             lcd.print("Changing code");
  138.             GetNewCode1();            //Get the new code
  139.             GetNewCode2();            //Get the new code again to confirm it
  140.             s=0;
  141.               for(i=0 ; i<sizeof(code) ; i++){     //Compare codes in array 1 and array 2 from two previous functions
  142.               if(code_buff1[i]==code_buff2[i])
  143.               s++;                                //again this how we verifiy, increment s whenever codes are matching
  144.               }
  145.                   if(s==sizeof(code)){            //Correct is always the size of the array
  146.                  
  147.                    for(i=0 ; i<sizeof(code) ; i++){
  148.                   code[i]=code_buff2[i];         //the code array now receives the new code
  149.                   EEPROM.put(i, code[i]);        //And stores it in the EEPROM
  150.                  
  151.                   }
  152.                   lcd.clear();
  153.                   lcd.print("Code Changed");
  154.                   delay(2000);
  155.                   }
  156.                   else{                         //In case the new codes aren't matching
  157.                   lcd.clear();
  158.                   lcd.print("Codes are not");
  159.                   lcd.setCursor(0,1);
  160.                   lcd.print("matching !!");
  161.                   delay(2000);
  162.                   }
  163.            
  164.           }
  165.          
  166.           else{                     //In case the old code is wrong you can't change it
  167.           lcd.clear();
  168.           lcd.print("Wrong");
  169.           delay(2000);
  170.           }
  171. }
  172.  
  173. void GetNewCode1(){                      
  174.   i=0;
  175.   j=0;
  176.   lcd.clear();
  177.   lcd.print("Enter new code");   //tell the user to enter the new code and press A
  178.   lcd.setCursor(0,1);
  179.   lcd.print("and press A");
  180.   delay(2000);
  181.   lcd.clear();
  182.   lcd.setCursor(0,1);
  183.   lcd.print("and press A");     //Press A keep showing while the top row print ***
  184.              
  185.          while(keypressed != 'A'){            //A to confirm and quits the loop
  186.              keypressed = myKeypad.getKey();
  187.                if(keypressed != NO_KEY && keypressed != 'A' ){
  188.                 lcd.setCursor(j,0);
  189.                 lcd.print("*");               //On the new code you can show * as I did or change it to keypressed to show the keys
  190.                 code_buff1[i]=keypressed;     //Store caracters in the array
  191.                 i++;
  192.                 j++;                    
  193.                 }
  194.                 }
  195. keypressed = NO_KEY;
  196. }
  197.  
  198. void GetNewCode2(){                         //This is exactly like the GetNewCode1 function but this time the code is stored in another array
  199.   i=0;
  200.   j=0;
  201.  
  202.   lcd.clear();
  203.   lcd.print("Confirm code");
  204.   lcd.setCursor(0,1);
  205.   lcd.print("and press A");
  206.   delay(3000);
  207.   lcd.clear();
  208.   lcd.setCursor(0,1);
  209.   lcd.print("and press A");
  210.  
  211.          while(keypressed != 'A'){
  212.              keypressed = myKeypad.getKey();
  213.                if(keypressed != NO_KEY && keypressed != 'A' ){
  214.                 lcd.setCursor(j,0);
  215.                 lcd.print("*");
  216.                 code_buff2[i]=keypressed;
  217.                 i++;
  218.                 j++;                    
  219.                 }
  220.                 }
  221. keypressed = NO_KEY;
  222. }
  223.  
  224. void OpenDoor(){             //Lock opening function open for 3s
  225.   lcd.clear();
  226.   lcd.print("Welcome");       //With a message printed
  227.   digitalWrite(Solenoid,HIGH);
  228.   delay(3000);
  229.   digitalWrite(Solenoid,LOW);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment