
Programming a traffic light system: Improving the code

Upgrade for more content
True or false? When the programs start, the bus has priority.
Ordinary traffic lights switch between red, yellow, and green at regular intervals, whether a vehicle comes or not. But this is a smart traffic light! It can respond to a signal from a bus that wants a green light. But the traffic light needs some improvement. It's red until the bus requests a green light, and it never uses its yellow light.
Now we will make this improvement. Let’s start with the pseudocode we wrote in another video. If button A is pressed the bus sends a signal in radiogroup “one”. The message is “one” and it means “I want to have green”. Now the traffic light’s pseudocode.
In that program we also started by setting the radiogroup to “one”. The variable “prio” controls whether the bus has priority or not. To start with, the bus doesn’t have that, so “prio” is “false”. If the message “one” is received, “prio” becomes “true”, and the bus gets a green light. Now let’s look at the code that manages the colour changes for the traffic light.
The loop runs all the time as long as the condition is true. If “prio” is “false” the red light turns on. Else the red light turns off and the green one shines. After three seconds the green turns off, “prio” becomes “false”, we go back to the start of the loop again, and the red light shines. The improvement we want to make now is to tell it when to shine the yellow light.
Between red and green we want it to be yellow. And the same between green and red. We add: Turn off red, turn on yellow and turn off green, turn on yellow. Let’s test the code. Oh, that’s too quick.
We need to tell it how long to shine red before becoming yellow, and how long to shine yellow, before becoming green. So we add: Pause for three seconds after red is turned on, here and pause for one second after yellow is turned on, in both of these two places. And here, we write: Turn off yellow, turn on green. And also write: Turn off yellow before “prio = false”. Now we have made one improvement: the yellow light shines when we want it to.
But still the traffic light only becomes green when the bus driver presses the button. That's because we wrote the code controlling the green light as an else-statement. Which means it only happens when something isn’t true. It’s better to tell it positively to always happen. So we remove the else-statement, and then yellow and green always shine when we want them to: every time the loop runs.
Let’s test our second improvement. Here comes a bus. The bus driver presses the button. “Prio” goes immediately to “true”. But nothing has changed. Why not?
It’s shining red, and then yellow anyway! What happened? Oh, we were here in the pseudocode, when the button was pressed, and it didn’t test for “prio” being “true” until the loop went back to the start. So the bus still had to wait at a red light. The pauses in the code gave us problems we didn’t expect.
We need to get the code to test if “prio” is “true” more often. When we have programmed it to "pause" the code stands still: it does not listen. It only checks whether “prio” is “true” here: at the if-statement. To get the traffic light to listen more often, we need more if-statements. Let’s reduce the delay to one second by splitting the pauses like this: Now the code can to jump to green from from several different places.
But we want to make sure that only one light shines at a time. If "prio" is "true" here, and we skip this row, red does not turn off. We want green to be the only light shining for the bus, so we have to tell the code to turn off red and yellow here. Let’s test again. Here comes the bus that is late, button A is pressed and the traffic light switches quickly to green.
Now the traffic light has become even smarter! Maybe you can come up with even more improvements!