#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#define SERVO_PIN 6
#define BUZZER_PIN 7
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo;
LiquidCrystal_I2C lcd(0x27, 20, 04);
const byte KEYPAD_ROWS = 4, KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
SafeState safeState;
void setup() {
lcd.begin(16, 2);
lockServo.attach(SERVO_PIN);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
if (safeState.locked()) {
lock();
} else {
unlock();
}
showStartupMessage();
}
void loop() {
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}
void beepKey() {
tone(BUZZER_PIN, 800, 50);
delay(100);
}
void beepSuccess() {
tone(BUZZER_PIN, 1200, 100);
delay(150);
tone(BUZZER_PIN, 1500, 100);
delay(150);
}
void beepError() {
for (int i = 0; i < 2; i++) {
tone(BUZZER_PIN, 300, 300);
delay(400);
}
}
void beepLock() {
tone(BUZZER_PIN, 1000, 100);
delay(150);
tone(BUZZER_PIN, 800, 100);
delay(150);
}
void lock() {
lockServo.write(SERVO_LOCK_POS); // إرسال إشارة لغلق السيرفو
safeState.lock(); // تعيين حالة الخزنة كمقفلة
beepLock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS); // إرسال إشارة لفتح السيرفو
}
void showStartupMessage() {
lcd.setCursor(2, 0);
lcd.print("WELL COME TO");
lcd.setCursor(0,1);
lcd.print("ELECTRONIC VAULT");
delay(1000);
lcd.setCursor(0, 2);
String message = "ArduinoSafe v1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]); delay(100);
}
delay(500);
}
String inputSecretCode() {
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
beepKey();
} else if (key == 'C' && result.length() > 0) {
result.remove(result.length() - 1);
lcd.setCursor(6 + result.length(), 1);
lcd.print(' ');
lcd.setCursor(6 + result.length(), 1);
beepKey();
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
bool setNewCode() {
lcd.clear(); lcd.setCursor(0, 0);
lcd.print("Enter new code:");
String newCode = inputSecretCode();
lcd.clear(); lcd.setCursor(0, 0);
lcd.print("Confirm new code");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear(); lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("VAULT NOT LOCK!");
beepError();
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear(); lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(1, 0);
lcd.print("VAULT UNLOCKED");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
beepSuccess();
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(0, 0);
lcd.print("PRESS # TO LOCK");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A FOR NEW CODE");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
if (readyToLock) {
lcd.clear(); lcd.setCursor(5, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.print(" ");
lcd.write(ICON_RIGHT_ARROW);
lcd.print(" ");
lcd.write(ICON_LOCKED_CHAR);
safeState.lock(); lock();
showWaitScreen(100);
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" VAULT LOCKED ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage(); unlock();
} else {
lcd.clear(); lcd.setCursor(0, 0);
lcd.print("Access Denied!");
beepError();
showWaitScreen(1000);
}
}