#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define Bz 15
#define Servopin 23
Servo servo1;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[4][4] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 16, 17, 18, 19 };
uint8_t rowPins[ROWS] = { 12, 13, 14, 27 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char inputPassword[4];
uint8_t inputIndex = 0;
const char correctPassword[4] = {'1', '2', '3', '4'};
const int ledPin = 25;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("Please Enter");
lcd.setCursor(1,1);
lcd.print("Password : ");
lcd.setCursor(1,2);
lcd.print ("Status :");
pinMode(ledPin, OUTPUT);
pinMode(Bz, OUTPUT);
digitalWrite(ledPin, LOW);
servo1.attach(Servopin);
servo1.write(0);
}
void loop() {
password();
}
void password(){
char key = keypad.getKey();
if (key) {
if (inputIndex < 4) {
inputPassword[inputIndex] = key;
inputIndex++;
Serial.print("Current Input: ");
for (uint8_t i = 0; i < inputIndex; i++) {
Serial.print(inputPassword[i]);
lcd.setCursor(12,1);
lcd.print(inputPassword[0]);
lcd.setCursor(13,1);
lcd.print(inputPassword[1]);
lcd.setCursor(14,1);
lcd.print(inputPassword[2]);
lcd.setCursor(15,1);
lcd.print(inputPassword[3]);
}
}
Serial.println();
}
if (inputIndex == 4) {
Serial.print("Password Entered: ");
for (uint8_t i = 0; i < 4; i++) {
Serial.print(inputPassword[i]);
}
Serial.println();
if (memcmp(inputPassword, correctPassword, 4) == 0) {
Serial.println("เก่ง เก่ง เก่ง");
digitalWrite(ledPin, HIGH);
lcd.setCursor(11,2);
lcd.print ("Pass ");
tone(2500,250);
servo1.write(120);
} else {
Serial.println("เเม่มมมมม");
digitalWrite(ledPin, LOW);
lcd.setCursor(11,2);
lcd.print ("Nopass");
}
inputIndex = 0;
}
}