#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ESP32Servo.h>
#define SERVO_PIN 12 // ESP32 pin GPIO26 connected to servo motor
Servo servoMotor;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
int Buffer = 14;
int Lock = 27;
int InSideSensor = 1;
int Interval = 15000;
unsigned long previues = 0;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 3 // three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {23, 19, 18, 5}; // GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins
byte pin_column[COLUMN_NUM] = {4, 2, 15}; // GPIO4, GPIO0, GPIO2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
Serial.begin(115200);
pinMode(Buffer, OUTPUT);
pinMode(Lock, OUTPUT);
servoMotor.attach(SERVO_PIN);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}
int ServoPos = 90;
String InputPassword;
String Password = "1234";
bool AutoClose = false;
bool setPassword = false;
bool setingPassword = false;
String ConfirmPassword;
void loop() {
char key = keypad.getKey();
if (!setPassword) {
lcd.setCursor(0, 0);
lcd.print("Input Password");
}
if (key) {
if (key != '*' && !setPassword) {
InputPassword += key;
lcd.setCursor(0, 1);
lcd.print(InputPassword);
}
if (key == '*' && !setPassword) {
lcd.clear();
InputPassword = "";
lcd.setCursor(0, 1);
lcd.print(InputPassword);
}
if (key == '#' || setPassword) {
setPassword = true;
if (key != '#') {
ConfirmPassword += key;
}
if (!setingPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Old Password : ");
lcd.setCursor(0, 1);
lcd.print(ConfirmPassword);
if (ConfirmPassword == Password) {
setingPassword = true;
ConfirmPassword = "";
InputPassword = "";
Serial.println("Go to New Password");
}
}
if (setingPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("New Password : ");
lcd.setCursor(0, 1);
if (key != '#' && key != '*' ){
InputPassword += key;
}
lcd.print(InputPassword);
if (key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password reset");
lcd.setCursor(0, 1);
lcd.print("Successfuly <3");
Password = InputPassword;
InputPassword = "";
setingPassword = false;
setPassword = false;
delay(100);
lcd.clear();
Serial.println(Password);
}
}
}
}
if (InputPassword == Password) {
for (; ServoPos <= 180 ; ServoPos++) {
if (ServoPos == 90) {
digitalWrite(Lock, HIGH);
digitalWrite(Buffer, HIGH);
delay(100);
digitalWrite(Buffer, LOW);
}
if (ServoPos <= 130 && ServoPos >= 90) {
digitalWrite(Lock, LOW);
}
servoMotor.write(ServoPos);
delay((10));
AutoClose = false;
}
if (AutoClose == true) {
for (; ServoPos >= 90 ; ServoPos--) {
servoMotor.write(ServoPos);
if (ServoPos == 180 && ServoPos >= 160) {
digitalWrite(Buffer, HIGH);
}
if (ServoPos == 160) {
digitalWrite(Buffer, LOW);
}
delay(10);
}
InputPassword = "";
lcd.clear();
AutoClose = false;
}
if (millis() - previues >= Interval) {
AutoClose = true;
previues = millis();
} else {
AutoClose = false;
}
}
}