我试图让这些GPIO端口同时打开和关闭,但是在RPi上以不同的间隔打开和关闭。我可以运行其中一个循环并且它可以工作,但是当我引入线程时它不会。
import RPi.GPIO as GPIO
from time import sleep
import thread
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
def fast():
while True:
GPIO.output(11, True)
sleep(.02)
GPIO.output(11, False)
sleep(.02)
def med():
while True:
GPIO.output(13, True)
sleep(.2)
GPIO.output(13, False)
sleep(.2)
def slow():
while True:
GPIO.output(15, True)
sleep(2)
GPIO.output(15, False)
sleep(2)
thread.start_new_thread(fast,())
thread.start_new_thread(med,())
thread.start_new_thread(slow,())
答案 0 :(得分:4)
这是因为没有主程序/循环。你的代码启动那些线程,但随后它将到达代码的末尾并退出运行python的进程,从而终止线程。因此,可以在底部添加raw_input("Press enter to exit")
。