#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#define SERVO_PIN 6
#define SERVO_PIN1 13
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo, lockServo1;
LiquidCrystal_I2C lcd(0x27,16,2);
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 safeState;
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void lock1() {
lockServo1.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void unlock1() {
lockServo1.write(SERVO_UNLOCK_POS);
}
String inputSecretCode() {
lcd.setCursor(4, 1);
lcd.print("[______]");
lcd.setCursor(5, 1);
String result = "";
while (result.length() < 6) {
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 pass:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new pass");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Pass mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Unlocked");
lcd.setCursor(15, 1);
lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void chon(){
lcd.setCursor(1, 0);
lcd.print("select channel");
lcd.setCursor(0, 1);
lcd.print("___A__B__C__D___");
auto key = keypad.getKey();
if (key != 'A'&& key != 'B'&& key != 'C'&& key != 'D') {
key = keypad.getKey();
}
if (key == 'A') {
safeUnlockedLogic();
}
while(key == 'B') {
safeUnlockedLogic();
}
while(key == 'C') {
safeUnlockedLogic();
}
while(key == 'D') {
safeUnlockedLogic();
}
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" # = enter pass");
lcd.setCursor(1, 1);
lcd.print(" A = new pass");
if(){
}
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = new pass");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
while(key == '#'){
mame();
}
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);
lcd.clear();
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("pass to unlock");
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
lcd.clear();
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Access Denied!");
showWaitScreen(1000);
lcd.clear();
}
}
void setup() {
lcd.begin(16, 2);
//init_icons(lcd);
lockServo.attach(SERVO_PIN);
Serial.begin(115200);
if (safeState.locked()) {
lock();
} else {
unlock();
}
chon();
}
void loop() {
if (safeState.locked()) {
mame();
} else {
chon();
}
}
void mame() {
lcd.setCursor(0, 0);
lcd.print(" # = to unlock");
lcd.setCursor(0, 1);
lcd.print("A =selectchannel");
auto key = keypad.getKey();
if (key != '#'&& key != 'A') {
key = keypad.getKey();
}
if (key == '#') {
safeLockedLogic();
}while (key == 'A'){
chon();
}
}
void safeUnlockedLogic1() {
lcd.setCursor(0, 0);
lcd.print(" # = enter pass");
lcd.setCursor(1, 1);
lcd.print(" A = new pass");
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = new pass");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode1();
}
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();
lock1();
showWaitScreen(100);
lcd.clear();
}
}
bool setNewCode1() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new pass1:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new pass1");
String confirmCode1 = inputSecretCode();
if (newCode.equals(confirmCode1)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Pass mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}