【问题标题】:How can I assign multiple functions to a single botton on Raspberry?如何将多个功能分配给 Raspberry 上的单个按钮?
【发布时间】: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


    【解决方案1】:

    错误是由于使用了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
    ...
    

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 1970-01-01
      • 2013-12-28
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多