#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const byte rows=4;
const byte cols=4;
// các phím
char key[rows][cols]={
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
// cac chan arrduino
byte colPins[cols]={5,4,3,2};
byte rowPins[rows]={9,8,7,6};
Keypad keypad= Keypad(makeKeymap(key),rowPins ,colPins ,rows,cols);
const String password = "1234"; // có thể thay đổi password
String input_password;
int count = 1;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Nhap mat khau: ");
pinMode(10, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if (key){
if(key == '*') {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap mat khau: ");
input_password = "";
count =1;
} else if(key == '#') {
count = 1;
if(password == input_password) {
lcd.setCursor(2,1);
lcd.print(" dung");
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10,LOW);
} else {
lcd.setCursor(2,1);
lcd.print(" sai");
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10,LOW);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nhap mat khau: ");
}
input_password = "";
} else {
input_password += key;
count++;
lcd.setCursor(count,1);
lcd.print("*");
}
}
}