#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Keypad settings
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);
// LCD settings
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Servo settings
Servo front; // front door
Servo back; // back door
int servoPinFront = 11; // front door servo pin
int servoPinBack = 10; // back door servo pin
int servoUnlockPos = 0;
int servoLockPos = 90;
// Password settings
String password = "1234";
String input = "";
// Attempts counter
int attempts = 5;
bool lockoutMode = false;
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.init();
lcd.setCursor(0, 0);
lcd.backlight();
showMessage();
front.attach(servoPinFront);
back.attach(servoPinBack);
front.write(servoLockPos); // Initialize front door servo to lock position
back.write(servoLockPos); // Initialize back door servo to lock position
}
void showMessage() {
lcd.setCursor(4, 0);
lcd.print("WELCOME");
delay(1000);
lcd.setCursor(0, 2);
String message = "HOMING SYSTEM";
for (int i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
void loop() {
if (lockoutMode) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TOO MANY ATTEMPTS!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WAIT FOR 30 SECONDS");
delay(1000);
for (int i = 30; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print("TIME REMAINING: " + String(i) + " SECONDS");
delay(1000);
}
lockoutMode = false;
attempts = 5; // Reset attempts counter
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password");
lcd.setCursor(1, 2);
lcd.print("Attempts: " + String(attempts));
input = inputPassword();
if (input == password) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Unlocked!");
front.write(servoUnlockPos); // Move front door servo to unlock position
back.write(servoUnlockPos); // Move back door servo to unlock position
delay(1000);
front.write(servoLockPos); // Move front door servo back to lock position
back.write(servoLockPos); // Move back door servo back to lock position
attempts = 5; // Reset attempts counter
} else {
attempts--; // Decrement attempts counter
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("Attempts remaining: " + String(attempts));
delay(1000);
if (attempts == 0) {
lockoutMode = true;
}
}
}
String inputPassword() {
String input = "";
while (input.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
input += key;
Serial.print(key);
Serial.println();
}
}
String password = String(input);
Serial.print(password);
return password;
}