Raspberry Pi捕获GPIO输入并忽略其他输入10秒

2024-04-29 10:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我创建了以下代码来捕获来自Raspberry PI3B+的三个GPIO终端的输入。这个代码做我想要的,除了有一个缓冲区和事件检测系统,我想清除一旦第一个输入成为活动。这个程序的目的是为了一个游戏节目一样的人试图在同一时间蜂拥而至的经验。但只能有一个“第一”人蜂拥而至。程序应该检测到第一个激活的输入,并在10秒内基本忽略其余的输入

PS-我对Python和Raspberry pi的细节非常熟悉

PPS-我已经将计时器缩短到3秒,以便于故障排除

# External module imports
from guizero import App, Text, TextBox, PushButton
import RPi.GPIO as GPIO
import sys
import time

# This procedure will watch three inputs and lock in the team that calls in first for 10seconds
# Change the colors of the buttons to show which team pressed in first.

def action1(dunno):
        # Team1 button pressed
        team1_btn.bg="green"
        team2_btn.bg="red"
        team3_btn.bg="red"
        sleep10()

def action2(dunno):
        # Team1 button pressed
        team1_btn.bg="red"
        team2_btn.bg="green"
        team3_btn.bg="red"
        sleep10()

def action3(dunno):
        # Team1 button pressed
        team1_btn.bg="red"
        team2_btn.bg="red"
        team3_btn.bg="green"
        sleep10()

def sleep10():
        #sleep for 10 seconds
        delay_timer.visible=True
        for i in range(3,0,-1):
                delay_timer.value=str(i)
                time.sleep(1)
        delay_timer.visible=False
        team1_btn.bg="light grey"
        team2_btn.bg="light grey"
        team3_btn.bg="light grey"


# Pin setup
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(16, GPIO.RISING, callback=action1, bouncetime=1800)
GPIO.add_event_detect(20, GPIO.RISING, callback=action2, bouncetime=1800)
GPIO.add_event_detect(21, GPIO.RISING, callback=action3, bouncetime=1800)


app = App(title="A07 DSS Tri-State - Jeopardy", width=700)
blank1 = Text(app, text="")
welcome_message = Text(app, text="This..      is..       Jeopardy!!", size=30, font="Times New Roman", color="blue")

blank2 = Text(app, text="")
blank3 = Text(app, text="")

team1_btn = PushButton(app, text="Team Black & Blue")
team1_btn.bg="light grey"
blank4 = Text(app, text="")
team2_btn = PushButton(app, text="Team Holiday Spirit")
team2_btn.bg="light grey"
blank5 = Text(app, text="")
team3_btn = PushButton(app, text="Team Creamsicle")
team3_btn.bg="light grey"
blank6 = Text(app, text="")
delay_timer = Text(app, text="0")
delay_timer.visible=False

app.display()


Tags: textimportappgpioredgreylightbg