
Programming a traffic light system: Detecting a vehicle

Upgrade for more content
What message is sent by radio to the traffic light?
This is a traffic light. It’s on red. Here comes a bus. Which is running late. Imagine if the bus could get back on schedule by not having to wait at a red light!
What if the bus could tell the traffic light that it’s coming? Now we going to program a bus so it can send a message to a traffic light. We are going to use radio signals for the communication. Before we start programming we need: A microcontroller and three LEDs connected to it, as described in another video. Another microcontroller, to represent the bus.
And a program for the bus. We start with the pseudocode for the bus. The bus will send a radio signal to the traffic light. For the traffic light to be able to receive the bus’s radio signal it needs to use the same frequency, the same radio group. We choose radio group “one”.
So the pseudocode for the bus’s program starts with: Start program. Set radio group to “one”. The bus wants to have a green light. When the bus sends the message "one" it means "here I come, switch to green". When will the bus send its "one" then?
Well, when it approaches the traffic light. We choose to connect this to the A button on the microcontroller. If button A is pressed, send a “one”. That's actually all we need for the bus program. Now to the traffic light program.
The traffic light is listening to the same radio channel that the bus is sending on. So the traffic light pseudocode will also be: Start program. Set radio group to “one”. The traffic light should not only listen, it should also react to every message from the bus. In order to listen and react constantly, we need to have a code that runs over and over again.
Then we use a loop with a condition that always is true. That part of the code looks like this: Now the traffic light just shines red all the time. When the traffic light receives the message from the bus, it will turn to green: give the bus priority. Create the "prio" variable that keeps track of the bus requesting priority. When “prio” is “true” the bus has priority.
else “prio” is “false”. We add this to the pseudocode: If radio receives “one” so set “prio” to “true”. When “prio” is “true” the traffic light turns to green. Else the traffic light should shine red. We add this condition: If “prio” is equal to “false” only then will the traffic light turn red.
We indent the row that’s connected to the if-row. But if "prio" does not equal "false"? We write: Else turn off red and turn on green. At the start of the program the traffic light should be red, “prio” should be “false”. Shall we test our pseudocode?
At first, “prio” is “false” and the traffic light is red. Now the bus driver presses the button. “Prio” becomes “true”. The code jumps to “else” and the light turns green! But it doesn’t turn red again. Hmm...we never decided how long the green light should be lit, "prio" will never go back to "false" again.
So we’ll allow the bus three seconds to drive past the traffic light by adding three seconds pause, here. Then we turn off the green light and set “prio” to “false” so the red light can shine again. All these rows are connected with the else-part. We show that by tabbing them in. Let’s test again. “Prio” is “false” and the traffic light is red.
Here comes the bus that is late, button A is pressed down and “prio” becomes “true”. The program jumps to “else” and the traffic light becomes green. After three seconds “prio” becomes “false” and the traffic light becomes red again. Even though we didn’t program the yellow light, and it’s always red if no bus comes, it's a good start anyway. And maybe this bus might catch up with its schedule.
Good!