raspberry-pi - GPIO.IN 是否需要链接到 RPi.GPIO 中的 GPIO.OUT 才能正常工作?
问题描述
我正在使用 GPIO 在 raspbian 模拟器上编写代码。我能够让它工作,但由于某种原因,当我更改 GPIO.HIGH 的顺序和条件中的打印语句时,它无法正常运行并在第一次单击后停止。任何人都知道这是模拟器的问题还是只是树莓派的属性并将其连接到硬件?如果我不将 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中的浏览数据,重新运行程序后,代码运行正常。
推荐阅读
- python - 如何仅注释堆叠条形图的一个类别
- c# - Blazor 应用:使用 AddAuthentication 时,Container.ReadItemAsync 无法连接到 Azure 数据库
- css - 阴影不在单独的类中工作,只有一般
- javascript - Vue 3 第三方组件是否与 Vue 2 兼容?
- javascript - 忽略正则表达式中的组匹配
- safari - 使用 iOS 的可见性层问题(使用 mapbox-gl)
- php - 如何在刀片视图中显示来自不同表的元素列表?
- javascript - 如何配置 monaco 编辑器只允许使用 es5 脚本(防止使用 let 和 const)
- amazon-web-services - 运行 Spark 作业后创建和销毁集群的步骤函数
- apache-spark - 在 DataFrame 中转义换行符