#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
void checkPassword();
void openLock();
void triggerAlarm();
void beep(int times, int msOn);
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 lockServo;
const int servoPin = 10;
const int buzzerPin = 11;
LiquidCrystal_I2C lcd(0x27, 16, 2);
String inputPassword = "";
const String correctPassword = "1234";
int attempts = 0;
const int maxAttempts = 4;
const int openAngle = 180;
const int closedAngle = 0;
const unsigned long openDuration = 3000;
const unsigned long alarmDuration = 5000;
void setup() {
Serial.begin(9600);
lockServo.attach(servoPin);
lockServo.write(closedAngle);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Smart Lock Ready");
lcd.setCursor(0,1);
lcd.print("Enter 4 digits");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') { // مسح الإدخال
inputPassword = "";
lcd.setCursor(0,1);
lcd.print(" "); // مسح السطر الثاني بالكامل
lcd.setCursor(0,1);
lcd.print("Cleared");
Serial.println("Cleared input");
delay(500);
lcd.setCursor(0,1);
lcd.print("Enter 4 digits");
} else if (key == '#') { // مثال: يمكنك استخدام # لإرسال/تأكيد يدويا — هنا نتجاهله لأن نتحقق تلقائياً بعد 4 أرقام
// يمكنك تخصيص سلوك لزر #
} else {
inputPassword += key;
lcd.setCursor(0,1);
lcd.print("Input:");
lcd.print(inputPassword);
int rem = 16 - (6 + inputPassword.length());
for (int i=0;i<rem;i++) lcd.print(' ');
Serial.print("Current: ");
Serial.println(inputPassword);
if (inputPassword.length() == 4) {
checkPassword();
inputPassword = "";
delay(300);
lcd.setCursor(0,1);
lcd.print("Enter 4 digits ");
}
}
}
}
void checkPassword() {
if (inputPassword == correctPassword) {
Serial.println("Password correct — Opening lock.");
lcd.setCursor(0,0);
lcd.print("Correct! Unlocking ");
lcd.setCursor(0,1);
lcd.print("Please wait... ");
openLock();
attempts = 0;
lcd.setCursor(0,0);
lcd.print("Smart Lock Ready ");
} else {
attempts++;
Serial.print("Wrong password. Attempts: ");
Serial.println(attempts);
lcd.setCursor(0,0);
lcd.print("Wrong Password ");
lcd.setCursor(0,1);
lcd.print("Attempts:");
lcd.print(attempts);
if (attempts >= maxAttempts) {
Serial.println("Max attempts reached! Alarm!");
lcd.setCursor(0,0);
lcd.print("!! ALARM !! ");
lcd.setCursor(0,1);
lcd.print("Locked... ");
triggerAlarm();
attempts = 0;
lcd.setCursor(0,0);
lcd.print("Smart Lock Ready ");
lcd.setCursor(0,1);
lcd.print("Enter 4 digits ");
} else {
beep(1, 200);
delay(800);
lcd.setCursor(0,0);
lcd.print("Smart Lock Ready ");
}
}
}
// فتح وغلق السيرفو
void openLock() {
lockServo.write(openAngle);
delay(openDuration);
lockServo.write(closedAngle);
Serial.println("Lock closed again.");
}
// تفعيل البازر عند المحاولات الخاطئة
void triggerAlarm() {
unsigned long start = millis();
while (millis() - start < alarmDuration) {
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(buzzerPin, LOW);
delay(100);
}
digitalWrite(buzzerPin, LOW);
}
// صفارة بسيطة
void beep(int times, int msOn) {
for (int i = 0; i < times; i++) {
digitalWrite(buzzerPin, HIGH);
delay(msOn);
digitalWrite(buzzerPin, LOW);
delay(100);
}
}