#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
const byte rows = 4;
const byte columns = 4;
int holdDelay = 700;
char key = 0;
char STR[4] = {'2', '0', '2', '1'}; // Current password
char newPassword[4] = {' ', ' ', ' ', ' '};
char str[4] = {' ', ' ', ' ', ' '};
int i = 0, count = 0;
char keys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
// Updated pin assignments for ESP32
byte pin_rows[rows] = {19, 18, 5, 17};
byte pin_columns[columns] = {16, 4, 0, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_columns, rows, columns);
enum Mode { UNLOCKING, CHANGE_PASSWORD };
Mode mode = UNLOCKING;
void setup() {
Serial.begin(9600);
myServo.attach(15); // Attach the servo to pin 15
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print(" Enter Password");
}
void loop() {
char temp = keypad.getKey();
if (temp != 0) {
key = temp;
}
if ((int)keypad.getState() == HOLD) {
delay(holdDelay);
}
if ((int)keypad.getState() == RELEASED) {
processKey(key);
key = 0; // Reset key after processing
}
delay(50);
}
void processKey(char key) {
if (key == '*') {
if (mode == UNLOCKING) {
mode = CHANGE_PASSWORD; // Switch to changing password mode
resetInput();
lcd.clear();
lcd.print(" New Password:");
} else {
resetInput();
mode = UNLOCKING; // Switch back to unlocking mode
lcd.clear();
lcd.print(" Enter Password");
}
return;
}
if (mode == UNLOCKING) {
handleUnlockingMode(key);
} else if (mode == CHANGE_PASSWORD) {
handlePasswordChangeMode(key);
}
}
void handleUnlockingMode(char key) {
if (i < 4) {
str[i] = key;
lcd.setCursor(6 + i, 1);
lcd.print(str[i]);
delay(100);
lcd.setCursor(6 + i, 1);
lcd.print("*");
i++;
}
if (i == 4) {
count = 1;
}
if (count == 1) {
if (comparePasswords(str, STR)) {
lcd.clear();
lcd.print(" Opened!");
myServo.write(180); // Open the door
delay(3000); // Keep the door open for 3 seconds
myServo.write(90); // Close the door
lcd.clear();
lcd.print(" Closed!");
delay(1000);
resetInput();
lcd.print(" Enter Password");
} else {
lcd.clear();
lcd.print(" Incorrect!");
delay(3000);
resetInput();
lcd.print(" Enter Password");
}
}
}
void handlePasswordChangeMode(char key) {
if (i < 4) {
newPassword[i] = key;
lcd.setCursor(6 + i, 1);
lcd.print(newPassword[i]);
delay(100);
lcd.setCursor(6 + i, 1);
lcd.print("*");
i++;
}
if (i == 4) {
if (comparePasswords(newPassword, STR)) {
lcd.clear();
lcd.print(" Same Password!");
delay(3000);
} else {
// Change the password
memcpy(STR, newPassword, sizeof(newPassword));
lcd.clear();
lcd.print(" Password Changed!");
delay(3000);
}
resetInput();
mode = UNLOCKING; // Return to unlocking mode
lcd.clear();
lcd.print(" Enter Password");
}
}
bool comparePasswords(char* input, char* reference) {
for (int j = 0; j < 4; j++) {
if (input[j] != reference[j]) {
return false;
}
}
return true;
}
void resetInput() {
for (int j = 0; j < 4; j++) {
str[j] = ' ';
newPassword[j] = ' ';
}
i = 0;
count = 0;
lcd.clear();
if (mode == UNLOCKING) {
lcd.print(" Enter Password");
} else {
lcd.print(" New Password:");
}
}