Traffic barrier HELP ME PLSSS

Hello,
I am working on a barrier project that involves traffic lights and an obstacle detection system. The barrier will lift when an obstacle is detected and lower when there is none.
I/O:

  1. Red LED on pin 2, Yellow LED on pin 3, Green LED on pin 4
  2. Two switches on pin 5 and pin 6
  3. Ultrasonic sensor HC-SR04
  • trigPin on pin 7
  • echoPin on pin 8
  1. Servo motor on pin 9

I am currently facing an issue with checking for obstacles. Can you help me?
#include <Servo.h>
#include <LiquidCrystal_I2C.h>

// Pin definitions
const int redLED = 2;
const int yellowLED = 3;
const int greenLED = 4;
const int switch1 = 5; // Switch to start the process
const int switch2 = 6; // Reset switch
const int trigPin = 7; // Ultrasonic sensor Trig pin
const int echoPin = 8; // Ultrasonic sensor Echo pin
const int servoPin = 9;

Servo myServo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address for the LCD

void setup() {
// Pin setup
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// Servo setup
myServo.attach(servoPin);
myServo.write(90); // Start with gate up

// LCD setup
lcd.init(); // Use init() instead of begin()
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("System Ready");

// Turn off all LEDs initially
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
}

void loop() {
// Check if switch1 is pressed
if (digitalRead(switch1) == LOW) {
runTrafficSystem("Cars Running", "Cars Stopped");
}

// Check if switch2 is pressed
if (digitalRead(switch2) == LOW) {
myServo.write(90); // Reset the gate to open
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
lcd.clear();
lcd.print("System Reset");
delay(2000); // Short delay to show reset message
}
}

void runTrafficSystem(const char* startMsg, const char* stopMsg) {
// Start the process
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(startMsg);

// Green light ON
digitalWrite(greenLED, HIGH);
countDown(5); // Countdown 5 seconds

// Yellow light ON for 3 seconds
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
delay(3000);
digitalWrite(yellowLED, LOW);

// Red light ON and close gate
digitalWrite(redLED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(stopMsg);
myServo.write(0); // Lower the gate

// Ultrasonic sensor to check for obstacles
for (int i = 0; i < 5; i++) {
if (checkObstacle()) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Obstacle Detected");
myServo.write(90); // Open gate
delay(2000); // Wait before checking again
break;
}
delay(1000); // Check every second
}

// Wait for 5 seconds before opening the gate
delay(5000);
digitalWrite(redLED, LOW);
myServo.write(90); // Open the gate
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gate Open");
}

void countDown(int seconds) {
for (int i = seconds; i >= 0; i--) {
lcd.setCursor(0, 1);
lcd.print("Countdown: ");
lcd.setCursor(11, 1); // Start position for the countdown number
lcd.print(" "); // Clear previous number
lcd.setCursor(11, 1); // Position for the countdown number
lcd.print(i); // Display current countdown number
delay(1000); // Wait 1 second
}
}

bool checkObstacle() {
// Clear the trigger
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Trigger the ultrasonic sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the echo pin
long duration = pulseIn(echoPin, HIGH);

// Calculate the distance in cm
long distance = duration * 0.034 / 2; // Speed of sound = 0.034 cm/us

// Return true if an obstacle is detected within 50 cm
return distance < 50;
}

Hi, welcome to the community
Hope u have a great experience here.
There are some needed improvements that u can make to your post:

  1. Add the code in code tags like this: hello world
  2. Your question "im facing issue with checking for obstacles is too vague. pls clear whats the exact problem. for eg. "I am getting random values in the serial monitor" or "sensor is not detecting at all no matter where the object is"
  3. Finally, when asking a question about a physical project, always include a circuit diagram/schematic. It helps us to troubleshoot better.
    Pls dont keep the title as "HELP ME PLSSSS" instead keep it something informative "Problem in receiving values from ultrasonic sensor" or so.
1 Like

Hand drawn and photographed wiring diagram. Use the IDE Tools Menu Auto Format option to make code readable. Post all code and verbose error logs in code tags.
That will get you better support.

I dont recommend using ultrasonic sensor..
It only bounce back on solid plane material..
Multiple sensor is needed.
And probably at specific distance

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.