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:...

1 answer below »







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.








Icon<br><br>Description automatically generated









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:







  1. (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:


    1. Green light turns on for

      5 seconds while the yellow and red LED are off.

    2. Yellow light turns on for

      1 second while the red and green LED are off.

    3. Red light turns on for 5

      seconds while the yellow and green LEDs are off.


  2. (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:


    1. When the light is green

      in North-South side, the light in the West-East is red.

    2. When the light turns

      yellow in North-South side, the light in the West-East side is still red.


    3. When the light in

      North-South turns red, the light in West-East becomes green.

    4. The sequence for each

      light is green (5 seconds), yellow (1 second), and red (5 seconds)

    5. Verify that the N/S and

      E/W are not both green at the same time!

    6. The process should loop

      indefinitely until you stop the program.



Answered 1 days AfterSep 19, 2022

Answer To: EET207 Week 4 – Lab (No Raspberry Pi) Introduction: If you do not have a Raspberry Pi available,...

Sathishkumar answered on Sep 21 2022
52 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here