Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Execution 4 : Enter the integer: 0 for Red Led, 1 for Yellow, 2 for Green Led: 5 Wrong Led Selection: Try again Enter 0
Execution :
Enter the integer:
for Red Led, for Yellow, for Green Led:
Wrong Led Selection: Try again
Enter to exit or to continue:
Enter the integer:
for Red Led, for Yellow, for Green Led:
Wrong Led Selection: Try again
Enter to exit or to continue:
End of Execution
Implement code in the provided template Python file to reflect the execution of the
instructor's completed Python script
Coding Architecture Rules:
a Read the comments provided in the template to implement code
b You have to implement the code based on the provided comments.
c You cannot change any code in the provided template file
d A variable name specified to be used in coding in a comment cannot be changed
e You cannot change the script flow provided in the template file.
f Any violations of the architecture rules will result in penalties, and if the violations
are significant, a zero grade will be assigned"""
A Program to turn on an user chosen LED in the Traffic Light,
and allow the user to select different leds repeatdely and
decide when to exit the program
#Import the library RPi.GPIO with alias GPIO
import RPi.GPIO as GPIO
#Import the sleep function from time module
from time import sleep
Setting the pin access mode to BCM
GPIO.setmodeGPIOBCM
Define a list collection type named leds and initialize
with the three GPIO pin numbers in broadcom mode to and
corresponding to red, yellow and green leds connected
Points:
Setup the three GPIO pins to be output pins
using the list variable leds
Points:
A boolean varible initialized to be used as the sentinel in the while loop"""
flagcont True
A While loop to execute continously until the user
decides to exit"""
whileflagcont:
Write code to prompt the user to select either
red or yellow or green led by entering
for Red Led
for Yellow Led
for Green Led.
Store the user entered integer in a variable named ledidx
Points:
printEnter the integer:
Write conditional statements if elif, and else using ledidx as
the sentinel to branch to a led operation.
Only in the if and elif blocks, the corresponding led should be turned
on for the user desired on time, and then turned off.
The GPIO pin should be selected using the list collection type variable
leds and the ledidx.
The else block should display that the user entered a wrong led
selection.
In the if and elif blocks, the user should be prompted to enter the
desired on time, and stored in an appropriate data type
In the if and elif blocks, after the led is turned off, display the led
was on for xx secs
Points:
Prompt the user to enter for exiting the program
or to continue.
Store the user entered value in the boolean variable flagcont.
Hint: An integer can be transformed into a boolean type False, and
an integer to boolean type True
Points:
printEnd of Execution'
GPIO.cleanup
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started