Advertisement
McMrARM

Arduino door lock

Nov 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <Wire.h>
  3. #include <LCD.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. #define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
  7. #define BACKLIGHT_PIN     3
  8. #define En_pin  2
  9. #define Rw_pin  1
  10. #define Rs_pin  0
  11. #define D4_pin  4
  12. #define D5_pin  5
  13. #define D6_pin  6
  14. #define D7_pin  7
  15.  
  16.  
  17. LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
  18.  
  19. const int beeper = 11;
  20.  
  21. const byte ROWS = 4; //four rows
  22. const byte COLS = 4; //four columns
  23. char keys[ROWS][COLS] = {
  24.   {'1', '2', '3', 'A'},
  25.   {'4', '5', '6', 'B'},
  26.   {'7', '8', '9', 'C'},
  27.   {'*', '0', '#', 'D'}
  28. };
  29.  
  30. byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
  31. byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
  32.  
  33. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  34.  
  35. const String valid = "7A8C";
  36. String entry = "";
  37.  
  38. void setup() {
  39.   Serial.begin(9600);
  40.   lcd.begin (20, 4);
  41.  
  42.   // Switch on the backlight
  43.   lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  44.   lcd.setBacklight(HIGH);
  45.   lcd.home();
  46.   lcd.print(" SECU-SURE SECURITY");
  47.   delay(2000);
  48.   tone(beeper, 987);
  49.   delay(100);
  50.   tone(beeper, 1318);
  51.   delay(300);
  52.   noTone(beeper);
  53.   lcd.setCursor ( 0, 1 );
  54.   lcd.print("  >>>> SECURE <<<<  ");
  55.  
  56.  
  57. }
  58.  
  59. void loop() {
  60.   char key = keypad.getKey();
  61.   if (key) {
  62.     tone(beeper, 3000);
  63.     delay(50);
  64.     noTone(beeper);
  65.     Serial.print(key);
  66.     if (entry.length() == 3) {
  67.       delay(200);
  68.       lcd.setCursor(7, 3);
  69.       lcd.print("*");
  70.       entry = entry + String(key);
  71.       Serial.println("");
  72.       lcd.setCursor ( 0, 3 );
  73.       lcd.print("              ");
  74.       if (entry == valid) {
  75.         lcd.setCursor ( 0, 1 );
  76.         lcd.print("  >>>> UNLOCK <<<<  ");
  77.         //do things
  78.         tone(beeper, 3000);
  79.         delay(50);
  80.         noTone(beeper);
  81.         delay(50);
  82.         tone(beeper, 3000);
  83.         delay(50);
  84.         noTone(beeper);
  85.  
  86.         delay(5000);
  87.         lcd.setCursor ( 0, 1 );
  88.         lcd.print("  >>>> SECURE <<<<  ");
  89.       } else {
  90.         tone(beeper, 400);
  91.         delay(1500);
  92.         noTone(beeper);
  93.       }
  94.       entry = "";
  95.     }
  96.     else if (entry.length() == 2) {
  97.       lcd.setCursor(6, 3);
  98.       lcd.print("*");
  99.       entry = entry + String(key);
  100.     }
  101.     else if (entry.length() == 1) {
  102.       lcd.setCursor(5, 3);
  103.       lcd.print("*");
  104.       entry = entry + String(key);
  105.     }
  106.     else if (entry.length() == 0) {
  107.       lcd.setCursor(0, 3);
  108.       lcd.print("PIN:");
  109.       lcd.setCursor(4, 3);
  110.       lcd.print("*");
  111.       entry = String(key);
  112.     }
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement