Advertisement
Xylitol

Electronic lock

Mar 9th, 2014
1,842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Password.h>
  2. #include <LiquidCrystal.h>
  3. #include <Keypad.h>
  4.  
  5. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  6. Password password = Password("1337");
  7. const byte ROWS = 4; // Four rows
  8. const byte COLS = 4; // Four columns
  9. // Define the Keymap
  10. char keys[ROWS][COLS] = {
  11.     {'1', '2', '3', 'A',},
  12.     {'4', '5', '6', 'B',},
  13.     {'7', '8', '9', 'C',},
  14.     {'*', '0', ' ', 'D',}
  15. };
  16. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  17. byte rowPins[ROWS] = {39, 38, 37, 36}; //connect to the row pinouts of the keypad
  18. byte colPins[COLS] = {43, 42, 41, 40}; //connect to the column pinouts of the keypad
  19. const int buttonPin = 52;
  20. int buttonState = 0;
  21.  
  22. // Create the Keypad
  23. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  24.  
  25. #define ledPin 13
  26.  
  27. void setup() {
  28.     lcd.begin(16, 2);
  29.     pinMode(buttonPin, INPUT);
  30.     pinMode(ledPin, OUTPUT);
  31.     digitalWrite(ledPin, LOW); // sets the LED on
  32.     Serial.begin(9600);
  33.     keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  34.     keypad.setDebounceTime(250);
  35. }
  36.  
  37. void loop() {
  38.     keypad.getKey();
  39.     buttonState = digitalRead(buttonPin);
  40.  
  41.     // check if the pushbutton is pressed.
  42.     // if it is, the buttonState is HIGH:
  43.     if (buttonState == HIGH) {
  44.         // turn LED on:    
  45.     } else {
  46.         // turn LED off:
  47.         lcd.clear();
  48.         password.reset();
  49.     }
  50. }
  51.  
  52. //take care of some special events
  53. void keypadEvent(KeypadEvent eKey) {
  54.     switch (keypad.getState()) {
  55.     case PRESSED:
  56.         lcd.print(eKey);
  57.         switch (eKey) {
  58.         case ' ':
  59.             guessPassword();
  60.             break;
  61.             lcd.setCursor(0, 1);
  62.         default:
  63.             password.append(eKey);
  64.         }
  65.     }
  66. }
  67.  
  68. void guessPassword() {
  69.     if (password.evaluate()) {
  70.  
  71.         digitalWrite(ledPin, HIGH);
  72.         delay(2000);
  73.         lcd.setCursor(0, 1);
  74.         lcd.print("VALID PASSWORD");
  75.         password.reset();
  76.         delay(600);
  77.         lcd.clear();
  78.         lcd.setCursor(0, 1);
  79.         lcd.print("Welcome");
  80.         delay(2000);
  81.         lcd.clear();
  82.         digitalWrite(ledPin, LOW);
  83.     } else {
  84.         digitalWrite(ledPin, LOW);
  85.         lcd.setCursor(0, 1);
  86.         lcd.print("INVALID PASSWORD");
  87.         password.reset();
  88.         delay(2000);
  89.         lcd.clear();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement