#include <Keypad.h>
#include "ui.h"
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 36, 37, 38, 39 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 32, 33, 34, 35 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char *passwdSavedInEEprom = "1234";
char bufferPwd[5];
uint8_t bufferIdx = 0;
struct {
bool pwdMode = false;
uint32_t timeout;
bool isPwdAccepted = false;
} pwdState;
bool checkPassword() { // checking password
if(strncmp(passwdSavedInEEprom, bufferPwd, 4) == 0)
pwdState.isPwdAccepted = true;
else
pwdState.isPwdAccepted = false;
return pwdState.isPwdAccepted;
}
void setup(){
Serial.begin(9600);
// Try playing with different debounceTime settings to see how it affects
// the number of times per second your loop will run. The library prevents
// setting it to anything below 1 millisecond.
keypad.setDebounceTime(20); // setDebounceTime(mS)
}
//bool mainAlarmState = false;
struct {
bool activated = false;
bool isElapsed = false;
uint32_t timeout;
} mainAlarmState;
void inputPwd(char k) {
bufferPwd[bufferIdx++] = k;
if (bufferIdx == 4) {
//bufferIdx = 0;
Serial.println(bufferPwd);
if (checkPassword()) {
Serial.println("password corretta");
mainAlarmState.activated = !mainAlarmState.activated;
if (mainAlarmState.activated == true) {
mainAlarmState.timeout = millis();
Serial.println("Alarm Activated");
} else {
Serial.println("Alarm Disactivated");
}
pwdState.pwdMode = false;
} else {
Serial.println("password errata");
bufferIdx = 0;
memset(bufferPwd, '\0', 5);
}
}
}
// lc = 106756 50ms
// lc = 105978 30ms
// lc = 103342 10ms
void passwdMangler(char key) {
if (pwdState.pwdMode == true) {
if(key) {
Serial.println(key);
inputPwd(key);
pwdState.timeout = millis();
} else if (millis() - pwdState.timeout >= 3000) {
pwdState.pwdMode = false;
Serial.println("timeout");
//bufferIdx = 0;
}
} else { // out of password mode
if (key == '#') {
pwdState.pwdMode = true;
pwdState.timeout = millis();
Serial.println("Enter Password: ");
bufferIdx = 0;
memset(bufferPwd, '\0', 5);
}
}
}
namespace Knote {
enum {C4=264, C4S=281, D4=293, D4S=316, E4=330, F4=352, F4S=371
, G4=396, G4S=422, A4=440, A4S=475, B4=495, C5=528
, C5S=562, D5=586, D5S=632
};
const uint16_t notes[] = { C4, C4S, D4, D4S
, E4, F4, F4S, G4
, G4S, A4, A4S, B4
, C5, C5S, D5, D5S
};
}
void loop() {
char key = keypad.getKey();
if (key) {
//Serial.println(keypad.key[0].kcode);
//noTone(2);
//Serial.println(key);
tone(2, Knote::notes[keypad.key[0].kcode], 10);
}
passwdMangler(key);
if (mainAlarmState.activated == true) {
if (mainAlarmState.isElapsed == false)
if (millis() - mainAlarmState.timeout > 3000) {
mainAlarmState.isElapsed = true;
Serial.println("Monitoring Input Sensor.");
}
if (mainAlarmState.isElapsed == true) {
; // monitora gli ingressi
}
}
}