RFID loop and return

Hi guys,

im having trouble with my loop including rfid and ultrasonic sensor. It's about a parking place you can access both when you put the right tag AND the car is close enough to the ultrasonic sensor.. Once the car leave the parking place (and not in front of the ultrasonic sensor anymore), i would like the gate to close... It's probably super easy but i don't get it... actually the gate stays opened even though the car left..

thanks for your time


#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <HCSR04.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED_Verte 5 //define green LED pin
#define LED_Rouge 4 //define red LED
#define BUZZER 2 //buzzer pin, 
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //define servo name
UltraSonicDistanceSensor capteur_distance(6, 7);






void setup()
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  myServo.attach(3); //servo pin
  myServo.write(0); //servo start position
  pinMode(LED_Verte, OUTPUT);
  pinMode(LED_Rouge, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
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 = "";
  byte letter;
  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));
  }
  int distance = capteur_distance.measureDistanceCm();
  Serial.println();
  Serial.print("Message : ");
  Serial.print("Distance: ");
  Serial.println(distance);  
  content.toUpperCase();
  if (content.substring(1) == "E9 CA 97 98" && distance < 40 ) //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_Verte, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myServo.write(90);
    delay(1000);
    return 0;
     }
 

  else   {

    Serial.println(" Access denied");
    digitalWrite(LED_Rouge, HIGH);
    delay(500);
    digitalWrite(LED_Rouge, LOW);
    delay(1000);
    tone(BUZZER, 300);
    delay(300);
    noTone(BUZZER);
    myServo.write(0);
    delay (1000);
     
  }

  delay (2000);
   
    
  }

 

your loop starts by checking the presence of a new RFID card and if it is not there just stops here. So you don't get a chance to check the distance

➜ you need to not return in that case but go check the distance to see if the car is gone

a state machine would probably be a good software architecture for this code

1 Like

Hello, as shown in the serial monitor, the UID tag is kept but the distance is supposed to show that the car left.. obviusly once i put my hand in front of the ultrasonic sensor, the door opens again while it should not.

i need to find how to initialize the RFID in the same time.. i keep digging. Thanks for your time

Capture d’écran 2021-09-07 à 14.20.06