#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int servoPin = 9;
Servo myServo;
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] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2, A0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27 for the I2C module, 16x2 display
char password[] = "123456"; // Change this to your desired password
char enteredPassword[9];
int passwordIndex = 0;
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
lcd.print("Enter Password:");
myServo.attach(servoPin);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*' || key == '#') {
// Ignore these keys
} else if (key == 'D') {
if (strcmp(enteredPassword, password) == 0) {
lcd.clear();
lcd.print("Password OK");
delay(1000);
lcd.clear();
myServo.write(90); // Rotate servo to 90 degrees
delay(2000);
myServo.write(0); // Rotate servo back to 0 degrees
lcd.print("Enter Password:");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(1000);
lcd.clear();
lcd.print("Enter Password:");
passwordIndex = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
}
} else {
enteredPassword[passwordIndex++] = key;
lcd.setCursor(passwordIndex - 1, 1);
lcd.print('*');
}
}
}