//https://miliohm.com/keypad-with-arduino-tutorial/
#include <Keypad.h>
#include <LiquidCrystal.h>
#define led_pin 1//to unlock and lock
// initialize the LCD
LiquidCrystal lcd(A5, 2, 3, 4, 5, 6);
String pad;
const byte numRows = 4; // set display to 4 rows
const byte numCols = 3; // set display to 3 columns
String password = "4321";
char keypressed;
char keymap[numRows][numCols] =
{{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
//------------------------------------------------------------
byte rowPins[numRows] = {7, 8, 9, 10};
byte colPins[numCols] = {11, 12, 13};
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad
void setup () {
lcd.begin(16, 2);
lcd.print("Enter Number:");
delay(3000);
lcd.clear();
lcd.cursor();
lcd.blink();
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, HIGH);
delay(1000);
digitalWrite(led_pin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
readKeypad();
if (keypressed == '#') {
if (pad == password) {
lcd.setCursor(0, 1);
lcd.print("Access Granted");
lcd.noBlink();
digitalWrite(led_pin, HIGH);
delay(2000);
lcd.setCursor(0, 0);
pad = "";
lcd.clear();
} else {
lcd.setCursor(0, 1);
lcd.print("Access Denied");
digitalWrite(led_pin, LOW);
lcd.noBlink();
delay(1000);
lcd.setCursor(0, 0);
pad = "";
lcd.clear();
}
} if (keypressed == '*') {
pad = "";
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print(pad);
delay(100);
}
void readKeypad() {
keypressed = myKeypad.getKey(); //deteksi penekanan keypad
if (keypressed != '#') {
String konv = String(keypressed);
pad += konv;
}
}