#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define row_num 4
#define col_num 4
char keys[row_num][col_num] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte row_pins[row_num] = {19, 18, 5, 17};
byte col_pins[col_num] = {16, 4, 2, 15};
LiquidCrystal_I2C lcd(0x27, 20, 4);
String password = "1234";
String input = "";
Keypad mykeypad = Keypad(makeKeymap(keys), row_pins, col_pins, row_num, col_num);
void setup() {
lcd.init(); // Initialize the LCD
lcd.clear(); // Clear the screen
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Enter password:");
Serial.begin(9600); // Start serial communication
}
void loop() {
char key = mykeypad.getKey();
if (key) {
if (key == '#') { // When "#" is pressed, check the password
lcd.clear();
if (input == password) {
lcd.setCursor(0, 0);
lcd.print("Welcome Home!");
delay(500); // Display for 4 seconds
// Send a signal to the second ESP32 to control the servo
Serial.println("SUCCESS"); // Send 'SUCCESS' to second ESP32
} else {
lcd.setCursor(0, 0);
lcd.print("Access denied!");
delay(500); // Show Access Denied for 2 seconds
// Send a signal to the second ESP32 for failure
Serial.println("FAILURE"); // Send 'FAILURE' to second ESP32
}
input = ""; // Reset input for the next attempt
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
}
else if (key == '*') { // When "*" is pressed, clear the password input
input = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password cleared");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
} else {
input.concat(key); // Add the key to the input string
lcd.setCursor(input.length() - 1, 1);
lcd.print('*'); // Display * for each entered character
}
}
}