如何将多个功能分配给 Raspberry 上的单个按钮?
How can I assign multiple functions to a single botton on Raspberry?
我正在尝试将多个功能分配给一个按钮。如果我按一次按钮,它会做一件事,如果我按两次它会做另一件事,依此类推。
这是我的程序:
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.IN)
pressed = 0;
timer = 0;
while True:
input_value = gpio.input(10)
if input_value == True:
pressed += 1;
time = 0; #to start the counter at 0
if (time > 10): #you wait 1 sec between each presure
print("the button has been pressed " + pressed + " times");
pressed = 0; # you don't count anymore
if (pressed > 0): # you are pressing the button so you count
time += 1;
if (pressed == 1):
print("t=1"); # do something
if (pressed == 2):
print("t=2"); # do something
if (pressed == 3):
print("t=3"); # do something
time.sleep(0.1)
我收到这个错误:
Traceback (most recent call last):
File "/home/pi/Desktop/button.py", line 16, in <module>
if (time > 10): #you wait 1 sec between each presure
TypeError: '>' not supported between instances of 'module' and 'int'
有人知道我怎么解决吗?
错误是由于使用 time
而不是 timer
。
...
...
if input_value == True:
pressed += 1;
timer = 0; #to start the counter at 0
if (timer > 10): #you wait 1 sec between each presure
print("the button has been pressed " + pressed + " times");
pressed = 0; # you don't count anymore
...
我正在尝试将多个功能分配给一个按钮。如果我按一次按钮,它会做一件事,如果我按两次它会做另一件事,依此类推。 这是我的程序:
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.IN)
pressed = 0;
timer = 0;
while True:
input_value = gpio.input(10)
if input_value == True:
pressed += 1;
time = 0; #to start the counter at 0
if (time > 10): #you wait 1 sec between each presure
print("the button has been pressed " + pressed + " times");
pressed = 0; # you don't count anymore
if (pressed > 0): # you are pressing the button so you count
time += 1;
if (pressed == 1):
print("t=1"); # do something
if (pressed == 2):
print("t=2"); # do something
if (pressed == 3):
print("t=3"); # do something
time.sleep(0.1)
我收到这个错误:
Traceback (most recent call last):
File "/home/pi/Desktop/button.py", line 16, in <module>
if (time > 10): #you wait 1 sec between each presure
TypeError: '>' not supported between instances of 'module' and 'int'
有人知道我怎么解决吗?
错误是由于使用 time
而不是 timer
。
...
...
if input_value == True:
pressed += 1;
timer = 0; #to start the counter at 0
if (timer > 10): #you wait 1 sec between each presure
print("the button has been pressed " + pressed + " times");
pressed = 0; # you don't count anymore
...