Untitled

 avatar
unknown
plain_text
10 months ago
3.6 kB
9
Indexable
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define servoPin 2

String UID = "C3 29 C8 A9"; // Enter your authorized card/tag ID

MFRC522 rfid(SS_PIN, RST_PIN);
Servo myservo;

int IR1 = 4; // Entry sensor
int IR2 = 3; // Exit sensor

int Slot = 4; // Total number of parking Slots
const int MaxSlots = 4; // Maximum number of slots

bool carEntering = false; // Track if a car is entering
bool carExiting = false; // Track if a car is exiting

void setup() {
  Serial.begin(9600);
  pinMode(IR1, INPUT);
  pinMode(IR2, INPUT);
  myservo.attach(servoPin);
  myservo.write(100); // Close the gate initially
  Serial.println("=== ARDUINO PARKING SYSTEM ===");

  SPI.begin();
  rfid.PCD_Init();
}

void loop() {
  // Check if a car is entering (IR1 -> IR2)
  if (digitalRead(IR1) == LOW && !carEntering && !carExiting) {
    carEntering = true;
    Serial.println("Car Detected at Entry!");

    if (Slot > 0) {
      int pass = rfunc(); // Scan RFID card
      if (pass == 1) {
        smoothOpenGate(); // Open the gate smoothly
        Slot--; // Decrement available slots
        Serial.println("Gate Opened!");

        // Wait for the car to pass through IR2
        while (digitalRead(IR2) == HIGH) { delay(100); }
        while (digitalRead(IR2) == LOW) { delay(100); }

        delay(2000); // Wait 2 seconds before closing the gate
        smoothCloseGate(); // Close the gate smoothly
        Serial.println("Gate Closed!");
      }
    } else {
      Serial.println("SORRY :( Parking Full");
    }
    carEntering = false;
  }

  // Check if a car is exiting (IR2 -> IR1)
  if (digitalRead(IR2) == LOW && !carExiting && !carEntering) {
    if (Slot == MaxSlots) {
      Serial.println("No Cars Left!");
    } else {
      carExiting = true;
      smoothOpenGate(); // Open the gate smoothly
      if (Slot < MaxSlots) {
        Slot++; // Increment available slots
      }
      Serial.println("Car Exiting...");

      // Wait for the car to pass through IR1
      while (digitalRead(IR1) == HIGH) { delay(100); }
      while (digitalRead(IR1) == LOW) { delay(100); }

      delay(2000); // Wait 2 seconds before closing the gate
      smoothCloseGate(); // Close the gate smoothly
      Serial.println("Gate Closed!");
      carExiting = false;
    }
  }

  // Display available slots
  Serial.print("Available Slots: ");
  Serial.println(Slot);
  delay(1000);
}

// Function to smoothly open the gate
void smoothOpenGate() {
  for (int pos = 100; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(10);
  }
}

// Function to smoothly close the gate
void smoothCloseGate() {
  for (int pos = 0; pos <= 100; pos += 1) {
    myservo.write(pos);
    delay(10);
  }
}

// Function to scan RFID card and check authorization
int rfunc() {
  Serial.println("Put your card to scan...");
  while (true) {
    if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
      break;
    }
    delay(300);
  }

  String ID = "";
  for (byte i = 0; i < rfid.uid.size; i++) {
    if (rfid.uid.uidByte[i] < 0x10) ID += "0";
    ID += String(rfid.uid.uidByte[i], HEX);
    if (i < rfid.uid.size - 1) ID += " ";
  }
  ID.toUpperCase();

  Serial.print("Scanned ID: ");
  Serial.println(ID);

  if (ID == UID) {
    Serial.println("Access Granted!");
    delay(1500);
    return 1;
  } else {
    Serial.println("Wrong Card!");
    delay(1500);
    return 0;
  }
}


Editor is loading...
Leave a Comment