from gpiozero import Button, LED
from time import time, sleep
from random import randint
led = LED(17) btn = Button(27)
while True:
btn.wait_for_release()
start = time()
led.on()
btn.wait_for_press()
end = time()
led.off()
print(end-start, 'seconds')
我想升级它包括按下按钮的时间窗口:
答案 0 :(得分:0)
来自docs:
wait_for_press(timeout=None)
参数:timeout(float) - 之前等待的秒数 诉讼。如果这是None(默认值),则无限期等待 直到设备处于活动状态。
这意味着您可以等待特定的timeout
,如下所示:
while True:
btn.wait_for_release()
start = time()
led.on()
btn.wait_for_press(5*60) # wait 5 minutes
end = time()
led.off()