【问题标题】:Raspberry Pi GPIO with python树莓派 GPIO 与 python
【发布时间】:2015-06-18 13:54:02
【问题描述】:

我被困在这个与工作相关的项目上。我想要完成的是,在按下特定数量的按钮后,我希望我的程序结束。我正在考虑循环。

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

    input_state = GPIO.input(18)
    if input_state == False:
            print('Button Pressed')
            time.sleep(0.2)

Max_Presses = 100
button_pressed  = 0

def button_pressed():
    return input_state()

 while button_pressed < Max_Presses:

    print 'still going'
    if button_pressed():
            button_pressed += 1

【问题讨论】:

  • 您是否已经尝试过?显示代码
  • 我把上面的代码贴出来了

标签: python raspberry-pi gpio


【解决方案1】:

是的,while 循环听起来是必要的。您将需要一个跟踪按钮按下次数的计数器。检查计数器作为您的循环条件,例如

MAX_PRESSES = 10
button_presses = 0

def button_pressed():
     return poll_GPIO_pin_or_whatever()   # something that detects button presses

while button_presses < MAX_PRESSES:
    print 'still going'
    if button_pressed():
        button_presses += 1

检查功能可能通过轮询 GPIO 引脚来工作,或者可能有某种回调机制适用于您的环境。

【讨论】:

  • 谢谢,这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多