#include <LiquidCrystal.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
// LCD pins
#define RS 8
#define E 6
#define D4 4
#define D5 5
#define D6 A0
#define D7 A1
LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
#define SS_PIN 10 // RFID SS (SDA) pin
#define RST_PIN 9 // RFID RST pin
#define BUTTON_PIN 2 // Push button pin
#define BUZZER_PIN 3 // Buzzer pin
#define SERVO_PIN 11 // Servo motor pin
#define RED_LED 12 // Red LED
#define GREEN_LED 13 // Green LED
Servo myServo;
MFRC522 rfid(SS_PIN, RST_PIN);
const int Cards = 5; // Number of authorized cards
const int uidSize = 4; // Size of each RFID UID in bytes
byte authorizedUIDs[Cards][uidSize] = {
{0xA1, 0xB2, 0xC3, 0xD4}, // Card 1
{0x11, 0x22, 0x33, 0x44}, // Card 2
{0xAA, 0xBB, 0xCC, 0xDD}, // Card 3
{0x12, 0x34, 0x56, 0x78}, // Card 4
{0xDE, 0xAD, 0xBE, 0xEF} // Card 5
};
bool doorUnlocked = false; // Door state
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
lcd.begin(16, 2); // Initialize 16x2 LCD
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
myServo.attach(SERVO_PIN);
lockDoor(); // Start in locked position
displayWelcomeMessage(); // Show welcome message at startup
}
void loop() {
scrollMessage("Please Scan the Card "); // Scroll message on LCD
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
if (isAuthorized(rfid.uid.uidByte)) {
unlockDoor();
} else {
accessDenied();
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
displayWelcomeMessage(); // Show welcome message after operation
}
if (digitalRead(BUTTON_PIN) == LOW) {
unlockDoor();
displayWelcomeMessage(); // Show welcome message after unlocking
}
}
// Function to display welcome message with scrolling text
void displayWelcomeMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome");
}
// Function to scroll a message on the second line
void scrollMessage(String message) {
static int position = 0;
int len = message.length();
lcd.setCursor(0, 1);
for (int i = 0; i < 16; i++) {
int charIndex = (position + i) % len; // Loop through the message
lcd.print(message[charIndex]);
}
position = (position + 1) % len; // Increment position
delay(300); // Scrolling speed
}
// Function to check if the scanned UID is authorized
bool isAuthorized(byte *scannedUID) {
for (int i = 0; i < Cards; i++) {
if (memcmp(scannedUID, authorizedUIDs[i], uidSize) == 0) {
return true; // Match found, authorized card
}
}
return false; // No match found
}
void unlockDoor() {
if (!doorUnlocked) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
digitalWrite(GREEN_LED, HIGH); // Green LED ON
digitalWrite(RED_LED, LOW); // Red LED OFF
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
myServo.write(90); // Unlock position
doorUnlocked = true;
delay(5000); // Delay before locking
lockDoor();
}
}
void lockDoor() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Locked");
digitalWrite(GREEN_LED, LOW); // Green LED turned off
digitalWrite(RED_LED, LOW); // Red LED turned off
myServo.write(0); // Lock position
doorUnlocked = false;
delay(1000);
}
void accessDenied() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
digitalWrite(RED_LED, HIGH); // Red LED ON
digitalWrite(GREEN_LED, LOW); // Green LED OFF
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
delay(500);
}