//Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo doorServo;
//Buttons
const int Button1 = 10;
const int Button2 = 9;
const int Button3 = 8;
//Exit button
const int ExitButton = 7;
//LED Lights
const int LEDGreen = 6;
const int LEDRed = 5;
const int LEDYellow = 4;
//Buzzer
const int Buzzer = 3;
//Servo
const int Servo = 2;
//Passcode
int Passcode[] = {1, 2, 3};
int UserCode[3];
int POS = 0;
//Room capacity counter
int INCount = 0;
const int MaxCapacity = 5;
void setup() {
lcd.init();
lcd.backlight();
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(ExitButton, INPUT_PULLUP);
pinMode(LEDRed, OUTPUT);
pinMode(LEDYellow, OUTPUT);
pinMode(LEDGreen, OUTPUT);
pinMode(Buzzer, OUTPUT);
doorServo.attach(Servo);
doorServo.write(0);
showIdleScreen();
}
void loop() {
if (digitalRead(Button1) == LOW) { record(1); delay(150); }
if (digitalRead(Button2) == LOW) { record(2); delay(150); }
if (digitalRead(Button3) == LOW) { record(3); delay(150); }
if (digitalRead(ExitButton) == LOW) {
handleExit();
delay(300);
}
}
void record(int val) {
UserCode[POS] = val;
lcd.setCursor(POS, 1);
lcd.print("*");
digitalWrite(LEDYellow, HIGH);
PlayTone(1000, 100);
delay(100);
digitalWrite(LEDYellow, LOW);
POS++;
if (POS == 3) {
checkPass();
POS = 0;
}
}
void checkPass() {
lcd.clear();
bool ok = true;
for (int i = 0; i < 3; i++) {
if (UserCode[i] != Passcode[i]) ok = false;
}
if (ok) {
if (INCount < MaxCapacity) {
INCount++;
lcd.setCursor(0, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Entering...");
digitalWrite(LEDGreen, HIGH);
successTone();
unlockDoor();
delay(2000);
lockDoor();
digitalWrite(LEDGreen, LOW);
} else {
lcd.setCursor(0, 0);
lcd.print("The Room is Full!");
lcd.setCursor(0, 1);
lcd.print("Try Again Later");
digitalWrite(LEDRed, HIGH);
errorTone();
delay(1500);
digitalWrite(LEDRed, LOW);
}
} else {
lcd.setCursor(0, 0);
lcd.print("Access Denied");
digitalWrite(LEDRed, HIGH);
errorTone();
delay(1000);
digitalWrite(LEDRed, LOW);
}
showIdleScreen();
}
void handleExit() {
if (INCount > 0) INCount--;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Have a Nice Day!");
lcd.setCursor(0, 1);
lcd.print("Exiting...");
digitalWrite(LEDYellow, HIGH);
exitTone();
unlockDoor();
delay(2000);
lockDoor();
digitalWrite(LEDYellow, LOW);
showIdleScreen();
}
void showIdleScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Passcode");
lcd.setCursor(0, 1);
lcd.print("IN: ");
lcd.print(INCount);
lcd.print("/");
lcd.print(MaxCapacity);
}
//Tone Functions
//Normal Tone
void PlayTone(int freq, int duration) {
tone(Buzzer, freq, duration);
delay(duration);
noTone(Buzzer);
}
//Success Tone (Access Granted)
void successTone() {
PlayTone(1200, 150);
delay(50);
PlayTone(1500, 150);
delay(50);
PlayTone(1800, 200);
}
//Error Tone (Access Denied/Room Full)
void errorTone() {
PlayTone(400, 300);
delay(100);
PlayTone(400, 300);
}
//Exit Tone (Exiting)
void exitTone() {
PlayTone(800, 150);
delay(50);
PlayTone(600, 200);
}
// Unlock
void unlockDoor() {
doorServo.write(90);
}
// Lock
void lockDoor() {
doorServo.write(0);
}