#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// --- Pin Definitions Based on Diagram ---
#define RST_PIN 9
#define SS_PIN 10
#define BUZZER_PIN 3
#define LED_GREEN 2
#define LED_RED 4
// Initialize LCD (Address 0x27 is standard for 16x2 I2C displays)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize RFID Reader
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Example authorized UID (This matches the default Wokwi custom card UID format)
byte authorizedUID[] = {0x11, 0x22, 0x33, 0x44};
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
// Initialize LCD
lcd.init();
lcd.backlight();
displayIdleMessage();
// Initialize Indicators
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, LOW);
}
void loop() {
// Reset look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Check UID match against authorizedUID array
bool accessGranted = true;
// Verify that the scanned card size matches your target key size (typically 4 bytes)
if (mfrc522.uid.size == sizeof(authorizedUID)) {
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != authorizedUID[i]) {
accessGranted = false; // A byte doesn't match, deny access
break;
}
}
} else {
accessGranted = false; // Card format mismatch
}
if (accessGranted) {
grantAccess();
} else {
denyAccess();
}
mfrc522.PICC_HaltA();
displayIdleMessage();
}
void displayIdleMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" ASTRO PASS ");
lcd.setCursor(0, 1);
lcd.print(" SMART RFID ");
}
void grantAccess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ACCESS GRANTED ");
lcd.setCursor(0, 1);
lcd.print(" WELCOME ABOARD ");
digitalWrite(LED_GREEN, HIGH);
// Successful access chirp
digitalWrite(BUZZER_PIN, HIGH);
delay(150);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
digitalWrite(BUZZER_PIN, HIGH);
delay(150);
digitalWrite(BUZZER_PIN, LOW);
delay(2000); // Leave gate simulated open
digitalWrite(LED_GREEN, LOW);
}
void denyAccess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" ACCESS DENIED ");
lcd.setCursor(0, 1);
lcd.print("INVALID BADGE ID");
digitalWrite(LED_RED, HIGH);
// Long rejection beep
digitalWrite(BUZZER_PIN, HIGH);
delay(800);
digitalWrite(BUZZER_PIN, LOW);
delay(1500);
digitalWrite(LED_RED, LOW);
}