是否需要将GPIO.IN链接到RPi.GPIO中的GPIO.OUT才能正常工作?

问题描述 投票:0回答:1

我当时在使用GPIO的raspbian模拟器上编写代码。我能够使其正常工作,但是由于某些原因,当我更改GPIO.HIGH和条件语句中的print语句的顺序时,它无法正常运行,并且在第一次单击后停止。有人知道这是模拟器的问题,还是树莓派的一个属性并将其连接到硬件吗?如果我不将GPIO.IN与GPIO.OUT链接,它也将根本无法工作。

此gif显示我更改订单或删除订单时会发生什么-它是第一次打开,但此后不会关闭或重新打开。由于某种原因,它使它脱离了while循环。

gif of activity

这是我使用的代码:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
import time
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
prev_input1 = 0

inputs = [11, 13, 15]
outputs = [3, 5, 7]
GPIO.setup(inputs, GPIO.IN)
GPIO.setup(outputs, GPIO.OUT)

secs = 0

def main():
  while True:
    button_press(15, 7)
    timer = add_secs(11, 5, 2)
    #print(timer)


def button_press(button1, button2):
  #take a reading
  global prev_input
  inputa = GPIO.input(button1)
  #if the last reading was low and this one high, print
  if ((not prev_input) and inputa):
    print("Light on")
    GPIO.output(button2, GPIO.HIGH)   #code does not work if I remove/reorder this statement
  if((not inputa) and prev_input):
    print("Light off")
    GPIO.output(button2, GPIO.LOW)      #code does not work if I remove/reorder this statement
  #update previous input
  prev_input = inputa
  #slight pause to debounce
  time.sleep(0.05)

def add_secs(button1, button2, num):
  global prev_input1
  secs = 0
  inputa = GPIO.input(button1)
  if((not prev_input1) and inputa):
    secs = num
    print(secs)
    GPIO.output(button2, GPIO.LOW)      #code does not work if I remove/reorder this statement
  prev_input1 = inputa
  time.sleep(0.05)
  return secs


main()
raspberry-pi emulation raspbian gpio
1个回答
0
投票

我联系了仿真器的开发人员,他让我知道仿真器存在问题,现已解决。

清除chrome中的浏览数据并重新运行程序后,代码即可正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.