GPIO.add_event_detect() 检测到连续按下错误的按钮

GPIO.add_event_detect() is detecting a false button press continuously

我的问题是:GPIO.add_event_detect() 在无限循环中连续检测到错误的上升沿,并且 运行 call_back function() 无限,即使我一次都没有按下连接到 GPIO 25 的按钮 但是 call_back function() 一直在执行。

这是我的代码,我想在其中调用包含函数 WhatsApp(lat_o,long_o) 的 call_back 函数 x1() 仅当按下按钮时WhatsApp(lat_o,long_o) 在我不按下按钮的情况下继续执行。此外,我将 WhatsApp(lat_o,long_o) 放在 x1() 中以消除将参数传递给 call_back 函数的问题。

#                           INTERRUPTS (NOT WORKING)

# Sample Coordinates
lat_o=33
long_o=72

# Import Libraries
import RPi.GPIO as GPIO
from time import sleep

def x1(channel):
    WhatsApp(lat_o,long_o)

# Configure GPIO of Rpi
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
button1 = 25 # For WhatsApp

# Setup GPIO for Whatsapp
GPIO.setup(button1, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # Set GPIO 25 (pin 22) to be an input pin for WhatsApp

# Detect button1-press
GPIO.add_event_detect(button1 ,GPIO.RISING, callback = x1)


try:  
    while True : pass  
except:
    GPIO.cleanup()

请帮忙! 我不想在我的最后一年项目的最终代码中使用轮询(即,在 while 循环中使用 if-else)来执行 WhatsApp(lat_o,long_o),因为我希望 GPIO 检测连续按下按钮并在此处使用轮询会耗尽我的 Raspberry Pi 4.

的大量电量

根据this关于raspsberrypi的讨论

A rising edge will be detected at approx. 1.25V. However, when the voltage lowers again and the input voltage drops below the 1.16V level, a stream of interrupts will start, and continue until the level is below 1.12V.

This is most dramatic when slower rising or falling edges are present, as an example, when excessive anti-debounce measures have been taken.

这是给出的解决方案

In software, I use two other techniques to get rid of the false hits. First, after the edge has been recognized by the system, we don't know if it is the one we expect. By waiting for 5mSec, and then reading the input again but now with a Logic Level command, we can determine if the edge is indeed what we expect and we then avoid and discard all other (false) edges.

最后是上升沿的代码

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

Input_Sig = 23 # any plain GPIO pin
# if there is no external pull, use the internal one (pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(Input_Sig, GPIO.IN)


def interrupt_service_routine(Input_Sig):
    sleep(0.005) # edge debounce of 5mSec
    # only deal with valid edges
    if GPIO.input(Input_Sig) == 1:
        print("RISING")
    return

# define the event; bountime of 5mSec means that subsequent edges will be ignored for 5mSec
GPIO.add_event_detect(Input_Sig, GPIO.RISING, callback=interrupt_service_routine, bouncetime=5)


def main():

    try:
        while True:
            pass # your code

    except KeyboardInterrupt:
        pass
    finally:
        print("\nRelease the used pin(s)")
        GPIO.cleanup([Input_Sig])


if __name__ == '__main__':
    main()