It's your first day on the job with the city transportation department and already there's a problem. Watch the video voicemail from your supervisor to find out what's going on. Then, scroll down to the notes from your coworker to discover how you can fix it.
I just got a call from the mayor. All the traffic signals in the city are flashing red. With your programming skills, we're hoping you can get this problem sorted. Have a look at the code in Visual Studio and see if you can make a proper traffic light sequence.
Fortunately, your coworker has left some documentation on the traffic light system. Take a look at the notes below.
The traffic light code is written in MicroPython. Whenever the system has a problem, it reverts back to flashing red so everyone is forced to stop and there are no fender benders.
Here's the code for that:
from machine import Pin
from time import sleep
red = Pin(4, Pin.OUT)
yellow = Pin(5, Pin.OUT)
green = Pin(6, Pin.OUT)
while True:
red.on()
yellow.off()
green.off()
sleep(1)
red.off()
yellow.off()
green.off()
sleep(1)
You'll also find a copy in the ~/Code directory on your development workstation. It's in the traffic_light subdirectory.
It works by using the microcontroller General Purpose Input Output (GPIO) pins to control the red, yellow, and green signal lights. In flashing red mode, only the GPIO pin controlling red is cycled on and off. Yellow and green stay off.
There is a sleep()
function that controls how many
seconds to pause before executing the next step. And the whole
thing is wrapped in a while True:
loop to make it
run forever.
Basically, it works like this:
You can run MicroPython code by logging in through
secure shell as the student user account,
changing to the Code/traffic_light
directory, and using the command:
mpremote run traffic_light.py
Here's an example of what that would look like:
login: student
password: ********
pi:~$ cd Code
pi:~/Code$ cd traffic_light
pi:~/Code/traffic_light$ mpremote run traffic_light.py
Press CTRL+C to get the command prompt back.
After making changes to the code, repeat the
mpremote run traffic_light.py
command to run it
again.
There was code written for the regular traffic light sequence, but it never got uploaded to the server. And to make matters worse, the laptop where it was saved got left unattended in the airport and was taken out to the tarmac and blown up by TSA.
So, you'll have to start over again. Shouldn't be too hard though.
Just remember the GPIO on()
and off()
methods and the sleep()
function to control the
delay time.
If you get stuck, here are some things that can help you.
All of the code at the top of the program (the
from import
statements and the
Pin()
assignments) can be left as-is. The
changes you want to make are part of the
while True:
loop.
while True:
red.on()
yellow.off()
green.off()
sleep(1)
red.off()
yellow.off()
green.off()
sleep(1)
Take the example of the flashing red. It looks like this:
while True:
red.on()
yellow.off()
green.off()
sleep(1)
red.off()
yellow.off()
green.off()
sleep(1)
Change it so the second block turns on green LED. Like this:
while True:
red.on()
yellow.off()
green.off()
sleep(1)
red.off()
yellow.off()
green.on()
sleep(1)
Now when you run the program, the sequence should be: red, green, repeat.
It works the same way for yellow. Just copy and paste the code block and adjust the on / off.
while True:
red.on()
yellow.off()
green.off()
sleep(1)
red.off()
yellow.off()
green.on()
sleep(1)
red.off()
yellow.on()
green.off()
sleep(1)
A typical traffic light runs like this: red, green, yellow
for a short time, and then back to red. Structure your code
blocks to light the LEDs in this sequence. Then, use the
values inside the sleep()
function to adjust
how long to wait for the next change.
while True:
red.on()
yellow.off()
green.off()
sleep(5)
red.off()
yellow.off()
green.on()
sleep(4)
red.off()
yellow.on()
green.off()
sleep(1)