Advertisement
Ramog

Code_lock

Oct 28th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. //libarys
  2. #include <LiquidCrystal.h>
  3. #include <Keypad.h>
  4.  
  5. // initialize the library with the numbers of the interface pins
  6. LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
  7.  
  8. const byte ROWS = 4; //four rows
  9. const byte COLS = 4; //four columns
  10.  
  11. //define the cymbols on the buttons of the keypads
  12. char hexaKeys[ROWS][COLS] = {
  13.   {'1', '2', '3', 'A'},
  14.   {'4', '5', '6', 'B'},
  15.   {'7', '8', '9', 'C'},
  16.   {'*', '0', '#', 'D'}
  17. };
  18.  
  19. byte rowPins[ROWS] = {10, 11, 12, 13}; //connect to the row pinouts of the keypad
  20. byte colPins[COLS] = {A3, A2, A1, A0}; //connect to the column pinouts of the keypad
  21.  
  22. int digits, password;
  23. bool PasswordIsVis = false;
  24.  
  25. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  26.  
  27.  
  28. void setup() {
  29.   lcd.begin(16, 2);
  30.   pinMode(2, OUTPUT);
  31.   digitalWrite(2, LOW);
  32.   Serial.begin(9600);
  33. }
  34.  
  35. void passwordcon(char keys) {
  36.   int key = keys - '0';
  37.   Serial.println(key);
  38.   Serial.println(digits);
  39.   int setpassword = 1003;
  40.  
  41.   if (digits == 1) {
  42.     password = 1;
  43.   }
  44.   password = password + key *  pow(10, 4 - digits);
  45.  
  46.   if (digits == 4 && password == setpassword) {
  47.     lcd.clear();
  48.     lcd.setCursor(0, 0);
  49.     lcd.print("Correct Password!");
  50.  
  51.     digitalWrite(2, HIGH);
  52.     delay(1000);
  53.     digitalWrite(2, LOW);
  54.  
  55.     password = 0;
  56.     digits = 0;
  57.   }
  58.   else if (digits == 4 && password != setpassword) {
  59.     digits = 0;
  60.     password = 0;
  61.     lcd.setCursor(0, 0);
  62.     lcd.clear();
  63.     lcd.print("Wrong Password!");
  64.     delay(1000 * 4);
  65.     lcd.clear();
  66.   }
  67. }
  68.  
  69. void loop() {
  70.   lcd.setCursor(0, 0);
  71.   lcd.print("Enter Password: ");
  72.  
  73.   // print the number of seconds since reset:
  74.   char customKey = customKeypad.getKey();
  75.  
  76.   if (customKey) {
  77.     lcd.setCursor(0, 1);
  78.     digits++;
  79.     passwordcon(customKey);
  80.     if (PasswordIsVis == false) {
  81.       for (int i = digits; i > 0; i--) {
  82.         lcd.print("*");
  83.       }
  84.     }
  85.     else {
  86.      
  87.       lcd.print(password);
  88.     }
  89.  
  90.   }
  91.  
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement