EET207
Week 4 – Lab (No Raspberry Pi)
Introduction:
If you do not have a Raspberry Pi
available, this is an alternate lab for creating and controlling stoplights.
You will import two Python libraries: turtle and time.
Please
also watch the video demonstration of using the Raspberry Pi with GPOs.
You will perform this alternate lab
on your PC.
Procedures:
You will need to install and then import the libraries
“turtle” and “time”
·
“turtle” is an interactive screen drawing
tool
·
“time” is used to set delays
Running the following code will show a single traffic light
at an intersection with a blinking yellow light. See the comments within the
code to analyze how it works. Then run the code. You should see the yellow
light blink (on for one second, off for one second, repeat). Note that it will
loop endlessly until you stop the program.
Code:
# Blinking Yellow Stoplight on PC - demo using turtle library
import
turtle
import
time
tl = turtle.Screen()
# tl for traffic light
tl.title(
"Stoplight"
)
tl.bgcolor(
"blue"
)
# Draw box representing stop light
pen = turtle.Turtle()
pen.color(
"yellow"
)
pen.width(
5
)
pen.hideturtle()
# This hides the arrow of the turtle
pen.penup()
pen.goto(-
30
,
60
)
# move to starting coordinates of rectangle
pen.pendown()
pen.fd(
60
)
# move pen forward
pen.rt(
90
)
# move pen right
pen.fd(
120
)
pen.rt(
90
)
pen.fd(
60
)
pen.rt(
90
)
pen.fd(
120
)
# position red light (unlit)
red_light = turtle.Turtle()
red_light.shape(
"circle"
)
red_light.color(
"grey"
)
red_light.penup()
red_light.goto(
0
,
40
)
# x, y coordinates for red light
# position yellow light (unlit)
yellow_light = turtle.Turtle()
yellow_light.shape(
"circle"
)
yellow_light.color(
"grey"
)
yellow_light.penup()
yellow_light.goto(
0
,
0
)
# x, y coordinates for yellow light
# position green light (unlit)
green_light = turtle.Turtle()
green_light.shape(
"circle"
)
green_light.color(
"grey"
)
green_light.penup()
green_light.goto(
0
, -
40
)
# x, y coordinates for green light
# Put blinking yellow traffic light into operation
while True
:
# Turn on yellow light
yellow_light.color(
"yellow"
)
# turn on the yellow light
time.sleep(
1
)
# wait 1 second
yellow_light.color(
"grey"
)
# turn off yellow light
time.sleep(
1
)
# 1 second delay
Problems:
- (One-way Traffic light) Using
the example code for the blinking yellow light, develop a python program to
display a single traffic light with the following required cases:
- Green light turns on for
5 seconds while the yellow and red LED are off.
- Yellow light turns on for
1 second while the red and green LED are off.
- Red light turns on for 5
seconds while the yellow and green LEDs are off.
- (Two-way Traffic light) Expanding
on your solution for problem 1, develop a python that simulates a two
traffic lights (North-South and West-East. Configure the lights for the following
sequence:
- When the light is green
in North-South side, the light in the West-East is red.
- When the light turns
yellow in North-South side, the light in the West-East side is still red.
- When the light in
North-South turns red, the light in West-East becomes green.
- The sequence for each
light is green (5 seconds), yellow (1 second), and red (5 seconds)
- Verify that the N/S and
E/W are not both green at the same time!
- The process should loop
indefinitely until you stop the program.