#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#define buzzer 12
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ===== Keypad setup =====
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const unsigned long NO_KEY_DEBOUNCE_MX = 1000;
unsigned loong noKeyStartMs = 0;
bool noKeyTiming = false
int maxinput = 4;
int lengthofpw = 4;
char password[4] = {'1','3','7','9'};
int currentkeyposition = 0;
char key[4] = {'0'};
void buzz1time(){
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer,LOW);
delay(200);
}
void buzz3times(){
for (int i = 0; i < 3; i=i+1){
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(300);
}
}
void cursororigin(){
lcd.clear();
lcd.setCursor(0,0);
}
void lockdoor(){
servo.write(110);
lcd.setCursor(2,1);
lcd.print("DOOR LOCKED");
buzz3times();
delay(2000);
lcd.clear();
}
void unlockdoor(){
servo.write(-110);
lcd.setCursor(2,1);
lcd.print("DOOR UNLOCKED");
buzz1time();
delay(2000);
lcd.clear();
}
void processnumberkey(char key){
int a=1;
lcd.setCursor(a, 1);
lcd.print("*");
a=a+1;
}
void pressedkeys(char keys){
if (currentkeyposition<maxinput){
key[currentkeyposition] = keys;
currentkeyposition=currentkeyposition+1;
}
}
bool checkpassword(){
int z = 0;
for (int i = 0; i < lengthofpw; i=i+1){
if (key[i] == password[i]){
z=z+1;
}
}
if (z == 4){
unlockdoor();
}
else{
lcd.print("WRONG PASSWORD");
buzz3times();
delay(2000);
lcd.clear();
}
}
void setup() {
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
pinMode(buzzer,OUTPUT);
}
void loop() {
char keys = keypad.getKey();
}