我有一个简单的设置,有 3 个按钮和一个 led。我想调节按钮的闪烁,每个按钮都分配了闪烁的"持续时间"。我的程序等到闪烁部分完成,直到它读取输入。我尝试了多处理,但我无法使其工作。
我还实施了光功率调节,但它工作正常。
这是"工作"代码:
import pyfirmata
import time
board = pyfirmata.Arduino('COM3')
it = pyfirmata.util.Iterator(board)
it.start()
analA = board.get_pin('a:0:i')
wejA = board.get_pin('d:2:i')
wejB = board.get_pin('d:4:i')
wejC = board.get_pin('d:7:i')
led = board.get_pin('d:10:p')
x = False
y = False
z = False
f = 1
while True:
x = wejA.read()
y = wejB.read()
z = wejC.read()
analog = analA.read()
if x == True:
f = 3
print(analog, x, y, z, f)
if y == True:
f = 1
print(analog, x, y, z, f)
if z == 1:
f = 0.3
print(analog, x, y, z, f)
led.write(analog)
time.sleep(f)
led.write(0)
time.sleep(f)
我盲目地尝试了多处理,但我失败了,这是我的尝试:
import pyfirmata
import time
import multiprocessing
from multiprocessing import Process
board = pyfirmata.Arduino('COM3')
it = pyfirmata.util.Iterator(board)
it.start()
analA = board.get_pin('a:0:i')
wejA = board.get_pin('d:2:i')
wejB = board.get_pin('d:4:i')
wejC = board.get_pin('d:7:i')
led = board.get_pin('d:10:p')
x = False
y = False
z = False
f = 1
def wejscia():
x = wejA.read()
y = wejB.read()
z = wejC.read()
analog = analA.read()
if x == True:
f = 3
print(analog, x, y, z, f)
if y == True:
f = 1
print(analog, x, y, z, f)
if z == 1:
f = 0.3
print(analog, x, y, z, f)
def wyjscia():
analog = analA.read()
led.write(analog)
time.sleep(f)
led.write(0)
time.sleep(f)
while True:
p1 = Process(target=wejscia)
p1.start()
p2 = Process(target=wyjscia)
p2.start()
p1.join()
p2.join()
如何使程序检查输入并同时执行输出?
我刚刚开始编程,所以任何建议都值得赞赏。
前段时间我遇到了同样的问题。 添加这个帮助我修复了它。
pin.enable_reporting()
这实质上是侦听输入引脚并报告其值。 所以我认为在初始化引脚时添加它可以解决问题。
wejA.enable_reporting()
wejB.enable_reporting()
wejC.enable_reporting()