Advertisement
Jimbo425

Untitled

Mar 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ||  Simple Password Entry Using Matrix Keypad
  3. ||  4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
  4. ||
  5. */
  6.  
  7.  
  8. //* is to validate password  
  9. //# is to reset password attempt
  10.  
  11. /////////////////////////////////////////////////////////////////
  12.  
  13. #include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
  14. #include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
  15.  
  16. Password password = Password( "1234" );
  17. unsigned long buttonPushedMillis;  // when button was released
  18.  unsigned long currentMillis;
  19. unsigned long turnOnDelay = 2500; // wait to turn on LED
  20.  
  21.  
  22. const byte ROWS = 4; // Four rows
  23. const byte COLS = 4; //  columns
  24. // Define the Keymap
  25. char keys[ROWS][COLS] = {
  26.   {'1','2','3','A'},
  27.   {'4','5','6','B'},
  28.   {'7','8','9','C'},
  29.   {'*','0','#','D'}
  30. };
  31.  
  32. byte rowPins[ROWS] = { A2,A3,A4,A5 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  33. byte colPins[COLS] = { 2,A0,A1, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  34.  
  35.  
  36. // Create the Keypad
  37. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  38.  
  39. void setup(){
  40.  
  41.   Serial.begin(9600);
  42.   keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  43. }
  44.  
  45. void loop(){
  46.   unsigned long currentMillis = millis();
  47.   keypad.getKey();
  48. }
  49.  
  50. //take care of some special events
  51. void keypadEvent(KeypadEvent eKey){
  52.   switch (keypad.getState()){
  53.     case PRESSED:
  54.     Serial.print("Pressed: ");
  55.     Serial.println(eKey);
  56.      buttonPushedMillis = currentMillis;
  57.      if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay)
  58.      {
  59.       Serial.print("timeout");
  60.      }
  61.    else
  62.    {
  63.     switch (eKey){
  64.       case '*': checkPassword(); break;
  65.       case '#': password.reset(); break;
  66.       default: password.append(eKey);
  67.      }
  68.   }
  69. }
  70. }
  71. void checkPassword(){
  72.   if (password.evaluate()){
  73.     Serial.println("Success");
  74.      password.reset();
  75.     //Add code to run if it works
  76.   }else{
  77.     Serial.println("Wrong");
  78.      password.reset();
  79.     //add code to run if it did not work
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement