#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int lcdCols = 16;
const int lcdLines = 2;
const byte ROWS = 4;
const byte COLS = 4;
const int len_key = 6;
char master_key[len_key] = {'1','2','3','4','5', '6'};
char attempt_key[len_key];
int colCursor;
unsigned long lastMillis;
const int LED_OUTPUT = 15;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {14, 27, 26, 25};
byte colPins[COLS] = {33, 32, 23, 12};
LiquidCrystal_I2C lcd(0x27, lcdCols, lcdLines);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
pinMode(LED_OUTPUT, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == 'A') {
lastMillis = millis();
passCodeInit();
passCode();
lcd.clear();
return;
}
else if (key == '#') {
lcd.clear();
return;
}
}
}
void passCode()
{
while (millis() - lastMillis < 30000)
{
char key = keypad.getKey();
lcd.setCursor(5+colCursor, 1);
if (key)
{
switch(key)
{
case '*':
passCodeInit();
break;
case '#':
delay(100); // added debounce
checkKEY();
break;
default:
if (colCursor < len_key)
{
lcd.print("*");
attempt_key[colCursor]=key;
colCursor++;
}
}
}
}
}
void checkKEY()
{
lcd.setCursor(0,1);
for (int i=0; i<len_key; i++)
{
if (attempt_key[i]!=master_key[i])
{
lcd.print("---Incorrect!---");
delay(1500);
passCodeInit();
return;
}
}
lcd.print("----Correct!----");
digitalWrite(LED_OUTPUT, HIGH);
delay(3000);
passCodeInit();
}
void passCodeInit() {
colCursor=0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password: ");
digitalWrite(LED_OUTPUT, LOW);
}