#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the keypad configuration
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {9,8,7,6}; // Connect the row pins to 5, 4, 3, 2
byte pin_column[COLUMN_NUM] = {5,4,3,2}; // Connect the column pins to 9, 8, 7, 6
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
Servo myServo; // Create a servo object
int buzzerPin = 11; // Buzzer connected to pin 11
int servoPin = 10; // Servo connected to pin 10
String inputPassword = ""; // Stores the entered password
String correctPassword = "1234"; // The correct password
bool doorUnlocked = false; // Flag to check if the door is unlocked
// Initialize LCD with I2C address 0x27 (check if this is the correct one for your LCD)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the servo and buzzer
myServo.attach(servoPin);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2);
lcd.backlight(); // Turn on the backlight
// Initially, set the servo to the locked position (0 degrees)
myServo.write(0);
// Display initial message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key); // Display keypress for debugging
inputPassword += key; // Add the pressed key to the input string
// Update the LCD with the entered key
lcd.setCursor(inputPassword.length() - 1, 1); // Move to the next position on the second row
lcd.print(key); // Display the entered character
// If the input password is correct
if (inputPassword == correctPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Correct Password");
lcd.setCursor(0, 1);
lcd.print("Welcome");
buzz(1); //beep once for correct password
unlockDoor(); // Call the function to unlock the door
inputPassword = ""; // Clear input after unlocking
}
// If the password is incorrect, play the buzzer
else if (inputPassword.length() == correctPassword.length()) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect Password!");
buzz(4); // Call the buzzer function to beep 5 times
inputPassword = ""; // Clear the input password
delay(2000); // Wait for 2 seconds to show the message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
// If user enters "##", lock the door
if (inputPassword == "##" && doorUnlocked) {
lockDoor();
inputPassword = ""; // Clear the input after locking
}
}
}
void unlockDoor() {
// Move the servo to 90 degrees to unlock the door
myServo.write(90);
doorUnlocked = true; // Set door status as unlocked
// After unlocking, show the message on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Unlocked");
lcd.setCursor(0, 1);
lcd.print("Press ## to Lock");
}
void lockDoor() {
// Move the servo to 0 degrees to lock the door
myServo.write(0);
doorUnlocked = false; // Set door status as locked
// After locking, show the message on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Locked");
}
void buzz(int times) {
for (int i = 0; i < times; i++) {
tone(buzzerPin, 1000); // Play a tone at 1000 Hz
delay(400); // Beep duration: 500ms
noTone(buzzerPin); // Turn off the buzzer
delay(400); // Pause between beeps
}
}