#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the LCD address and dimensions
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 25, 33, 32}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define the correct password
String password = "1234";
String inputPassword = "";
// Define the pushbutton pin
const int buttonPin = 15; // Adjust to your ESP32 GPIO pin
bool systemOn = false;
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
lcd.backlight();
// Initialize the pushbutton
pinMode(buttonPin, INPUT_PULLUP);
// Initial LCD message
lcd.setCursor(0, 0);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("start system");
Serial.begin(115200);
}
void loop() {
if (digitalRead(buttonPin) == LOW && !systemOn) {
systemOn = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
inputPassword = "";
delay(500); // Debounce delay
}
if (systemOn) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Check if the password is correct
if (inputPassword == password) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome to your");
lcd.setCursor(0, 1);
lcd.print("home account");
delay(3000); // Show the message for 3 seconds
lcd.clear();
systemOn = false;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong password!");
lcd.setCursor(0, 1);
lcd.print("Try again");
delay(3000); // Show the message for 3 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
inputPassword = ""; // Reset input
}
} else if (key == '*') {
// Clear the current input
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second line
} else {
// Add the key to the input
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
}
if (digitalRead(buttonPin) == LOW && systemOn) {
// Turn off the system if the button is pressed again
systemOn = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System stopped");
delay(500); // Debounce delay
}
}