Page 1 of 1

You are asked to build a circuit that consists of 5 LEDs (any colours) with appropriate resistors, a button and a LCD as

Posted: Sun Apr 10, 2022 8:53 am
by answerhappygod
You are asked to build a circuit that consists of 5 LEDs (any
colours) with appropriate resistors, a button and a LCD as shown in
the demo video. Arrange the LEDs in a line and keep the breadboard
as neat as possible. Create a Python program which will light up
the LEDs in a travelling pattern as shown in the demo video. Start
the pattern at a pace of your choice. Pressing the button will
increase or decrease the pattern speed in steps (you choose the
direction), starting over when maximum or minimum is reached. The
LCD needs to show the pattern speed in percent, which needs to be
updated with each button press. Please watch the following video to
see how to use the LCD1602 display:
While you are free to pursue your own solution, it is recommended
that you write your program on the base of the a2.py
skeleton . Follow the TODO lines to complete the missing
pieces, deleting the TODO lines after they are done. Do not forget
to verify your completed program using pycodestyle. Make sure to
read the Grading Rubric before submitting your work for
grading.
a2.py Skeleton -
#!/usr/bin/python3
from gpiozero import PWMLED, Button
from signal import pause, signal, SIGTERM, SIGHUP
from time import sleep
#TODO: import Threading, if used (see:

#TODO: import LCD library (see:
delay = 0.1
active = True
#TODO: fill in GPIO pin numbers for LEDs below, matching
breaboard connections
leds = (PWMLED(), PWMLED(), PWMLED(), PWMLED(), PWMLED())
#TODO: declare Button object
#TODO: declare LCD object
def cleanup(signum, frame):
exit(1)
def change_speed():
global delay
#TODO: change delay in 5 steps, for example
between 0.1 and 0.5 seconds
#TODO: update LCD
def show_pattern():
try:
while active:
for num in (0, 1, 2, 3,
4, 3, 2, 1):

leds[num].on()
#TODO:
sleep based on delay variable

leds[num].off()
except AttributeError:
pass
try:
signal(SIGTERM, cleanup)
signal(SIGHUP, cleanup)
#TODO: register button press callback with
change_speed function
#TODO: start the show_pattern function, preferably
as a thread
pause()
except KeyboardInterrupt:
pass
finally:
active = False
#TODO: join thread, if used
#TODO: clear LCD
#TODO: close all LEDs
sleep(0.25)