// 2022-09-22
// @haniefstudio
// Indobot Challenge : smart password with Keypad, LCD and LED
// load libraries
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// set params
LiquidCrystal_I2C LCD(0x27, 16, 2); // set lcd
const byte ROWS = 4; // Baris
const byte COLS = 4; // Kolom
char Keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {12, 13, 19, 18};
byte colPins[COLS] = {5, 4, 2, 15};
Keypad THE_KEYPAD = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
char KEY_PRESSED;
String KEY_INPUTED = "";
String CORRECT_PASSWORD = "54312";
int LED_PIN_CORRECT = 26;
int LED_PIN_WRONG = 27;
// # set params
void setup() {
LCD.init();
LCD.backlight();
pinMode(LED_PIN_CORRECT, OUTPUT);
pinMode(LED_PIN_WRONG, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN_CORRECT, LOW);
digitalWrite(LED_PIN_WRONG, LOW);
LCD.setCursor(0, 0);
LCD.print("Input Password");
KEY_PRESSED = THE_KEYPAD.getKey();
switch (KEY_PRESSED) {
case '0' ... '9' :
KEY_INPUTED = KEY_INPUTED + KEY_PRESSED;
break;
case '#' :
// check if password correct
LCD.setCursor(0, 1);
if (KEY_INPUTED == CORRECT_PASSWORD) {
LCD.print("Access Accepted!");
digitalWrite(LED_PIN_CORRECT, HIGH);
} else {
LCD.print("Access Denied!");
digitalWrite(LED_PIN_WRONG, HIGH);
}
delay(2000);
KEY_INPUTED = "";
LCD.clear();
break;
case '*' :
// reset
KEY_INPUTED = "";
LCD.clear();
break;
case 'A' ... 'D' :
// wrong button / not used button
LCD.setCursor(0, 1);
LCD.print("Wrong Button!");
digitalWrite(LED_PIN_WRONG, HIGH);
delay(2000);
LCD.clear();
break;
};
LCD.setCursor(0, 1);
LCD.print(KEY_INPUTED);
}