【发布时间】:2022-01-01 18:54:23
【问题描述】:
我正在尝试将多个功能分配给一个按钮。如果我按下按钮一次,它会做一件事,如果我按下它两次,它会做另一件事,依此类推。 这是我的程序:
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'
谁知道怎么解决?
【问题讨论】:
标签: python raspberry-pi gpio