python - How to interrupt a while loop, run a function and go back to the loop -
i'm beginning python , raspberry pi , trying make physical computing them.
i began that: https://www.raspberrypi.org/learning/physical-computing-with-python/ , went well.
than tried play around traffic lights (with success!): https://www.raspberrypi.org/learning/physical-computing-with-python/trafficlights/
here code:
from gpiozero import button, trafficlights time import sleep lights = trafficlights(25, 8, 7) while true: lights.green.on() print("green") sleep(12) lights.green.off() lights.amber.on() print("amber") sleep(4) lights.red.on() lights.amber.off() print("red") sleep(12) lights.amber.on() print("red & amber") sleep(4) lights.red.off() lights.amber.off()
than tried add button pedestrian crossing. here have problems.
here code:
from gpiozero import button, trafficlights time import sleep button = button(21) lights = trafficlights(25, 8, 7) def pedestrian_crossing(): sleep(4) lights.off() lights.amber.on() print("pedestrian crossing: amber") sleep(4) lights.red.on() lights.amber.off() print("pedestrian crossing: red") sleep(12) lights.amber.on() print("pedestrian crossing: red & amber") sleep(4) lights.red.off() lights.amber.off() button.when_pressed = pedestrian_crossing while true: lights.green.on() print("green") sleep(12) lights.green.off() lights.amber.on() print("amber") sleep(4) lights.red.on() lights.amber.off() print("red") sleep(12) lights.amber.on() print("red & amber") sleep(4) lights.red.off() lights.amber.off()
i tried button.wait_for_press
, button.wait_for_release
& co., button.is_pressed
1 gave me best result.
the problem when press button, function called, loop continues. how rewrite code , when press button loop stops, function called, inside function done , goes loop?
or there's solution, other button attributes?
thanks in advance!
how below, instead of call pedestrian crossing function directly when button press, set variable indicate pedestrian crossing press trigger, inside while loop call pedestrian_crossing function if being press return while loop
put checking pedestrian_press var @ my_sleep function check everytime my_sleep being call in while loop make ~1s delay interrupt
pedestrian_press = false def pedestrian_crossing(): global pedestrian_press pedestrian_press = false # reset press sleep(4) lights.off() lights.amber.on() print("pedestrian crossing: amber") sleep(4) lights.red.on() lights.amber.off() print("pedestrian crossing: red") sleep(12) lights.amber.on() print("pedestrian crossing: red & amber") sleep(4) lights.red.off() lights.amber.off() def set_pedestrian_press(): global pedestrian_press pedestrian_press = true button.when_pressed = set_pedestrian_press def my_sleep(n): # checking pedestrian press done @ 1 sec interval # make check @ smaller interval # change range(n*10) sleep(0.1), 0.1s interval in range(n): sleep(1) if pedestrian_press: # check if pedestrian press pedestrian_crossing() while true: lights.green.on() print("green") my_sleep(12) # every second check pedestrian_press lights.green.off() lights.amber.on() print("amber") my_sleep(4) # every second check pedestrian_press lights.red.on() lights.amber.off() print("red") my_sleep(12) # every second check pedestrian_press lights.amber.on() print("red & amber") my_sleep(4) # every second check pedestrian_press lights.red.off() lights.amber.off()
Comments
Post a Comment