#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const String password = "1234"; // 설정할 비밀번호
String inputPassword = ""; // 입력된 비밀번호 저장
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C 주소 확인 필요!
const byte ROWS = 4; // 4행
const byte COLS = 4; // 4열
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // 행 핀 설정
byte colPins[COLS] = {6, 7, 8, 9}; // 열 핀 설정
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter PIN:");
}
void loop() {
char key = keypad.getKey(); // 키패드 입력 받기
if (key) { // 키 입력이 있으면 실행
if (key == '#') { // '#' 키를 누르면 입력된 값 확인
lcd.clear();
if (inputPassword == password) {
lcd.setCursor(0, 0);
lcd.print("Access Granted!"); // 비밀번호 일치
} else {
lcd.setCursor(0, 0);
lcd.print("Access Denied!"); // 비밀번호 불일치
}
delay(2000); // 2초 동안 메시지 표시
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter PIN:");
inputPassword = ""; // 입력 초기화
} else if (key == '*') { // '*' 키를 누르면 초기화
inputPassword = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter PIN:");
} else {
if (inputPassword.length() < 4) { // 4자리까지만 입력 가능
inputPassword += key;
lcd.setCursor(inputPassword.length(), 1);
lcd.print('*'); // 입력값 가리기
}
}
}
}