/*
Forum: https://forum.arduino.cc/t/the-lcd-screen-doesnt-show-the-input-from-the-keypad/1232826
Wokwi: https://wokwi.com/projects/391735173918223361
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
// RS, E, D4 ..D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char timeobj;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {3, 2, A4, A5};
int codiceSegreto = 0;
int tempoTimer = 0;
bool codiceInserito = false;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("Inserisci codice:");
}
void loop() {
char key = keypad.getKey();
if (!codiceInserito) {
inserisciCodice(key);
} else {
gestisciTimer(key);
}
}
void inserisciCodice(char key) {
static String inputCode = "";
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
inputCode += key;
lcd.print(key);
}
if (key == '#') {
codiceSegreto = inputCode.toInt();
inputCode = "";
Serial.print("codiceSegreto\t");
Serial.println(codiceSegreto);
lcd.clear();
lcd.print("Inserisci tempo:");
}
if (key == '*') {
tempoTimer = inputCode.toInt();
codiceInserito = true;
lcd.clear();
lcd.print("Codice e tempo inseriti!");
Serial.print("tempoTimer\t");
Serial.println(tempoTimer);
delay(2000);
lcd.clear();
}
}
}
void gestisciTimer(char tempotimer)
{
int time;
time=atoi(tempotimer);
Serial.print("timer\t");
Serial.println(time);
if (tempoTimer > 0) {
lcd.print(time);
delay(500);
lcd.clear();
delay(1000);
time--;
}
if (tempoTimer == 0) {
lcd.clear();
lcd.print("Tempo scaduto!");
}
}