#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const int ROWS = 4;
const int COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int cursorColumn = 0;
int passwordIndex = 0;
int enteredPassword[4];
int correctPassword[4] = {4,4,4,4};
void setup() {
lcd.init();
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
lcd.setCursor(cursorColumn, 0);
lcd.print(key);
enteredPassword[passwordIndex] = key - '0';
passwordIndex++;
cursorColumn++;
if (cursorColumn == 4) {
// ตรวจสอบรหัสผ่านที่ป้อนเมื่อเต็ม 4 หลัก
if (checkPassword()) {
lcd.clear();
lcd.print("Nai");
delay(9000);
// ทำอย่างอื่นที่คุณต้องการเมื่อรหัสผ่านถูกต้อง
} else {
lcd.clear();
lcd.print("Password incorrect");
// ทำอย่างอื่นที่คุณต้องการเมื่อรหัสผ่านไม่ถูกต้อง
}
delay(2000); // หน่วงเวลา 2 วินาที
lcd.clear();
cursorColumn = 0;
passwordIndex = 0;
}
}
}
bool checkPassword() {
for (int i = 0; i < 4; i++) {
if (enteredPassword[i] != correctPassword[i]) {
return false;
}
}
return true;
}