Here's a link to what I have been trying a accomplish: https://projects.raspberrypi.org/en/projects/physical-computing/9
Near the bottom of the page it says, "Try adding the button for a pedestrian crossing. The button should move the lights to red (not immediately), and give the pedestrians time to cross before moving back to green until the button is pressed again."
This is what I currently have for code:
from gpiozero import Button, TrafficLights
from time import sleep
from signal import pause
button = Button(21)
lights = TrafficLights(4, 5, 6)
print("Normal operation")
def sleep1():
for i in range(100):
sleep(0.1)
if button.is_pressed:
sleep(4)
lights.off()
lights.amber.on()
sleep(3)
lights.amber.off()
lights.red.on()
print("Pedestian crossing")
sleep(10)
lights.off()
lights.green.on()
sleep(6)
while True:
lights.green.on()
sleep1()
lights.green.off()
lights.amber.on()
sleep(3)
lights.amber.off()
lights.red.on()
sleep(10)
lights.off()
It sort of does what I want it to do. I would like for the "while True" sequence to restart once the "Pedestrian" sequence is completed. Does anyone have any suggestions?
gpiozero
you'd usebutton.when_pressed
to set up a callback function: gpiozero.readthedocs.io/en/v1.2.0/recipes.html#button