#include <Keypad.h>
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SERVO_PIN 10
Servo doorServo;
LiquidCrystal_I2C lcd(0x27,16,2);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
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);
String password = "2005";
String input = "";
void setup() {
Serial.begin(9600);
doorServo.attach(SERVO_PIN);
doorServo.write(0); // Door locked
lcd.init();
lcd.backlight();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();
lcd.setCursor(0,0);
lcd.print("Enter Password");
}
void loop() {
char key = keypad.getKey();
if(key) {
input += key;
lcd.setCursor(0,1);
lcd.print(input);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,20);
oled.print("Input: ");
oled.print(input);
oled.display();
}
if(input.length() == 4) {
if(input == password) {
lcd.clear();
lcd.print("Door Open");
doorServo.write(90);
oled.clearDisplay();
oled.setCursor(0,20);
oled.print("Door OPEN");
oled.display();
delay(5000);
doorServo.write(0);
}
else {
lcd.clear();
lcd.print("Wrong Password");
oled.clearDisplay();
oled.setCursor(0,20);
oled.print("Access Denied");
oled.display();
delay(2000);
}
input = "";
lcd.clear();
lcd.print("Enter Password");
}
}