#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// Define keypad and LCD connections
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] = {19, 18, 5, 17}; // Adjust pin numbers as needed
byte colPins[COLS] = {16, 4, 0, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set I2C address of LCD
//Deine door lock pin and buzzer
#define doorLock 25
#define buzzer 26
// Define password
const char password[] = "1234"; // Set your password
char enteredPassword[5] = "";
byte currentPosition = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on backlight
lcd.clear(); // Clear display
lcd.setCursor(0, 0);
lcd.print("Connected, IP: ");
lcd.setCursor(0, 1);
lcd.println("192.168.12.01");
delay(5000);
lcd.clear();
lcd.setCursor(1, 0); // Set cursor to first line, first column
lcd.print("Enter Password ");
lcd.setCursor(0, 1);
lcd.print("Or Scan The Cart");
pinMode(doorLock, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
cmpPassword();
}
void cmpPassword() {
char key = keypad.getKey();
if (key) {
if (key == '*' || key == '#') {
currentPosition = 0;
lcd.clear();
lcd.print("Enter Password:");
} else {
enteredPassword[currentPosition++] = key;
enteredPassword[currentPosition] = '\0';
lcd.setCursor(currentPosition + 6, 1);
lcd.print("*");
}
if (currentPosition == 4) {
if (strcmp(enteredPassword, password) == 0) {
lcd.clear();
lcd.print("Access Granted!");
digitalWrite(doorLock, HIGH); // Unlock door
delay(3000); // Keep door unlocked for 3 seconds
digitalWrite(doorLock, LOW); // Relock door
lcd.clear();
lcd.print("taype Password ");
lcd.setCursor(0, 1);
lcd.print("Or Scan The Cart");
currentPosition = 0;
} else {
lcd.clear();
lcd.print("Incorrect Password");
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
lcd.clear();
lcd.print("taype Password ");
lcd.setCursor(0, 1);
lcd.print("Or Scan The Cart");
currentPosition = 0;
}
}
}
}