#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#include <DHT.h> // Include the DHT library
#define HI_TEMP_THRESHOLD 10
#define LO_TEMP_THRESHOLD 60
/* Locking mechanism definitions */
#define SERVO_PIN 6
#define SERVO_LOCK_POS 90
#define SERVO_UNLOCK_POS 20
Servo lockServo;
#define BUZZER_PIN 13
/* DHT Sensor definitions */
#define DHT_PIN A4 // Pin connected to the DHT22 sensor
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE); // Initialize DHT sensor
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte 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 = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
/* Global variable to store operating mode */
bool coldMode = true; // true = Cold mode, false = Hot mode
bool coldBoot=true
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
digitalWrite(BUZZER_PIN, HIGH);
delay(1000); // Keep the buzzer on for 1 second
digitalWrite(BUZZER_PIN, LOW);
}
void showStartupMessage() {
lcd.setCursor(4, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(0, 2);
String message = "Frieza(r) v1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(2500);
}
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;
}
}
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);
if (coldBoot==true){
selectOperatingMode(); // Select the mode when setting a new code
coldBoot=false;
}
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
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(1000);
}
void selectOperatingMode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Mode:");
lcd.setCursor(0, 1);
lcd.print("A=Cold, B=Hot");
char key = keypad.getKey();
while (key != 'A' && key != 'B') {
key = keypad.getKey(); // Keep waiting for 'A' or 'B'
}
if (key == 'A') {
coldMode = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cold Mode Selected");
} else if (key == 'B') {
coldMode = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hot Mode Selected");
}
delay(2000); // Display the selection for 2 seconds
}
void safeUnlockedLogic() {
lcd.clear();
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);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A=n code,D=Mode");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#' && key != 'D') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
} else if (key == 'D') {
selectOperatingMode();
safeUnlockedLogic(); // Allow mode selection using 'D'
}
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();
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature and humidity on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(int(round(temperature)));
lcd.print("C,");
lcd.print("Hum:");
lcd.print(int(round(humidity)));
lcd.print("%");
// Temperature alert based on the selected mode
if (coldMode) {
if (temperature > HI_TEMP_THRESHOLD) {
// Cold mode: Trigger high-temperature alert
digitalWrite(BUZZER_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("High Temp Alert!");
digitalWrite(BUZZER_PIN, LOW);
delay(10000); // Keep the alert for 10 seconds
}
} else {
if (temperature < LO_TEMP_THRESHOLD) {
// Hot mode: Trigger low-temperature alert
digitalWrite(BUZZER_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("Low Temp Alert!");
digitalWrite(BUZZER_PIN, LOW);
delay(10000); // Keep the alert for 10 seconds
}
}
// Wait for user to press '#' before entering the code
lcd.setCursor(0, 1);
char key = keypad.getKey();
while (key != '#') {
key = keypad.getKey(); // Wait until '#' is pressed
}
// Once '#' is pressed, ask for the code
lcd.clear();
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!");
showWaitScreen(1000);
}
}
void setup() {
lcd.begin(16, 2);
init_icons(lcd);
lockServo.attach(SERVO_PIN);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW); // Ensure the buzzer is off initially
dht.begin(); // Initialize the DHT sensor
Serial.begin(115200);
if (safeState.locked()) {
lock();
} else {
unlock();
}
showStartupMessage();
// Force user to select the operating mode at boot
}
void loop() {
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}