GPIO.IN 是否需要链接到 RPi.GPIO 中的 GPIO.OUT 才能正常工作?
Does GPIO.IN need to be linked to GPIO.OUT in RPi.GPIO in order for it to work properly?
我正在使用 GPIO 在 raspbian 模拟器上编写代码。我能够让它工作,但由于某种原因,当我更改 GPIO.HIGH 的顺序和条件中的打印语句时,它没有正确地 运行 并在第一次单击后停止。任何人都知道这是模拟器的问题还是只是 raspberry pi 的 属性 并将其连接到硬件?如果我不 link GPIO.IN 和 GPIO.OUT,它也根本不起作用。
此 gif 显示了当我更改顺序或删除时发生的情况 - 它第一次打开,但之后不会关闭或再次打开。由于某种原因,它使它脱离了 while 循环。
这是我使用的代码:
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()
我联系了模拟器的开发人员,他告诉我模拟器存在问题,现在已解决。
清除chrome中的浏览数据,重新运行程序后代码运行正常。
我正在使用 GPIO 在 raspbian 模拟器上编写代码。我能够让它工作,但由于某种原因,当我更改 GPIO.HIGH 的顺序和条件中的打印语句时,它没有正确地 运行 并在第一次单击后停止。任何人都知道这是模拟器的问题还是只是 raspberry pi 的 属性 并将其连接到硬件?如果我不 link GPIO.IN 和 GPIO.OUT,它也根本不起作用。
此 gif 显示了当我更改顺序或删除时发生的情况 - 它第一次打开,但之后不会关闭或再次打开。由于某种原因,它使它脱离了 while 循环。
这是我使用的代码:
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()
我联系了模拟器的开发人员,他告诉我模拟器存在问题,现在已解决。
清除chrome中的浏览数据,重新运行程序后代码运行正常。