【发布时间】:2021-05-13 23:55:12
【问题描述】:
我目前正在开发一个连接到树莓派的键盘。按下每个按钮时,都需要将一个字符串附加到列表中一次,然后重新启动循环,以便可以放置下一个输入。代码如下所示:
message = [] #final list will be stored here
loop = 0 #determines the position of each number inputted
while True:
if (GPIO.input(12) == GPIO.HIGH) and (GPIO.input(19) == GPIO.HIGH): #the character pad works in an array, when 2 "buttons" are pressed, they correspond do a location on the pad
message.insert(loop, "1") #inserts the number into the list
loop = loop + 1
if (GPIO.input(12) == GPIO.HIGH) and (GPIO.input(15) == GPIO.HIGH):
messege.insert(loop, "4")
loop = loop + 1
#this repeats for the other 14 buttons however the code is the same
如果按下“1”的按钮,然后按下“4”,则此代码的输出应该如下所示:
['1' '4']
但是,输出看起来像这样:
['1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '1...]
有没有办法让每个按钮每次按下激活一次?
这段代码中的按钮都是在pud_down中设置的
【问题讨论】:
-
这里需要的叫做“边缘检测”。您需要保持输入的当前状态,当状态与您的理解发生变化时,您就需要采取行动。请记住,只要按下按钮,GPIO 线就会保持有效状态,并且它们将被按下很多次循环。
标签: python button arraylist raspberry-pi gpio