【问题标题】:Raspberry Pi blinking LED every time button is on每次按钮打开时,Raspberry Pi 都会闪烁 LED
【发布时间】:2019-09-27 19:09:34
【问题描述】:

我有一个连接到拨动开关和 LED 的树莓派。每个开关打开一个 LED,第三个开关激活它对应的 LED,并再次闪烁 20 秒。目前,python 脚本将允许您根据需要关闭和打开按钮,但最后一个 while 参数仅在第一次运行。每次打开 GPIO 25 时,如何将其更改为使第三个 LED 闪烁?

t_end = time.time() + 20
while(1):

    GPIO.wait_for_edge(23, GPIO.FALLING)
    print ("Phase 1 Initiated")
    sounda.play()

    GPIO.wait_for_edge(24, GPIO.FALLING)
    print ("Phase 2 Initiated")
    soundb.play()

    GPIO.wait_for_edge(25, GPIO.FALLING)
    print ("Phase 3 Initiated")
    soundc.play()
    while time.time() < t_end:
        GPIO.output(6, GPIO.HIGH) 
        sleep(.5)
        GPIO.output(6, GPIO.LOW) 
        sleep(.5) 

【问题讨论】:

    标签: python switch-statement gpio led pi


    【解决方案1】:

    您在外部 while 循环之前将 t_end 设置为等于 time.time() + 20,之后不再更新它,这样一旦 20 秒过去了,time.time() 就等于然后大于 @987654324 @,您的内部 while time.time() &lt; t_end while 循环中的代码将永远不会再次执行。

    因为这段代码中有两个sleep(.5) 语句

    while time.time() < t_end:
        GPIO.output(6, GPIO.HIGH) 
        sleep(.5)
        GPIO.output(6, GPIO.LOW) 
        sleep(.5)  
    

    意味着每个闪烁周期需要一秒钟,因为 LED 亮了半秒然后熄灭了半秒,为什么不直接用 for i in range(20): 替换 while time.time() &lt; t_end:,然后你会得到 20 秒的闪烁而不需要处理任何额外的时间变量。

    【讨论】:

    • 这效果好多了,谢谢。我将它绑定到一个线程函数,现在相机和 led 的工作没有时间的复杂性。时间
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多