#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
/* sensor magnetico*/
const int sensorMag = 6;
int stateMag; // 0 cerrado - 1 abierto
const int ledMag = 7;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
RTC_DS1307 rtc;
/* 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;
void lock() {
/*lockServo.write(SERVO_LOCK_POS);*/
safeState.lock();
}
void unlock() {
/*lockServo.write(SERVO_UNLOCK_POS);*/
}
void showStartupMessage() {
lcd.setCursor(2, 0);
lcd.print("Bienvenido!");
delay(1000);
lcd.setCursor(0, 1);
String message = "CamuAlarma v 1.0";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
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("Ingresar codigo nuevo:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirmar");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Error");
lcd.setCursor(0, 1);
lcd.print(" NO Activada!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Desactivada!");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void safeUnlockedLogic() {
time();
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(2, 0);
lcd.print(" # Activar");
lcd.setCursor(15, 0);
lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print("A = codigo nuevo");
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();
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();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Activada! ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Codigo erroneo!");
showWaitScreen(1000);
}
}
void time() {
DateTime now = rtc.now();
lcd.home();
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
delay(1000);
lcd.noDisplay();
delay(1000);
lcd.display();
}
void checkSensorMag() {
stateMag = digitalRead(sensorMag);
if (stateMag == HIGH){
digitalWrite(ledMag, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println(stateMag);
}
else{
digitalWrite(ledMag, LOW); // turn the LED off by making the voltage LOW
Serial.println(stateMag);
}
}
void setup() {
Serial.begin(115200);
/* Sensor Magnetico*/
pinMode(sensorMag, INPUT);
pinMode(ledMag, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
if (! rtc.begin()) {
lcd.print("Couldn't find RTC");
Serial.flush();
abort();
}
// Init
lcd.init();
lcd.backlight();
init_icons(lcd);
showStartupMessage();
}
void loop() {
time();
checkSensorMag();
if (safeState.locked()) {
safeLockedLogic();
} else {
safeUnlockedLogic();
}
}