当 gpio.input(i) = true 时,我试图让“p”触发 0 和 1
谁能告诉我这段代码有什么问题 - 它不起作用!
import RPi.GPIO as gpio
import time as time
gpio.setmode(gpio.BOARD)
i = 15
o = 11
t = 0.3
p = 0
gpio.setup(i, gpio.IN)
gpio.setup(o, gpio.OUT)
gpio.output(o, True)
while True:
if gpio.input(i) and p == 0:
print("P = 1")
p = 1
time.sleep(2)
else:
if (gpio.input(i)) and p == 1:
p = 0
while True:
if p == 1:
gpio.output(o, False)
time.sleep(t)
gpio.output(o, True)
time.sleep(t)
最佳答案
我的猜测是:gpio.input(i) 读取输入,然后输入消失,所以在 else 部分它的计算结果为 false。
试试这个:
if gpio.input(i):
if p == 0:
p = 1
else:
p = 0
关于python - 如何为变量赋值作为 if 语句的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18890198/