I've been having some trouble with this "Ultrasonic Traffic-Light" and can't figure out why. I've browsed through forums, but can't find any that helps so decided to ask by a forum myself.
I just can get the red led (stop) to turn of when the green led(go) is on. The green led works as intended, it's just the red one that I'm having trouble with. Any help is appreciated.
It worked, but I would've liked if you could label your stuff, similar to what you recommended. I say this because I would've liked to figure out what you did differently. Otherwise, thanks so much for your help!
Compare your code with @J-M-L 's and you'll notice what he did differently.
At least that's what I do, and it works for me.
I add:
To see if it helps you, I'm leaving you the code of @J-M-L commented by Copilot
// Define ultrasonic sensor pins
const byte trigPin = 3; // Trigger pin for the ultrasonic sensor
const byte echoPin = 4; // Echo pin for the ultrasonic sensor
// Define LED pins for traffic light simulation
const byte redPin = 5; // Red LED pin (STOP)
const byte yellowPin = 6; // Yellow LED pin (SLOW)
const byte greenPin = 7; // Green LED pin (GO)
// Define distance thresholds in centimeters
const float goThreshold = 105.0; // Distance above which the light turns green
const float slowThreshold = 50.0; // Distance above which the light turns yellow
// Define traffic light states
enum State { STOP, SLOW, GO };
State state = STOP; // Initial state is STOP
// Function to measure distance using the ultrasonic sensor
float distance() {
long duration;
// Send a 10µs pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the time it takes for the echo to return (timeout after 20ms)
duration = pulseIn(echoPin, HIGH, 20000);
// Convert duration to distance in centimeters
return duration * 0.034 / 2.0;
}
// Function to update LED states based on current traffic state
void setLights() {
digitalWrite(redPin, (state == STOP) ? HIGH : LOW); // Turn on red LED if STOP
digitalWrite(yellowPin, (state == SLOW) ? HIGH : LOW); // Turn on yellow LED if SLOW
digitalWrite(greenPin, (state == GO) ? HIGH : LOW); // Turn on green LED if GO
}
// Arduino setup function (runs once at startup)
void setup() {
// Set LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
// Set ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize LEDs based on initial state
setLights();
}
// Arduino loop function (runs repeatedly)
void loop() {
float dist = distance(); // Measure current distance
State newState;
// Determine traffic light state based on distance
if (dist > goThreshold) {
newState = GO;
} else if (dist > slowThreshold) {
newState = SLOW;
} else {
newState = STOP;
}
// Update state and LEDs only if the state has changed
if (newState != state) {
state = newState;
setLights();
}
delay(50); // Small delay to reduce flickering and CPU load
}