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:
- Red LED on pin 2, Yellow LED on pin 3, Green LED on pin 4
- Two switches on pin 5 and pin 6
- Ultrasonic sensor HC-SR04
- trigPin on pin 7
- echoPin on pin 8
- 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;
}