Traffic Light with ultrasonic sensor

Hi all I am currently working on a smart traffic light system which consists of four lanes, each lanes has one sensor to detect if that lane is in long queue. If yes, then the green light will be turned on for longer time.
I tried to use If Else for this sensor part, but my problem is when 2 or more sensors have input then I couldn’t make the respective long queue lanes to have green light on for long time, I can only make one lane green light to on for longer. Seek for help.
Below is my coding for traffic light system without sensor part, and second is with my sensor part.

(1) Coding 1 - without sensor
int r1 = 52;
int y1 = 50;
int g1 = 48;
int r2 = 46;
int y2 = 44;
int g2 = 42;
int r3 = 40;
int y3 = 38;
int g3 = 36;
int r4 = 34;
int y4 = 32;
int g4 = 30;
//////////////////////
void setup() {
pinMode(r1, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(y2, OUTPUT);
pinMode(g2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(y3, OUTPUT);
pinMode(g3, OUTPUT);
pinMode(r4, OUTPUT);
pinMode(y4, OUTPUT);
pinMode(g4, OUTPUT);
}

void loop() {

digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(r2, HIGH);
delay(3000);

digitalWrite(y1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(9000);

digitalWrite(g2, LOW);
digitalWrite(r3, LOW);
digitalWrite(y2, HIGH);
digitalWrite(r3, HIGH);
delay(3000);

digitalWrite(y2, LOW);
digitalWrite(r3, LOW);
digitalWrite(r2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g3, LOW);
digitalWrite(r4, LOW);
digitalWrite(y3, HIGH);
digitalWrite(r4, HIGH);
delay(3000);

digitalWrite(y3, LOW);
digitalWrite(r4, LOW);
digitalWrite(r3, HIGH);
digitalWrite(g4, HIGH);
delay(9000);

digitalWrite(r3, HIGH);
digitalWrite(g4, LOW);
digitalWrite(r1, HIGH);
digitalWrite(y4, HIGH);
delay(3000);

digitalWrite(r1, LOW);
digitalWrite(y4, LOW);

}

(2) Coding 2 - with 2 sensors using IF ELSE

#define echoPin1 7 ///// lane 1
#define trigPin1 8 ///// lane 1
#define echoPin2 5 ///// lane 2
#define trigPin2 6 ///// lane 2

int r1 = 52;
int y1 = 50;
int g1 = 48;
int r2 = 46;
int y2 = 44;
int g2 = 42;
int r3 = 40;
int y3 = 38;
int g3 = 36;
int r4 = 34;
int y4 = 32;
int g4 = 30;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration1, distance1, duration2, distance2; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(r1, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(y2, OUTPUT);
pinMode(g2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(y3, OUTPUT);
pinMode(g3, OUTPUT);
pinMode(r4, OUTPUT);
pinMode(y4, OUTPUT);
pinMode(g4, OUTPUT);

}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);

digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance1 = (duration1)/58.2;
////////////////////////////////////////////////////////
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance2 = (duration2)/58.2;

if (distance1 >= maximumRange || distance1 <= minimumRange || distance2 >= maximumRange || distance1 <= minimumRange) {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance1); // Green for Lane 1 //
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g1, LOW); // Yellow for Lane 1 //
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(r2, HIGH);
delay(3000);

digitalWrite(y1, LOW); // Green for Lane 2 //
digitalWrite(r2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(9000);

digitalWrite(g2, LOW);// Yellow for Lane 2 //
digitalWrite(r3, LOW);
digitalWrite(y2, HIGH);
digitalWrite(r3, HIGH);
delay(3000);

digitalWrite(y2, LOW); // Green for Lane 3 //
digitalWrite(r3, LOW);
digitalWrite(r2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g3, LOW);// Yellow for Lane 3 //
digitalWrite(r4, LOW);
digitalWrite(y3, HIGH);
digitalWrite(r4, HIGH);
delay(3000);

digitalWrite(y3, LOW); // Green for Lane 4 //
digitalWrite(r4, LOW);
digitalWrite(r3, HIGH);
digitalWrite(g4, HIGH);
delay(9000);

digitalWrite(r3, HIGH); // Yellow for Lane 3 //
digitalWrite(g4, LOW);
digitalWrite(r1, HIGH);
digitalWrite(y4, HIGH);
delay(3000);

digitalWrite(r1, LOW);
digitalWrite(y4, LOW);
}

if (distance1 <= maximumRange || distance1 <= minimumRange || distance2 >= maximumRange || distance2 <= minimumRange){
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
delay(18000);

digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(r2, HIGH);
delay(3000);

digitalWrite(y1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(9000);

digitalWrite(g2, LOW);
digitalWrite(r3, LOW);
digitalWrite(y2, HIGH);
digitalWrite(r3, HIGH);
delay(3000);

digitalWrite(y2, LOW);
digitalWrite(r3, LOW);
digitalWrite(r2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g3, LOW);
digitalWrite(r4, LOW);
digitalWrite(y3, HIGH);
digitalWrite(r4, HIGH);
delay(3000);

digitalWrite(y3, LOW);
digitalWrite(r4, LOW);
digitalWrite(r3, HIGH);
digitalWrite(g4, HIGH);
delay(9000);

digitalWrite(r3, HIGH);
digitalWrite(g4, LOW);
digitalWrite(r1, HIGH);
digitalWrite(y4, HIGH);
delay(3000);

digitalWrite(r1, LOW);
digitalWrite(y4, LOW);
}

if (distance1 >= maximumRange || distance1 <= minimumRange || distance2 <= maximumRange || distance2 <= minimumRange){
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(r2, HIGH);
delay(3000);

digitalWrite(y1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(18000);

digitalWrite(g2, LOW);
digitalWrite(r3, LOW);
digitalWrite(y2, HIGH);
digitalWrite(r3, HIGH);
delay(3000);

digitalWrite(y2, LOW);
digitalWrite(r3, LOW);
digitalWrite(r2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g3, LOW);
digitalWrite(r4, LOW);
digitalWrite(y3, HIGH);
digitalWrite(r4, HIGH);
delay(3000);

digitalWrite(y3, LOW);
digitalWrite(r4, LOW);
digitalWrite(r3, HIGH);
digitalWrite(g4, HIGH);
delay(9000);

digitalWrite(r3, HIGH);
digitalWrite(g4, LOW);
digitalWrite(r1, HIGH);
digitalWrite(y4, HIGH);
delay(3000);

digitalWrite(r1, LOW);
digitalWrite(y4, LOW);
}

if (distance1 <= maximumRange && distance2 <= maximumRange || distance1 <= minimumRange || distance2 <= minimumRange){
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
delay(18000);

digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(r2, HIGH);
delay(3000);

digitalWrite(y1, LOW);
digitalWrite(r2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(18000);

digitalWrite(g2, LOW);
digitalWrite(r3, LOW);
digitalWrite(y2, HIGH);
digitalWrite(r3, HIGH);
delay(3000);

digitalWrite(y2, LOW);
digitalWrite(r3, LOW);
digitalWrite(r2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r4, HIGH);
delay(9000);

digitalWrite(g3, LOW);
digitalWrite(r4, LOW);
digitalWrite(y3, HIGH);
digitalWrite(r4, HIGH);
delay(3000);

digitalWrite(y3, LOW);
digitalWrite(r4, LOW);
digitalWrite(r3, HIGH);
digitalWrite(g4, HIGH);
delay(9000);

digitalWrite(r3, HIGH);
digitalWrite(g4, LOW);
digitalWrite(r1, HIGH);
digitalWrite(y4, HIGH);
delay(3000);

digitalWrite(r1, LOW);
digitalWrite(y4, LOW);
}

//Delay 50ms before next reading.
delay(500);
}

Maybe the massive delays meant you didn’t detect the queues properly

Needs more code tag.

Looks like 64 seconds worth of delays in a single block. That's 64 seconds of not trying to sense anything. I may have miscounted somewhere but that's a VERY long time.

Turn this into a finite state machine and get rid of all those delays (use millis() for timing). This will make your code a lot more responsive and flexible.

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.

Thanks.. Tom... :slight_smile:

These are called arrays. They let you shrink code by grouping like variables under a single name and access them by number.

So the same function or loop can serve them all, no need to have a special code for each one,

https://www.arduino.cc/reference/en/language/variables/data-types/array/

I remember ultrasonic sensor on traffic lights from test sites in the 70's. They never got wide use, just trials. By the 80's and since it's all magnetic detection. Are you about reinvent the square wheel?

can i get the circuit connection or any type of block diagram .

can you please send me the circuit diagram .

sagarpdn13:
can i get the circuit connection or any type of block diagram .

OP was a one post wonder. Didn't get back on our comments; don't expect anything better on your request.