#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define row_num 4
#define col_num 4
char keys[row_num][col_num]={
{'1', '2', '3', 'A' },
{'4', '5', '6', 'B' },
{'7', '8', '9', 'C' },
{'*', '0', '#', 'D' }
};
byte row_pins[row_num]={19,18,5,17};
byte col_pins[col_num]={16,4,2,15};
String mypassword="1234";
String input="";
LiquidCrystal_I2C lcd(0x27,20,4);
Keypad mykeypad= Keypad(makeKeymap(keys),row_pins,col_pins,row_num,col_num);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(5,0);
lcd.print("KeyPad");
Serial.print("Enter the password: ");
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
char key= mykeypad.getKey();
if(key){
Serial.print(key);
lcd.clear();
lcd.setCursor(5,0);
lcd.print(String(key));
if(key =='#'){
Serial.println();
if(input.compareTo(mypassword) == 0){
Serial.println("Access granted...");
while(true);
}
else{
Serial.println("Access denined...");
Serial.print("Try again!...");
}
input="";
}
else if(key == '*'){
input="";
Serial.println();
Serial.print("Password is cleared,enter again..");
}
}
else{
input.concat(key);
// Serial.print(key);
}
}