#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Variable
bool passc = true;
bool ins = false;
bool home = true;
bool man = false;
bool almMan = false;
bool passmanc = false;
bool hardReset = false;
const unsigned long durata = 15000;
const unsigned long durataIns = 10000;
long noRetro = 0;
unsigned long startTime = 0;
long tempIns = 0;
bool condizione;
bool reset;
bool canSound = false;
char key;
const byte ROWS = 4; //4 righe
const byte COLS = 4; //4 colonne
char keys[ROWS][COLS] = {
{'1','2','3','a'},
{'4','5','6','b'},
{'7','8','9','c'},
{'*','0','#','d'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //pin righe
byte colPins[COLS] = {5, 4, 3, 2}; //pi colonne
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char password[5] = "b030";//password
char tempPassword[5];
int tempPasswordIndex = 0;
void pass(){
key = keypad.getKey();
if (ins){
scatto();
scattopre();
}
if (millis() - tempIns >= durataIns || key == '#') {
passc = false;
home = false;
hardReset = false;
lcd.clear();
for(int i; i >= 5; i++){
tempPassword[i] = "";
}
tempPasswordIndex = 0;
}
else if (key){
noRetro = millis();
lcd.backlight();
tempPassword[tempPasswordIndex] = key; //legge password
tempPasswordIndex++;
lcd.setCursor(tempPasswordIndex - 1,1);
lcd.print(F("*"));
tone(1,1200);
delay(20);
noTone(1);
if(tempPasswordIndex>4) //4 caratteri per la password
{
tempPasswordIndex = 0;
if(checkPassword()) //if true...
{
lcd.clear();
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
delay(500);
lcd.setCursor(3,0);
lcd.print(F("Password V"));
delay(500);
passc = false;
reset = false;
condizione = false;
if (hardReset){
lcd.clear();
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
lcd.setCursor(4, 0);
lcd.print(F("Reset"));
delay(150);
lcd.print(F("."));
tone(1, 1200);
delay(50);
noTone(1);
delay(250);
lcd.print(F("."));
tone(1, 1200);
delay(50);
noTone(1);
delay(250);
lcd.print(F("."));
tone(1, 1200);
delay(200);
digitalWrite(0,HIGH);
}
lcd.clear();
}
else //if false...
{
lcd.clear();
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
delay(500);
lcd.setCursor(3,0);
lcd.print(F("Password X"));
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
}
}
}
delay(50);
}
//----------------------------------------
void passman(){
key = keypad.getKey();
if (ins){
scatto();
scattopre();
}
if (millis() - tempIns >= durataIns || key == '#') {
passmanc = false;
lcd.clear();
for(int i; i >= 5; i++){
tempPassword[i] = "";
}
tempPasswordIndex = 0;
}
else if (key){
noRetro = millis();
lcd.backlight();
tempPassword[tempPasswordIndex] = key; //legge password
tempPasswordIndex++;
lcd.setCursor(tempPasswordIndex - 1,1);
lcd.print(F("*"));
tone(1,1200);
delay(20);
noTone(1);
if(tempPasswordIndex>4) //4 caratteri per password
{
tempPasswordIndex = 0;
if(checkPassword()) //if true...
{
lcd.clear();
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
delay(500);
lcd.setCursor(3,0);
lcd.print(F("Password V"));
delay(500);
passmanc = false;
ins = false;
if (almMan){
almMan = false;
}
man = !man;
lcd.clear();
}
else //if false...
{
lcd.clear();
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
delay(500);
lcd.setCursor(3,0);
lcd.print(F("Password X"));
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
}
}
}
delay(50);
}
boolean checkPassword(){ // controllo password
if(strncmp(password, tempPassword, 4) == 0)
{
return true;
}
else {
return false;
}
}
void controllo(){
lcd.setCursor(14, 0);
if(digitalRead(13) == HIGH){
lcd.print(F("V"));
}
else if(digitalRead(13) == LOW){
lcd.print(F("X"));
}
lcd.setCursor(15, 0);
if(digitalRead(12) == HIGH){
lcd.print(F("V"));
}
else if(digitalRead(12) == LOW){
lcd.print(F("X"));
}
}
void retro(){
if (digitalRead(11) == LOW){
if (millis() - noRetro >= 5000) {
lcd.noBacklight();
}
}
else{
lcd.backlight();
}
}
void stato(){
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
if (man){
lcd.setCursor(13,0);
lcd.print(F("MAN"));
}
else{
scattoman();
}
if(ins == false && reset == false){
if (almMan == false){
digitalWrite(11,LOW);
}
lcd.setCursor(0,0);
lcd.print(F("OFF"));
}
else{
lcd.setCursor(0,0);
lcd.print(F("ON"));
scatto();
scattopre();
}
}
void scatto(){
if(digitalRead(13) == LOW){
lcd.setCursor(13,0);
lcd.print(F("ALM"));
digitalWrite(11,HIGH);
}
delay(20);
}
void scattopre(){
if (condizione == false){
condizione = digitalRead(12) == LOW;
canSound = true;
}
if (condizione) {
if (canSound){
tone(1, 1200);
delay(50);
noTone(1);
delay(50);
}
reset = true;
if (startTime == 0) {
startTime = millis(); // Memorizza il tempo di inizio
}
if (millis() - startTime >= durata) {
lcd.setCursor(13,0);
lcd.print(F("ALM"));
digitalWrite(11,HIGH);
canSound = false;
noTone(1);
}
}
}
void scattoman(){
if(digitalRead(10) == LOW){
lcd.setCursor(13,0);
lcd.print(F("ALM"));
almMan = true;
digitalWrite(11,HIGH);
}
delay(20);
}
void setup() {
pinMode(13,INPUT);
pinMode(12,INPUT);
pinMode(10,INPUT);
pinMode(11,OUTPUT);
pinMode(1,OUTPUT);
pinMode(0,OUTPUT);
lcd.init(); //inizializza lcd
lcd.backlight(); //accende la retroilluminazione
delay(700);
lcd.noBacklight();
delay(700);
lcd.backlight();
delay(700);
lcd.setCursor(4, 1);
lcd.print(F("DifAlarm"));
delay (1000);
lcd.setCursor(0, 0);
lcd.print(F("Inizializzazione"));
delay (1500);
lcd.clear();
noRetro = millis();
}
void loop(){
stato();
retro();
char key = keypad.getKey();
if(key){
noRetro = millis();
lcd.backlight();
tempIns = millis();
}
if (key == '#' && ins == false && man == false && almMan == false){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
passc = true;
home = true;
while(passc){
pass();
controllo();
retro();
}
if (home){
lcd.clear();
lcd.setCursor(1,0);
startTime = millis();
lcd.print(F("Inserimento..."));
delay(15000);
noRetro = millis();
startTime = millis();
ins = true;
lcd.clear();
}
else{lcd.clear();}
}
else if (key == '#' && ins == true && man == false){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
passc = true;
home = true;
while(passc){
pass();
retro();
canSound = false;
noTone(1);
}
if (home){
ins = false;
}
}
if (key == '*'){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
passmanc = true;
while(passmanc){
passman();
retro();
}
}
if(key == 'd'){
hardReset = true;
passc = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
while(passc){
pass();
}
}
if (digitalRead(12) == HIGH && reset == false) {
startTime = millis(); // Resetta il timer se la condizione diventa falsa
}
}