#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>
// Define keypad and LCD parameters
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; // Row pin numbers
byte colPins[COLS] = {8, 7, 6, 5}; // Column pin numbers
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define servo motor parameters
Servo servo; // Create servo object to control the servo motor
const int servoPin = 9; // Pin connected to servo motor
// Define LCD parameters
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, EN, D4, D5, D6, D7
// Define correct passcode and variables
const String correctPasscode = "1234"; // Change this to your desired passcode
String enteredPasscode = "";
void setup() {
Serial.begin(9600);
servo.attach(servoPin); // Attach the servo motor
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.print("Enter Passcode:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (enteredPasscode == correctPasscode) {
unlockDoor();
} else {
lcd.clear();
lcd.print("Incorrect Passcode!");
delay(2000);
lcd.clear();
lcd.print("Enter Passcode:");
}
enteredPasscode = ""; // Reset the entered passcode
} else {
enteredPasscode += key;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous input
lcd.setCursor(0, 1);
lcd.print(enteredPasscode);
}
}
}
void unlockDoor() {
lcd.clear();
lcd.print("Door Unlocked");
servo.write(90); // Rotate the servo to unlock position
delay(3000); // Wait for 2 seconds
servo.write(0); // Rotate the servo back to lock position
delay(1000);
lcd.clear();
lcd.print("Enter Passcode:");
}