#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
#define I2C_ADD 0X27
#define LCD_COL 16
#define LCD_ROW 2
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const uint8_t pinRows[ROWS] = {4,5,6,7};
const uint8_t pinCols[COLS] = {8,9,10,11};
LiquidCrystal_I2C lcd(I2C_ADD, LCD_COL, LCD_ROW);
Servo myservo;
Keypad mykey = Keypad(makeKeymap(keys),pinRows,pinCols,ROWS,COLS);
String input = "";
const String passcode = "1234";
void setup() {
// Serial.begin(9600);
// while (!Serial) {
// ; // Wait for serial port to connect. Needed for native USB
// }
// Serial.println("Serial communication started");
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Enter Code: ");
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
myservo.attach(3);
myservo.write(0);
// Serial.println("Setup completed");
}
void loop() {
char key = mykey.getKey();
if (key != NO_KEY) {
// Serial.print("Key pressed: ");
// Serial.println(key);
if (key == '#') {
// Serial.println("Enter key pressed");
if (input.length() > 0) {
if (input == passcode) {
// Serial.println("Access granted");
grantAccess();
} else {
// Serial.println("Access denied");
denyAccess();
}
}
} else if (key == '*') {
// Serial.println("Clear key pressed");
clearInput();
} else {
input += key;
updateDisplay();
// Serial.print("Current input: ");
// Serial.println(input);
}
}
}
void grantAccess(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Access Granted!!");
lcd.setCursor(0,1);
lcd.print("Door Opening..");
myservo.write(90);
digitalWrite(12,HIGH);
delay(5000);
myservo.write(0);
lcd.setCursor(0,1);
lcd.print("Door Closing..");
delay(3000);
digitalWrite(12,LOW);
resetSystem();
}
void denyAccess(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Access Denied!");
digitalWrite(13,HIGH);
delay(2000);
digitalWrite(13, LOW);
resetSystem();
}
void clearInput(){
input = "";
updateDisplay();
}
void updateDisplay(){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(input);
}
void resetSystem() {
input = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Code:");
updateDisplay();
// Serial.println("System reset");
}