按下按钮时 GPIO 事件检测不提供输出

GPIO event detect not giving output when button pressed

下面的 python 脚本应该等待按钮按下,打印按钮按下消息,然后退出。

但是,当我按下按钮时,什么也没有打印出来。然后当我按下回车键时,脚本打印“检测到按钮按下”然后停止。

我该如何修复此代码,以便在我按下按钮时打印 'Button push detected'?

我按照教程编写了这段代码:

#Button input detection
#by 
#Start date: 11th February 2021
#End date: 11th February 2021

#Importing GPIO andtime libraries as per usual with a python script making use of RPi GPIO
import RPi.GPIO as GPIO
import time

#Callback function to print 'Button push detected' when called
def button_callback(channel):
    print("Button push detected")
    
#Disabling annoying GPIO warnings and setting GPIO mode to board
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#GPIO event detector that detects a rising edge on pin 11
GPIO.add_event_detect(11,GPIO.FALLING,callback=button_callback)

#Command that does not stop until someone presses enter
quit = input("Press enter to quit")

#Clean up the pins used
GPIO.cleanup()

问题是您的脚本需要一个等待按钮被按下的循环,以便在检测到按钮按下时,您的脚本将能够对事件做出反应。

现在您的脚本会设置事件检测,但会等待输入,一旦输入完成,您的脚本就会退出。

查看此 Raspberry Pi 论坛 post、https://www.raspberrypi.org/forums/viewtopic.php?t=201747

它有一个通过按下按钮点亮和关闭 LED 的程序。

但是在我看来,如果您希望脚本在您按下按钮时结束,您可能需要更改回调函数以引发异常而不是点亮 LED。

请参阅下面修改后的程序,其中包含一些附加注释以及注释掉的 LED light/unlight。

然而,这个修改后的程序使用了一个繁忙的循环,它会持续运行直到按下按钮。使用忙循环虽然适用于像您这样的简单程序,但通常不是一个好主意。

另一种方法是使用 GPIO.wait_for_edge() 函数来暂停脚本,直到按下按钮。请参阅 Raspberry Pi StackExchange - Pausing code execution till a button is pressed,它消除了繁忙的循环。

首先是忙循环版本。

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

btn_input = 4;     # button to monitor for button presses.
LED_output = 17;   # LED to light or not depending on button presses.

# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
# GPIO.setup(LED_output, GPIO.OUT)

# handle the button event
def buttonEventHandler_rising (pin):
    # turn LED on
    # GPIO.output(LED_output,True)
    raise Exception('button pressed')
    
def buttonEventHandler_falling (pin):
    # turn LED off
    # GPIO.output(LED_output,False)
    raise Exception('button released')


# set up the event handlers so that when there is a button press event, we
# the specified call back is invoked.
# for your purposes you may only want to detect the falling
# indicating that the button was released.
# GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler_rising) 
GPIO.add_event_detect(btn_input, GPIO.FALLING, callback=buttonEventHandler_falling)
 
# we have now set our even handlers and we now need to wait until
# the event we have registered for actually happens.
# This is an infinite loop that waits for an exception to happen and
# and when the exception happens, the except of the try is triggered
# and then execution continues after the except statement.
try:  
    while True : pass  
except:
    GPIO.cleanup()      

请参阅有关这些论坛中发生的事情的解释 posts

https://www.raspberrypi.org/forums/viewtopic.php?t=128510

https://www.raspberrypi.org/forums/viewtopic.php?t=141520

还有这篇关于 python 中的异常的文章。 https://realpython.com/python-exceptions/