Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- || Simple Password Entry Using Matrix Keypad
- || 4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
- ||
- */
- //* is to validate password
- //# is to reset password attempt
- /////////////////////////////////////////////////////////////////
- #include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
- #include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
- Password password = Password( "1234" );
- unsigned long buttonPushedMillis; // when button was released
- unsigned long currentMillis;
- unsigned long turnOnDelay = 2500; // wait to turn on LED
- const byte ROWS = 4; // Four rows
- const byte COLS = 4; // columns
- // Define the Keymap
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = { A2,A3,A4,A5 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte colPins[COLS] = { 2,A0,A1, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
- // Create the Keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- void setup(){
- Serial.begin(9600);
- keypad.addEventListener(keypadEvent); //add an event listener for this keypad
- }
- void loop(){
- unsigned long currentMillis = millis();
- keypad.getKey();
- }
- //take care of some special events
- void keypadEvent(KeypadEvent eKey){
- switch (keypad.getState()){
- case PRESSED:
- Serial.print("Pressed: ");
- Serial.println(eKey);
- buttonPushedMillis = currentMillis;
- if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay)
- {
- Serial.print("timeout");
- }
- else
- {
- switch (eKey){
- case '*': checkPassword(); break;
- case '#': password.reset(); break;
- default: password.append(eKey);
- }
- }
- }
- }
- void checkPassword(){
- if (password.evaluate()){
- Serial.println("Success");
- password.reset();
- //Add code to run if it works
- }else{
- Serial.println("Wrong");
- password.reset();
- //add code to run if it did not work
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement