#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Variable
bool passc = true;
bool ins = false;
const unsigned long durata = 5000;
long noRetro = 0;
unsigned long startTime = 0;
bool condizione;
bool reset;
bool canSound = false;
char key;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
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}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
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 (key){
noRetro = millis();
lcd.backlight();
tempPassword[tempPasswordIndex] = key; //reading password
tempPasswordIndex++;
lcd.setCursor(tempPasswordIndex - 1,1);
lcd.print(F("*"));
tone(1,1200);
delay(20);
noTone(1);
if(tempPasswordIndex>4) //4 char for 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"));
passc = false;
reset = false;
condizione = false;
delay(500);
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(){ // checking 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(ins == false && reset == 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 setup() {
pinMode(13,INPUT);
pinMode(12,INPUT);
pinMode(11,OUTPUT);
pinMode(1,OUTPUT);
lcd.init(); //Init with pin default ESP8266 or ARDUINO
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();
}
if (key == '#' && ins == false){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
passc = true;
while(passc){
pass();
controllo();
retro();
}
lcd.clear();
lcd.setCursor(1,0);
lcd.print(F("Inserimento..."));
//delay(5000);
noRetro = millis();
ins = true;
lcd.clear();
}
else if (key == '#' && ins == true){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Password"));
passc = true;
while(passc){
pass();
retro();
canSound = false;
noTone(1);
}
ins = false;
}
if (digitalRead(12) == HIGH && reset == false) {
startTime = millis(); // Resetta il timer se la condizione diventa falsa
}
}