簡體   English   中英

如何將多個功能分配給 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
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM