
Programming a traffic light system: Changing the lights

Upgrade for more content
True or false? Yellow light means go.
This is a traffic light. It controls traffic at a crossroads. Red for stop, green for go, and a yellow light, to tell that now the traffic light is switching either from red to green or vice versa. Now we’ll make our own traffic lights. For that we need some components: A microcontroller that we can program to control the traffic light.
LEDs that turn red, green and yellow. Some cables, to connect all the parts. Once we have all the components we can get started. First we take a red LED. This is what it looks like.
It has two legs, one a bit longer and one shorter. Gently bend them apart a bit like this and connect the long leg to one of the positive poles on the microcontroller. We select connection, or pin, zero for the red LED. Connect the yellow LED to pin one. And the green LED to pin two.
Connect a cable from the short leg of each LED to the minus pole. So far, none of the diodes are lit. This is because the circuits are still open. Now we will program the microcontroller so that we can control when it closes and opens the circuit. A traffic light turns its lights on and off in a certain order: red, yellow, green, yellow, and so on.
Now we will formulate clear instructions for how the lights should work. The instructions, or the algorithm should be: Turn on red, turn on yellow, turn on green, turn on... Oh, now all the lights are turned on. What has gone wrong? We must obviously improve the algorithm a little bit.
Pause the video and think about what's missing in the instructions. We didn’t give instructions about when a lamp is to be turned off. We ended up with all three lights on at the same time. Let’s adjust the algorithm: Turn on red, turn off red and turn on yellow, turn off yellow and turn on green, turn off green and turn on yellow, turn off yellow and redo. Let’s test our algorithm.
Yes! The lamps don’t shine at the same time. But, now the traffic light changes colour very quickly. No bus has time to move before it switches to red again. Pause the video and think how the algorithm can be improved.
We haven’t talked about how long each colour should be turned on. New variant of the algorithm with pauses between colour changes. Add: Three seconds pause here, so those who want to cross the street can get over. One second here, while it's yellow, so everyone's ready. Three seconds here, so the bus has time to drive through the crossroads.
And one second here. Try again. Yes, it works really well. The pseudocode is almost complete. We can use these instructions, our algorithm, as the basis for programming.
But wait, will the pseudocode just end here? After the green and the yellow light, we turn on red again? No, we don’t need that. We have written 'redo'. It's a loop.
A loop in a program allows some of the program to repeat as long as any condition is true. We want the traffic light to work all the time, so the loop should run continuously. So we put a condition that is always true. Take 'one equal to one'. As long as that's true, our loop runs.
To mark which rows are involved in the loop, we tab in those rows as shown: Okay, now we have done the pseudocode, so it's time for you to program the traffic light.