Untitled

 avatar
unknown
plain_text
10 months ago
2.6 kB
12
Indexable
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>

// RFID setup
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

// Servo setup
#define SERVO_PIN 3
Servo myservo;

// Buzzer setup
#define BUZZER_PIN 4

// LCD setup (16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Delays
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myservo.attach(SERVO_PIN);
  myservo.write(0);

  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  lcd.int();        // initialize LCD
  lcd.backlight();
  
  lcd.setCursor(0, 0);
  lcd.print("RFID Door Lock");
  lcd.setCursor(0, 1);
  lcd.print("Scan your card");
  
  Serial.println("Put your card to the reader...");
}

void loop() {
  // Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // Show UID on Serial Monitor
  Serial.print("UID tag :");
  String content = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  content.toUpperCase();

  // Check UID
  if (content.substring(1) == "69 C8 E2 2A") {   // <-- change this UID to your card
    Serial.println("Authorized access");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Welcome Home   ");
    lcd.setCursor(0, 1);
    lcd.print(" Have a Nice Day");

    // Buzzer: single beep
    digitalWrite(BUZZER_PIN, HIGH);
    delay(200);
    digitalWrite(BUZZER_PIN, LOW);
    
    myservo.write(70);
    delay(7500);
    myservo.write(0);
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Scan your card");
  }
  else {
    Serial.println("Access denied");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Access Denied  ");
    lcd.setCursor(0, 1);
    lcd.print(" Try Again...   ");

    // Buzzer: 3 quick beeps
    for (int i = 0; i < 3; i++) {
      digitalWrite(BUZZER_PIN, HIGH);
      delay(150);
      digitalWrite(BUZZER_PIN, LOW);
      delay(150);
    }

    delay(DENIED_DELAY);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Scan your card");
  }
}
Editor is loading...
Leave a Comment