traffic light project!

I have a traffic light which only has Green and Red lights only… I got it installed with wiring and arduino board/relay inside and an ultrasonic sensor for distance and i uploaded the code to it… everything works great.

Basically the code is when the sensor reads the distance (150cm= 4.9 ft) or less then it turns Red, otherwise its Green if it didnt detect anything in front of it.

My only problem is… the light stays on all the time… i’m trying to add to the code something that tells the lights or relay to turn off when there is no reading or maybe turn off the lights after lets say 5 mins of no reading… can someone help me as am a new member to arduino and still trying to figure this out.

Thanks!!

#define trigPin 12
#define echoPin 13
#define ledR 4
#define ledG 5

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 150) {
digitalWrite(ledR,HIGH); // Turn the Red light on when the distance is 150cm or less
digitalWrite(ledG,LOW);
}
else {
digitalWrite(ledR,LOW); // Turn the Green light
digitalWrite(ledG,HIGH);
}
if (distance >= 400 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(“cm”);
}
delay(200);
}

When the state change occurs, set a var like redMS to millis() + 180000 then have the loop look for for when millis > redMS to turn off the lamp (digitalWrite(ledR),LOW);.

Essentially you tell it a time to turn off from current time measured in milliseconds of up-time. I assume its for car parking. Have fun!

ivaldiz: its Green if it didnt detect anything in front of it.

tells the lights or relay to turn off when there is no reading

Aren't those contradictory? If it didn't detect anything, that means there's no reading.....

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

Thank you very much… and sorry for not posting the code the right way… this post literally my first one! lol

below is the diagram I used for my circuit - the only thing is missing from it is the ultrasonic sensor and am using red and green traffic light not yellow… and yes im using for parking the car inside my garage… i can post a video to show how it works… but i dont think the forums allows video format…

Thanks!

Below is a copy of code again…

#define trigPin 12
#define echoPin 13
#define ledR 4
#define ledG 5

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance <= 150) {  
    digitalWrite(ledR,HIGH); // Turn the Red light on when the distance is 150cm or less 
    digitalWrite(ledG,LOW);
}
  else {
    digitalWrite(ledR,LOW); // Turn the Green light
    digitalWrite(ledG,HIGH);
  }
  if (distance >= 400 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println("cm");
  }
  delay(200);
}

Hi,
Thanks for the info.
OPs circuit,
F8NATNRHDRS68F6.LARGE.jpg
Tom… :slight_smile:

mattlogue:
When the state change occurs, set a var like redMS to millis() + 180000 then have the loop look for for when millis > redMS to turn off the lamp (digitalWrite(ledR),LOW);.

Essentially you tell it a time to turn off from current time measured in milliseconds of up-time. I assume its for car parking. Have fun!

Never thought of using millis() as a timer… I will give that a try!

Thanks alot!

I will give that a try!

Do it properly, though. Do NOT add times. That can cause rollover, which causes all kinds of problems. Use subtraction, instead, which is guaranteed to work, even when the value output by millis() rolls over.

unsigned long now = millis();
if(now - then >= idleTime)
{
   // It's time to not be idle anymore
   then = now;
}