#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
/* Định nghĩa cơ chế khóa */
#define SERVO_PIN 6
#define SERVO_LOCK_POS 10
#define SERVO_UNLOCK_POS 90
Servo lockServo;
/* Tạo LCD object */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Thiết lập bàn phím */
const byte KEYPAD_ROWS = 4; // số hàng
const byte KEYPAD_COLS = 4; // số cột
//Kết nối tới Arduino
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
//Mảng đại diện cho các phím trên keypad
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Tạo keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
// SafeState lưu trữ mã code trong EEPROM
// Class SafeState được định nghĩa trong file SafeState.h. Nó quản lý trạng thái hiện tại của khóa: mở hay đóng và mã code
SafeState safeState;
//Hàm khóa
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
//Hàm mở khóa
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
//Hàm nhập mã code
String inputSecretCode() {
lcd.setCursor(4, 1);
lcd.print("[______]");
lcd.setCursor(5, 1);
String result = "";
while (result.length() < 6) {
char key = keypad.getKey();
// Chỉ chấp nhận các chữ số làm mã code
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
return result;
}
//Hàm hiện thị trạng thái chờ
//Hàm này được sử dụng khi khóa, mở khóa và nhập sai mã code
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
//Vòng lặp này tạo hoạt ảnh xuất hiện lần lượt
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("-");
}
}
//Hàm tạo mã code mới
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");
delay(2000);
return false;
}
}
//Hàm hiển thị access thành công
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Access Granted");
delay(1000);
}
//Hàm logic khóa
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Enter Code ");
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Access Denied");
showWaitScreen(1000);
}
}
//Hàm logic mở khóa
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(" # to lock");
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = 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();
safeState.lock();
lock();
showWaitScreen(100);
}
}
void setup() {
lcd.begin(16, 2); //Khởi tạo và cài đặt số cột và hàng của LCD
lockServo.attach(SERVO_PIN);
// Đảm bảo rằng khóa vật lý được đồng bộ hóa với trạng thái EEPROM
Serial.begin(115200);
if (safeState.locked()) {
lock();
} else {
unlock();
}
}
void loop() {
//Có hai trạng thái chính mỗi trạng thái được xử lý bởi 1 hàm riêng
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}