因此,我创建了以下代码来捕获Raspberry PI 3 B +的三个GPIO端子的输入。这段代码可以实现我想要的功能,除了似乎有一个缓冲区和第一个输入变为活动状态后要清除的event_detect系统。该程序的目的是为了进行游戏表演,就像人们试图同时嗡嗡作响的体验一样。但是只能有一个“第一”人来嗡嗡作响。程序应检测到第一个活动输入,而基本上忽略其余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()