/**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
/* Locking mechanism definitions */
#define SERVO_PIN 6
#define SERVO_LOCK_POS 0
#define SERVO_UNLOCK_POS 60
//#define BUZZER_PIN A0
#define buzzer A0
//
Servo lockServo;
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 3;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
String master_pass = "2465";
byte backlightPin=13; //pin to power LCD backlight (pin 13)
byte ledRedPin=1; //pin to power LED RED
byte ledGreenPin=0; //pin to power LED GREEN
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void showStartupMessage() {
lcd.setCursor(6, 0);
lcd.print("Salut!");
delay(200);
lcd.setCursor(2, 2);
String message = "ArduinoSafe v1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(600);
}
String inputSecretCode() {
lcd.setCursor(7, 1);
lcd.print("[____]");
lcd.setCursor(8, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
tone(buzzer, 200, 25);
delay(25);
noTone(buzzer);
}
}
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("=");
}
}
void showWaitScreenLocking() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("[LOCK in 5 sec]");
lcd.setCursor(1, 1);
for (byte i = 1; i < 7; i++) {
delay(1000);
lcd.print(i);
}
}
void flashScreen(int times = 6, int sdelay = 200){
for(int i=0;i<times;i++){ //for loop 10 times
digitalWrite(backlightPin,HIGH); //turn on backlight
tone(buzzer, 150, 25);
delay(sdelay); // wait half a second
digitalWrite(backlightPin,LOW); //turn off backlight
noTone(buzzer);
delay(sdelay); // wait half a second
}
digitalWrite(backlightPin,HIGH); //turn on backlight
}
bool setNewCode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Introdu Cod Nou:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirma Cod Nou");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("! Nepotrivire Cod !");
lcd.setCursor(2, 1);
lcd.print("Seiful nu este");
lcd.setCursor(5, 2);
lcd.print("BLOCAT!!!");
flashScreen();
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("DESCHIS!");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 0);
lcd.print("# INCHIDE SEIFUL");
lcd.setCursor(19, 0);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (!safeState.locked() && !newCodeNeeded) {
delay(5000);
showWaitScreenLocking();
lock();
}
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print("* = Schimba Codul");
newCodeNeeded = false;
}
digitalWrite(ledGreenPin,HIGH); //turn on LED
digitalWrite(ledRedPin,LOW); //turn off LED
auto key = keypad.getKey();
while (key != '*' && key != '#') {
key = keypad.getKey();
if(key){
tone(buzzer, 200, 25);
delay(25);
noTone(buzzer);
}
}
bool readyToLock = true;
if (key == '*' || newCodeNeeded) {
readyToLock = setNewCode();
}
bool auto_lock = true;
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();
auto_lock = false;
lcd.setCursor(2, 2);
lcd.print("SEIFUL SE INCHIDE");
showWaitScreen(100);
}
}
void safeLockedLogic() {
digitalWrite(ledGreenPin,LOW); //turn on LED
digitalWrite(ledRedPin,HIGH); //turn off LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Seif INCHIS! ");
lcd.setCursor(18, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.write(ICON_LOCKED_CHAR);
lcd.setCursor(0, 3);
lcd.write(ICON_LOCKED_CHAR);
lcd.write(ICON_LOCKED_CHAR);
lcd.setCursor(18, 3);
lcd.write(ICON_LOCKED_CHAR);
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
lcd.setCursor(2, 2);
lcd.print("SEIFUL SE DESCHIDE");
showWaitScreen(200);
if (unlockedSuccessfully || master_pass == userCode) {
if(master_pass == userCode){
safeState.setLock(false);
}
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("!ACCES INTERZIS!");
lcd.setCursor(2, 1);
lcd.print("!ACCES INTERZIS!");
lcd.setCursor(2, 2);
lcd.print("!ACCES INTERZIS!");
lcd.setCursor(2, 3);
lcd.print("!ACCES INTERZIS!");
flashScreen();
}
}
void setup() {
pinMode(backlightPin,OUTPUT); // set backlightPin to OUTPUT mode
pinMode(ledRedPin,OUTPUT); // set backlightPin to OUTPUT mode
pinMode(ledGreenPin,OUTPUT); // set backlightPin to OUTPUT mode
pinMode(buzzer, OUTPUT);
digitalWrite(backlightPin,HIGH); //turn on backlight
digitalWrite(ledGreenPin,HIGH); //turn on LED
digitalWrite(ledRedPin,HIGH); //turn off LED
lcd.begin(20, 4);
init_icons(lcd);
lockServo.attach(SERVO_PIN);
/* Make sure the physical lock is sync with the EEPROM state */
//Serial.begin(115200); // comment to use pins 0 and 1
if (safeState.locked()) {
lock();
} else {
unlock();
}
showStartupMessage();
}
void loop() {
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}