按下按钮时GPIO事件检测未提供输出 [英] GPIO event detect not giving output when button pressed

查看:70
本文介绍了按下按钮时GPIO事件检测未提供输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下python脚本应该等待按钮按下,打印按钮按下消息,然后退出.

The following python script is supposed to wait for a button press, print a button press message, and then exit.

但是,当我按下按钮时,什么也不会打印.然后,当我按下Enter键时,脚本会在屏幕上显示检测到按钮按下".然后停止.

However, when I press the button nothing is printed. Then when I press the enter key, the script prints "Button push detected" then stops.

如何解决此代码,以便在按下按钮时打印出检测到按钮按下"?

How might I fix this code so that 'Button push detected' is printed when I push the button?

我按照教程制作了这段代码:

I followed a tutorial to make this code:

#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()

推荐答案

问题是您的脚本需要一个循环,该循环等待直到按下按钮为止,以便在检测到按钮按下时,您的脚本将能够对事件.

The problem is that your script needs a loop that waits until the button is pressed so that when the button press is detected, your script will be able to react to the event.

现在,您的脚本设置了事件检测功能,但随后会等待输入,一旦输入,您的脚本就会退出.

Right now your script sets the event detection but then sits waiting for input and as soon as the input is made, your script exits.

请参阅此Raspberry Pi论坛帖子, https://www.raspberrypi.org/forums/viewtopic.php?t = 201747

See this Raspberry Pi forum post, https://www.raspberrypi.org/forums/viewtopic.php?t=201747

具有一个程序,该程序可以通过按下按钮来打开和关闭LED.

which has a program for lighting an LED on and off with button presses.

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

However it looks to me like you would probably need to make a change to the call back function to raise an exception rather than light the LED if you want the script to end when you press the button.

请参阅下面的经过修改的程序,其中带有一些其他注释以及注释掉的LED灯/不亮灯.

See below the modified program with some additional annotation along with the LED light/unlight commented out.

但是,此修改后的程序使用了一个忙循环,该忙循环一直持续运行,直到按下按钮为止.使用繁忙循环虽然对诸如您的程序这样的简单程序有效,但这通常不是一个好主意.

However this modified program uses a busy loop that just runs continuously until the button is pressed. Using a busy loop, while it works for simple programs such as yours, is generally not a good idea.

一种替代方法是使用 GPIO.wait_for_edge()函数,该函数将暂停脚本,直到按下按钮为止.请参见 Raspberry Pi StackExchange-暂停执行代码,直到按下按钮为止删除忙循环.

An alternative is to use the GPIO.wait_for_edge() function that will pause the script until the button is pressed. See Raspberry Pi StackExchange - Pausing code execution till a button is pressed which removes the busy loop.

首先是繁忙循环版本.

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()      

请参阅有关这些论坛帖子中发生的情况的解释

See as well the explanations as to what is happening in these forum 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/

And this article about exceptions in python. https://realpython.com/python-exceptions/

这篇关于按下按钮时GPIO事件检测未提供输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆