
Programming a traffic light system: Countdown display

Upgrade for more content
True or false? We show the number of seconds to green light with the microcontroller.
A bus approaches a traffic light which happens to turn green. The bus drives through without needing to stop. Now it arrives at the next traffic light which also changes to green. What luck! Or is it luck?
someone planned for this one to turn green. No, Someone wants the bus not to have to start and stop. This is called a ‘green wave’. No...not that kind of green wave. But a wave of green lights.
Imagine if the traffic light system could know what average speed a bus needs to have to get a green wave, and if the traffic lights could show how many seconds will pass before they turn green. Then the driver could adapt the speed of the bus, to time the arrival at each light. Now we are going to program one such traffic light to let us know when it's going to turn green. Before we start programming we need several things. 1.
A microcontroller. This controls the display that shows how long before it turns green. 2. Three LEDs, one red, one yellow, and one green. These are the traffic lights.
3. Cables to connect all the components. 4. And a program for working a traffic light. We can use a program that we made in another video.
We’ll connect the traffic light in exactly the same way as in that video. The pseudocode for the program looks like this: When red, the traffic light should show how long before it turns green. How can the program figure that out? Pause the video and think. This row instructs that the red light shines for three seconds, before this row tells the yellow light to shine.
Then on the next row there is a pause for one second before the green light shines. If we add the pauses together while it’s changing to red and yellow, we get three seconds plus one second, which together is four seconds. It will therefore take four seconds to become green again from when the red light starts to shine. In the code we add: “Show four on the display” There's a pause for three seconds and then there’s one second left before green. We show that by adding: “Show one on the display” before the one second pause.
Let’s test our pseudocode. the display jumps from four seconds to one second. But The bus driver won’t know when it's three or two seconds left to green. How can we improve our pseudocode so it becomes clearer? We can split our three seconds pause into three pauses, of one second each.
For each pause we can show a new number on the display. Like this: “Show three on the display” Pause one second and “Show two on the display” Pause one second. Let’s test again. Four, three, two, one, green. It works!
Now the driver gets information every second about how long before the green light. The bus driver can use this information to adapt the speed so that the bus can travel through town without having to stop. We have programmed a green wave! Ride that wave!