How to make ultrasonic sensor read after RFID card

Hello everyone… i desperately need your help on how to arrange the coding to make ultrasonic sensor and rfid detect at the same time. Here is my coding. In short, i make a program that using servo motor, rfid and ultrasonic sensor.

i want to make a parking system that need rfid card to lift the gate up or down. After the car get into the parking spot, the ultrasonic sensor will detect if the car is there. If it doesnt have any car inside the box within 3 seconds, the bar will lift up. If ultrasonic sensor detect that there is a car inside of it, the bar will remain in the same position and let the car park inside of it.

im doing my coding right when verified, but when i run it does not working as i wanted it to be :’( :’( can u guys figure it out and help me through all of this problems. I really appreciate all of your help.

if u not really understand how i want it work, here i attached the video link of example how i want it to work.

#include <SPI.h>
#include <MFRC522.h>
int count = 0;

#include <VarSpeedServo.h>

VarSpeedServo servo1;
const int servopin = 6;

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

int trigPin = 2;
int echoPin = 3;
long distance;
long duration;

void setup()

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522

Serial.println(“Put your card to the reader…”);

void loop()
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
//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 < 0x10 ? " 0" : " ");
_ Serial.print(mfrc522.uid.uidByte*, HEX);_
_ content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte, HEX));
Serial.print("Message : ");
if (content.substring(1) == “27 3F 3B 4F”)
servo1.write(0,90,true); //Ni untuk ejas servo kalau nak bagi palang pacak jadi (0,90, true)
Serial.println(“Authorized access”);
count++; // Increment Count by 1*

* delay(400);

else {
* delay(2000);*
* Serial.println(" Access denied");*
long duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10) {
Serial.println(“the distance is less than 5”);
else {

RFID_Counter_servo.ino (2.18 KB)

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Please tell us what the problems are. I do not want to guess and the video is not working.

The easiest way would be to create a pseudo state machine or use some flags to determine if something has happened or not. If card is valid set flag. If flag is set raise gate and clear flag. You can add delays etc.
Good Luck & Have Fun!