Advertisement
kabbo512

Door Lock

Aug 17th, 2021
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <LiquidCrystal.h>
  3. #include <Servo.h>
  4. Servo myservo;
  5. LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
  6. #define Password_Lenght 7 // Give enough room for six chars + NULL char
  7. int pos = 0;    // variable to store the servo position
  8. char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
  9. char Master[Password_Lenght] = "123456";
  10. byte data_count = 0, master_count = 0;
  11. bool Pass_is_good;
  12. char customKey;
  13. const byte ROWS = 4;
  14. const byte COLS = 3;
  15. char keys[ROWS][COLS] = {
  16.   {'1', '2', '3'},
  17.   {'4', '5', '6'},
  18.   {'7', '8', '9'},
  19.   {'*', '0', '#'}
  20. };
  21. bool door = true;
  22. byte rowPins[ROWS] = {1, 2, 3, 4}; //connect to the row pinouts of the keypad
  23. byte colPins[COLS] = {5, 6, 7}; //connect to the column pinouts of the keypad
  24. Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
  25. void setup()
  26. {
  27.   myservo.attach(9);
  28.   ServoClose();
  29.   lcd.begin(16, 2);
  30.   lcd.print(" Arduino Door");
  31.   lcd.setCursor(0, 1);
  32.   lcd.print("--Look project--");
  33.   delay(3000);
  34.   lcd.clear();
  35. }
  36. void loop()
  37. {
  38.   if (door == 0)
  39.   {
  40.     customKey = customKeypad.getKey();
  41.     if (customKey == '#')
  42.     {
  43.       lcd.clear();
  44.       ServoClose();
  45.       lcd.print("  Door is close");
  46.       delay(3000);
  47.       door = 1;
  48.       clearData();
  49.     }
  50.   }
  51.   else Open();
  52. }
  53. void clearData()
  54. {
  55.   while (data_count != 0)
  56.   { // This can be used for any array size,
  57.     Data[data_count--] = 0; //clear array for new data
  58.   }
  59.   return;
  60. }
  61. void ServoOpen()
  62. {
  63.   for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
  64.     // in steps of 1 degree
  65.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  66.     delay(15);                       // waits 15ms for the servo to reach the position
  67.   }
  68. }
  69. void ServoClose()
  70. {
  71.   for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
  72.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  73.     delay(15);                       // waits 15ms for the servo to reach the position
  74.   }
  75. }
  76. void Open()
  77. {
  78.   lcd.setCursor(0, 0);
  79.   lcd.print(" Enter Password");
  80.   customKey = customKeypad.getKey();
  81.   if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  82.   {
  83.     Data[data_count] = customKey; // store char into data array
  84.     lcd.setCursor(data_count, 1); // move cursor to show each new char
  85.     lcd.print(Data[data_count]); // print char at said cursor
  86.     data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
  87.   }
  88.   if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  89.   {
  90.     Data[data_count]='\0';
  91.     if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)
  92.     {
  93.       lcd.clear();
  94.       ServoOpen();
  95.       lcd.print("  Door is Open");
  96.       delay(1000);
  97.       door = 0;
  98.      
  99.     }
  100.     else
  101.     {
  102.       lcd.clear();
  103.       lcd.print(" Wrong Password");
  104.       delay(1000);
  105.       door = 1;
  106.       clearData();
  107.     }
  108.    
  109.    }
  110.    
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement