我正在尝试打开三个不同的LED,但我希望它们在不同的时间关闭,但这看起来不可能......:哭: 这是我的代码:
import RPi.GPIO as GPIO
import time from threading
import Thread
def led1():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.output(36,1)
time.sleep(3)
GPIO.output(36,0)
GPIO.setwarnings(False)
def led2(): GPIO.setmode(GPIO.BOARD)
GPIO.setup(38, GPIO.OUT)
GPIO.output(38,1)
time.sleep(5)
GPIO.output(38,0)
GPIO.setwarnings(False)
def led3():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40,1)
time.sleep(7)
GPIO.output(40,0)
GPIO.setwarnings(False)
thread1= Thread(target = led1)
thread2= Thread(target = led2)
thread3= Thread(target = led3)
thread1.run() thread2.run()
thread3.run()
##thread1.join () // not sure if I have to put that or not
##thread2.join()
##thread3.join()
GPIO.cleanup()
答案 0 :(得分:0)
为什么不做这样的事情?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(36,1)
GPIO.output(38,1)
GPIO.output(40,1)
time.sleep(3)
GPIO.output(36,0)
time.sleep(2)
GPIO.output(38,0)
time.sleep(2)
GPIO.output(40,0)
GPIO.cleanup()
编辑:如果你不知道每个led的开启次数:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
GPIO.setwarnings(False)
led1time = 3
led2time = 5
led3time = 7
m = max(led1time, led2time, led3time)
GPIO.output(36,1)
GPIO.output(38,1)
GPIO.output(40,1)
t = 0
while t <= m:
if t >= led1time:
GPIO.output(36,0)
if t >= led2time:
GPIO.output(38,0)
if t >= led3time:
GPIO.output(40,0)
time.sleep(0.1)
t += 0.1
GPIO.cleanup()
现在,您可以设置每个LED的开启时间,准确度为0.1秒,您可以通过用较低的数字替换任何出现的0.1来提高准确度。