#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad setup
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo setup
Servo myservo;
int servoPin = 10;
// RGB LED setup
int redPin = 11;
int greenPin = 12;
int bluePin = 13;
// Buzzer setup
int buzzerPin = A0;
// Password
String password = "1234";
String inputPassword = "";
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize servo
myservo.attach(servoPin);
// Initialize RGB LED pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Initialize buzzer pin
pinMode(buzzerPin, OUTPUT);
// Display initial message
lcd.print("Press any button");
}
void loop() {
char key = keypad.getKey();
if (key) {
lcd.clear();
lcd.print("Enter password:");
while (inputPassword.length() < password.length()) {
key = keypad.getKey();
if (key) {
inputPassword += key;
lcd.setCursor(inputPassword.length(), 1);
lcd.print("*");
}
}
if (inputPassword == password) {
// Correct password
lcd.clear();
lcd.print("Welcome");
setColor(0, 255, 0); // Green
myservo.write(90);
delay(2000);
lcd.clear();
lcd.print("For lock press *");
while (keypad.getKey() != '*') {
// Wait for '*' key press
}
lcd.clear();
lcd.print("Locked");
setColor(0, 0, 255); // Blue
myservo.write(0);
delay(3000); // Add a 3-second delay before displaying "Press any button"
// Wait for button press to unlock
lcd.clear();
lcd.print("Press any button");
setColor(255, 255, 255); // White
waitForButtonPress();
} else {
// Incorrect password
lcd.clear();
lcd.print("Try again");
setColor(255, 0, 0); // Red
delay(2000);
lcd.clear();
lcd.print("Press any button");
}
inputPassword = "";
}
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void waitForButtonPress() {
setColor(255, 255, 254); // White
while (!keypad.getKey()) {
// Wait for any key press
}
}