/*
Wiring :
LCD Arduino
SDA ----> A4
SCL ----> A5
VCC ----> 5V
GND ----> GND
LED ----> 13
Servo ----> 7
Keypad:
R1 ----> 3
R2 ----> 2
R3 ----> 1
R4 ----> 0
C1 ----> A3
C2 ----> A2
C3 ----> A1
C4 ----> A0
*** O-Tech ***
A Place You Can Trust
https://wa.me/201207738604
https://www.facebook.com/profile.php?id=61555112881938
https://www.youtube.com/@OTECHegypt
*/
#define buzzer 4
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Servo.h>
Servo servo1;
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, A2, A1, A0}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
String u_pass = "1234", cur_pass = "", new_pass = "";
byte chg_pass = 0; // 0 : don't change, 1 : enter new pass, 2 : confirm new pass
byte failed_attempt = 0;
int lockout_dur = 5; // lock the keypad for 5 seconds after 3 failed attempts.
unsigned long int unlocked = 0;
bool reset = false;
void setup(){
Serial.begin(9600);
Serial.println("Starting");
pinMode(13, OUTPUT);
pinMode(buzzer, OUTPUT);
servo1.attach(7);
lcd.init();
lcd.backlight();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
delay(1000);
}
void loop(){
char key = customKeypad.getKey();
if(reset && millis() - unlocked >= 5000){
lcd.clear();
lcd.print("Enter Password: ");
lcd.setCursor(0, 1);
reset = false;
unlocked = 0;
servo1.write(90); // close door
}
if (key){
tone(buzzer, 1000, 25);
//Serial.println(key);
if(key == '#'){ // compare passwords here
// chg_pass 0
if(chg_pass == 0){
if(cur_pass == u_pass){
Serial.println("Password is correct");
lcd.clear();
lcd.print(" Unlocked ");
servo1.write(0); // open door
unlocked = millis();
tone(buzzer, 1000, 150);
reset = true;
failed_attempt = 0;
}
else{
failed_attempt++;
Serial.println("Wrong password");
lcd.clear();
lcd.print("wrong pass");
for(int i = 0; i <= 5; i++){
tone(buzzer, 5000, 50);
delay(200);
}
lcd.clear();
lcd.print("failed attempts");
lcd.setCursor(0, 1);
lcd.print(failed_attempt);
delay(1000);
reset = true;
if(failed_attempt >= 3){ start_alarm(); }
}
}
else if(chg_pass == 1){
// Ask for the old password and verify it first.
if(cur_pass == u_pass){
chg_pass = 2;
Serial.println("Password is correct");
Serial.println("Enter New Pass"); // numbers only
lcd.setCursor(0, 0);
lcd.print("Enter new pass: ");
clear_line(1);
lcd.setCursor(0, 1);
}
else{
Serial.print("cur_pass is "); Serial.println(cur_pass);
chg_pass = 0;
Serial.println("Wrong Pass"); Serial.println("Press \"*\" to try again");
lcd.setCursor(0, 0);
lcd.print("Wrong pass! ");
delay(500);
lcd.print("Press \"*\" to retry");
}
}
else if(chg_pass == 2){ // old pass was entered correctly, now reading new pass for 1st time
Serial.println("confirm pass");
lcd.clear();
lcd.print("Confirm pass: ");
lcd.setCursor(0, 1);
new_pass = cur_pass;
chg_pass = 3;
}
else if(chg_pass == 3){ //now confirming new pass
if(cur_pass == new_pass){
Serial.println("Password changed");
lcd.clear();
lcd.print("Password changed");
u_pass = new_pass;
new_pass = "";
delay(1000);
lcd.clear();
lcd.print("Enter password : ");
lcd.setCursor(0, 1);
}
else{
Serial.println("pass1 & pass2 don't match");
Serial.println("press \"*\" to start over");
lcd.clear();
lcd.print("pass1 != pass2 ");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Press * to retry");
delay(1000);
new_pass = "";
reset = true;
}
chg_pass = 0;
}
cur_pass = "";
}
else if(key - '0' >= 0 && key - '0' <= 9){ // use numbers only for the password (no letters or symbols)
lcd.print("*");
cur_pass += key;
//Serial.println(cur_pass);
}
if(key == '*'){
chg_pass = 1;
Serial.println("Enter old pass");
lcd.clear();
lcd.print("Change password ");
delay(1000);
lcd.clear();
lcd.print("Enter old pass :");
lcd.setCursor(0, 1);
// move to 2nd line on lcd
//Serial.println("New Password : "); // confirm current pass before showing this.
}
}
}
void start_alarm(){
lcd.clear();
lcd.print("Keypad LOCKED!");
unsigned long int lockout_start = millis();
int i;
while(millis() - lockout_start <= lockout_dur * 1000){
// turn on buzzer/siren/light
digitalWrite(13, HIGH);
// flash LCD
lcd.noBacklight();
for(i=700;i<800;i+=2){
tone(buzzer,i);
delay(1);
}
for(i=800;i>700;i--){
tone(buzzer,i);
delay(1);
}// delay(500);
digitalWrite(13, LOW);
lcd.backlight();
for(i=700;i<800;i+=2){
tone(buzzer,i);
delay(1);
}
for(i=800;i>700;i--){
tone(buzzer,i);
delay(1);
}
noTone(buzzer);
}
Serial.println("You can try again now");
lcd.clear();
lcd.print("Enter password:");
lcd.setCursor(0, 1);
failed_attempt = 0;
}
void clear_line(int ln) {
lcd.setCursor(0, ln);
lcd.print(" ");
}