#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#define SERVO_PIN 6
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo;
float BETA = 3950;
LiquidCrystal_I2C lcd(0x27,16,2);
bool choice;
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, 8, 7};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
SafeState safeState;
void readTemperature() {
delay(500);
int analogValue1 = analogRead(A0);
float celsius1 = 1 / (log(1 / (1023.0 / analogValue1 - 1)) / BETA + 1.0 / 298.15) - 273.15;
int analogValue2 = analogRead(A1);
float celsius2 = 1 / (log(1 / (1023.0 / analogValue2 - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.println(celsius1);
Serial.println(celsius2);
}
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void showStartupMessage() {
readTemperature();
lcd.setCursor(4, 0);
lcd.print("Welcome!");
delay(100);
}
String inputSecretCode() {
readTemperature();
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;
}
}
return result;
}
void showWaitScreen(int delayMillis, bool showDots) {
readTemperature();
lcd.setCursor(2, 1);
if (showDots) {
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 2; i++) {
delay(delayMillis);
lcd.print("=");
}
} else {
lcd.print(" ");
}
}
bool setNewCode() {
readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new code:");
String newCode = inputSecretCode();
readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new code");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
readTemperature();
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(20);
return false;
}
}
void handleLockSystem() {
lcd.clear();
if (safeState.locked()) {
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Safe Locked! ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(20, true);
if (unlockedSuccessfully) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Unlocked!");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(10);
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
showWaitScreen(10, false);
}
} else {
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 0);
lcd.print(" # to lock");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
char key = keypad.getKey();
bool newCodeNeeded = true;
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(10, true);
}
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lockServo.attach(SERVO_PIN);
if (safeState.locked()) {
lock();
} else {
unlock();
}
showStartupMessage();
}
void loop() {
handleLockSystem();
}