#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
int servoPin = 11;
const int keypadRows = 4;
const int keypadColumns = 4;
const byte rowPins[keypadRows] = {9, 8, 7, 6};
const byte colPins[keypadColumns] = {5, 4, 3, 2};
char keys[keypadRows][keypadColumns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap((char*)keys), rowPins, colPins, keypadRows, keypadColumns);
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo servo;
int servoUnlockPos = 90;
int servoLockPos = 0;
String password = "1234";
void setup() {
lcd.begin(20, 4);
lcd.init();
lcd.setCursor(0, 0);
lcd.backlight();
servo.attach(servoPin);
servo.write(servoLockPos);
}
void loop() {
lcd.setCursor(1, 0);
lcd.print("Enter password:");
String input = inputPassword();
if (input == password) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Unlocked!");
servo.write(servoUnlockPos);
delay(1000);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
delay(1000);
}
}
String inputPassword() {
String input = "";
while (input.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
input += key;
}
}
return input;
}