/*
Credit for : Nathan Sobieck
|| 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 <Wire.h>
#include <Servo.h>
#include <Password.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Password password = Password( "1234" );
const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = { 8, 7, 6, 5 };
byte colPins[COLS] = { 4, 3, 2 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(10, OUTPUT); //Pin IN1 on the relay module
digitalWrite(10, HIGH); // Turn it HIGH
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
keypad.addEventListener(keypadEvent);
}
void loop() {
keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
// lcd.write("Pressed: ");
lcd.write(eKey);
switch (eKey) {
case '*': checkPassword();password.reset(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword() {
if (password.evaluate()) {
Serial.println("Success");
lcd.write("Success");
digitalWrite(10, LOW); // Actuate the relay module
delay(1000);
digitalWrite(10,HIGH);
delay(1000);
lcd.clear();
}
else {
Serial.println("Wrong");
lcd.write("Wrong");
digitalWrite(10, HIGH); //Reset the relay module
// myservo.write(0);
delay(1000);
lcd.clear();
}
}