#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
Servo myservo;
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[4] = {12, 14, 27, 26};
byte pin_column[4] = {25, 33, 13, 32};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, 4, 4 );
LiquidCrystal_I2C lcd(0x27, 16, 2);
int cursorColumn = 0;
const String password = "1234";
String input_password;
void setup() {
Serial.begin(9600);
input_password.reserve(32);
myservo.attach(5);
lcd.init();
lcd.backlight();
pinMode(2, INPUT);
myservo.write(0);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Enter the OTP");
char key = keypad.getKey();
if (key) {
lcd.setCursor(cursorColumn, 1);
lcd.print(key);
cursorColumn++;
Serial.println(key);
if (key == '*') {
input_password = "";
lcd.clear();
cursorColumn=0;
} else if (key == '#') {
if (input_password == password)
{
Serial.println("The password is correct => unlock");
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Correct Password");
delay(1000);
lcd.clear();
myservo.write(90);
int i;
i=0;
while(i<=1000){
delay(i);
int state;
state=digitalRead(2);
if(state==HIGH)
{
int x=1;
myservo.write(0);
lcd.setCursor(0, 0);
lcd.print("Locked");
delay(1500);
lcd.clear();
lcd.print("Wait");
i=1000;
}
i=i+100;
}
delay(500);
lcd.setCursor(0, 1);
lcd.print("4 sec left");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("3 sec left");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("2 sec left");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("1 sec left");
delay(1000);
lcd.clear();
myservo.write(0);
} else {
Serial.println("The password is incorrect, try again");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect");
lcd.setCursor(0, 1);
lcd.print("Password");
delay(2000);
lcd.clear();
}
cursorColumn=0;
input_password = "";
}
else {
input_password += key;
}
}
}