#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
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 front;
Servo back;
int servoPinFront = 11;
int servoPinBack = 10;
int servoUnlockPos = 0;
int servoLockPos = 90;
const int trigPin = 12;
const int echoPin = 13;
String password = "1234";
String input = "";
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);
back.write(servoLockPos);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void showMessage() {
lcd.setCursor(4, 0);
lcd.print("WELCOME");
delay(1000);
lcd.setCursor(0, 2);
String message = "HOME SECURITY SYSTEM";
for (int i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
int readDistance() {
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = pulseIn(echoPin, HIGH) * 0.034 / 2;
return distance;
}
String inputPassword() {
String input = "";
lcd.setCursor(0, 0);
lcd.print("Enter password");
lcd.setCursor(0, 1);
lcd.print("Attempts: " + String(attempts));
lcd.setCursor(0, 2);
lcd.print("----");
while (input.length() < 4) {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
lcd.setCursor(input.length(), 2);
lcd.print('*');
input += key;
Serial.print(key);
Serial.println();
} else if (key == '#') {
input = "";
lcd.setCursor(0, 2);
lcd.print("----");
}
}
}
return input;
}
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 15 SECONDS");
delay(1000);
for (int i = 15; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print("TIME REMAINING: " + String(i) + " SECONDS");
delay(1000);
}
lockoutMode = false;
attempts = 5;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CALL 0707350937");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password");
lcd.setCursor(0, 1);
lcd.print("Attempts: " + String(attempts));
lcd.setCursor(0, 2);
lcd.print("----");
}
int distance = readDistance();
if (distance <= 15) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Front door: " + String(distance) + "cm");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password");
lcd.setCursor(0, 1);
lcd.print("Attempts: " + String(attempts));
lcd.setCursor(0, 2);
lcd.print("----");
input = inputPassword();
if (input == password) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Unlocked!");
front.write(servoUnlockPos);
delay(1000);
front.write(servoLockPos);
} else {
attempts--;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect password!");
lcd.setCursor(0, 1);
lcd.print("Attempts: " + String(attempts));
lcd.setCursor(0, 2);
lcd.print("----");
delay(2000);
if (attempts == 0) {
lockoutMode = true;
}
}
}
}