简体   繁体   English

如何中断while循环,运行函数并返回循环

[英]How to interrupt a while loop, run a function and go back to the loop

I'm beginning with Python and Raspberry Pi and was trying to make some Physical Computing with them. 我从Python和Raspberry Pi开始,并尝试使用它们进行一些物理计算。

I began with that: https://www.raspberrypi.org/learning/physical-computing-with-python/ and everything went well. 我从那开始: https : //www.raspberrypi.org/learning/physical-computing-with-python/一切顺利。

Than I tried to play around with traffic lights (with success!): https://www.raspberrypi.org/learning/physical-computing-with-python/trafficlights/ 比我尝试玩交通信号灯(成功!): https : //www.raspberrypi.org/learning/physical-computing-with-python/trafficlights/

Here my code: 这是我的代码:

from gpiozero import Button, TrafficLights
from 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 I tried to add a button for a pedestrian crossing. 比我尝试为人行横道添加一个按钮。 But here I have problems. 但是我这里有问题。

Here the code: 这里的代码:

from gpiozero import Button, TrafficLights
from 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 with button.wait_for_press , button.wait_for_release & Co., but button.is_pressed was the one that gave me the best result. 我尝试使用button.wait_for_pressbutton.wait_for_release &Co.,但是button.is_pressed是给我最好结果的那个。

The problem is that when I press the button, the function is called, but the loop continues. 问题是当我按下按钮时,该函数被调用,但是循环继续。 So how could I rewrite the code and than when I press the button the loop stops, the function is called, everything inside the function is done and then it goes back to the loop? 因此,我该如何重写代码,而不是按下按钮时,循环停止,调用函数,完成函数内部的所有操作,然后返回循环?

Or there's another solution, with the other button attributes? 或者还有其他按钮属性的解决方案?

Thanks in advance! 提前致谢!

How about like below, Instead of call the pedestrian crossing function directly when button press, set a variable to indicate pedestrian crossing press trigger, then inside while loop will call pedestrian_crossing function if being press then return to while loop 如下所示,不是在按下按钮时直接调用行人过路功能,而是设置一个变量来指示行人过路按下触发器,然后在while循环内,如果被按下,则会调用walker_crossing功能,然后返回while循环

put the checking for pedestrian_press var at my_sleep function so it will get check everytime my_sleep being call in while loop that make it almost ~1s delay interrupt 将check_walker_press var放在my_sleep函数中,以便每次在while循环中调用my_sleep时都会得到检查,这使得它几乎延迟了约1秒的延迟中断

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 for pedestrian press done at 1 sec interval
    # to make it check at smaller interval
    # change to range(n*10) then sleep(0.1), for 0.1s interval
    for i 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 will check for pedestrian_press
    lights.green.off()
    lights.amber.on()
    print("AMBER")
    my_sleep(4) # every second will check for pedestrian_press
    lights.red.on()
    lights.amber.off()
    print("RED")
    my_sleep(12) # every second will check for pedestrian_press
    lights.amber.on()
    print("RED & AMBER")
    my_sleep(4) # every second will check for pedestrian_press
    lights.red.off()
    lights.amber.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM