#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
// ตั้งค่า LCD I2C (Address ปกติ 0x27 หรือ 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ตั้งค่า Servo
Servo myServo;
const int servoPin = 9;
// ตั้งค่า Keypad (ใช้ขาไม่ซ้ำกัน)
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; // แถว
byte colPins[COLS] = {4, 3, 2, A3}; // คอลัมน์
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// รหัสผ่านที่ถูกต้อง
String password = "999#";
String inputPassword = "";
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
myServo.attach(servoPin);
myServo.write(0); // ตั้งค่า Servo ให้อยู่ที่ตำแหน่งล็อก
}
void loop() {
char key = keypad.getKey(); // อ่านค่าปุ่มที่กด
if (key) {
lcd.setCursor(inputPassword.length(), 1);
lcd.print('*'); // แสดง * เมื่อมีการกดปุ่ม
inputPassword += key;
if (key == '#') { // ตรวจสอบรหัสเมื่อกด #
lcd.clear();
if (inputPassword == password) {
lcd.setCursor(0, 0);
lcd.print("Access Granted");
myServo.write(90); // หมุน Servo ไปที่ตำแหน่งปลดล็อก
} else {
lcd.setCursor(0, 0);
lcd.print("Access Denied");
}
delay(5000); // รอ 5 วินาที
myServo.write(0); // กลับสู่ตำแหน่งล็อก
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
inputPassword = ""; // รีเซ็ตรหัสผ่าน
}
}
}