Problem writing code for model specific (image provided) traffic light prototype

Hi. I’m a final year physics student in Sierra Leone (Africa) and completely new to the Arduino. However I have to use the arduino UNO to build something (in my case a traffic light system) for my dissertation. I’m using a particular intersection in my country for a model (see attached image). The traffic lights would run on a timed sequence. However for the intersection in question, the traffic direction 2 (indicated on image) is usually heavy in the morning (into city centre) and traffic direction 1 heavy in the evening (as people return home from their daily activities). I’d like to make provision for those two distinct traffic conditions in my code. I’m thinking of using a code that gives more time to traffic 2 for a certain period (to suggest mornings) in the cycle the reverse happening for traffic 1 in the evening. However since it is just a prototype i would be using very small time intervals. Any idea on how to include this? or how to solve this traffic problem?

Here is my code for the timed sequence. Pls note traffic 2&1 are denoted by “cgreen” and “lgreen” in code. I’d be grateful to receive as much help as i can. been cracking my head for a while on this

const int cgreen=6;
const int cyellow=5;
const int cred=7;
const int zgreen=8;
const int zyellow=9;
const int zRed=10;
const int lgreen=11;
const int lyellow=12;
const int lred=13;
const int delayTime=2000;

void setup(){
 pinMode (cgreen, OUTPUT);
 pinMode (cyellow, OUTPUT);
 pinMode (cred, OUTPUT);
 pinMode (zgreen, OUTPUT);
 pinMode (zyellow, OUTPUT);
 pinMode (zRed, OUTPUT);
 pinMode (lgreen, OUTPUT);
 pinMode (lyellow, OUTPUT);
 pinMode (lred, OUTPUT);
  
}

void loop(){
digitalWrite (cred, HIGH);
digitalWrite (zRed, HIGH);
digitalWrite (lred, HIGH);
delay (delayTime*3);
digitalWrite (zyellow, HIGH);
digitalWrite (zRed, LOW);
delay (delayTime);
digitalWrite (zgreen, HIGH);
digitalWrite (zyellow, LOW);
delay (delayTime*4);
digitalWrite (zgreen, LOW);
digitalWrite (zyellow, HIGH);
digitalWrite (lred, LOW);
digitalWrite (lyellow, HIGH);
delay (delayTime);
digitalWrite (zyellow, LOW);
digitalWrite (zRed, HIGH);
digitalWrite (lred, LOW);
digitalWrite (lyellow, HIGH);
delay (delayTime);
digitalWrite (lyellow, LOW);
digitalWrite (lgreen, HIGH);
delay (delayTime*7);
digitalWrite (lgreen, LOW);
digitalWrite (cred, LOW);
digitalWrite (cyellow, HIGH);
digitalWrite (lyellow, HIGH);
delay (delayTime);
digitalWrite (lyellow, LOW);
digitalWrite (lred, HIGH);
digitalWrite (cyellow, HIGH);
delay (delayTime);
digitalWrite (cgreen, HIGH);
digitalWrite (cyellow, LOW);
delay (delayTime*7);
digitalWrite(cgreen, LOW);
digitalWrite(cyellow, HIGH);
delay (delayTime);
digitalWrite(cyellow,LOW);
}

I once worked on a traffic light system for a large town.

Note : signal == 3 coloured lights on a pole.

The point is that the sequence of lights for each traffic signal (green/orange/red) runs the same timed sequence, but the time between different signals depends on traffic density , distance between signals , and detection of flow by eg radar sensors.

  • don't use delay() - it blocks the processor - look at the BlinkWithoutDelay example.

  • make each signal it's own little state machine , and start it according to conditions

look at finite state machine examples

An intersection is, of course, 2 (or more!) closely coupled signals.

regards

Allan

Thanks Allan. Do you have a link for the blink without delay and finite state machine examples?

use the search box at the top of this page

regards

Allan

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

Also have a look at Planning and Implementing a Program

Rather than write your program as a long series of digitalWrite()s you should break it up into functions. For example you might have a function to deal with each traffic signal and then you could call the functions at different intervals depending on your requirement.

Using a long repetitive series of digitalWrite()s and delay()s provides great opportunity for silly mistakes and makes the code very difficult to modify without introducing errors.

If you were building a real system I would suggest using a Real Time Clock (RTC) module to keep time. But for a simulation you could just use millis() even though it is not as accurate. I think I would create a variable that counts "pseudo" hours and use its value to determine the different cycle times for different periods of the day.

...R

You also can have a look at the traffic light example, using task macros. These macros wrap the aforementioned principles (blink without delay, state machines...), for easier and more obvious use.