目前,我的程序接受用户输入的闪烁长度和闪烁之间的延迟,然后在指定的时间内循环通过灯光。我有一个RGB灯也连接到Pi,我希望它根据灯闪烁的速度,RGB灯将改变颜色。由于程序现在,RGB灯将保持红色。我用不同的程序测试了RGB灯,它工作正常,所以我很确定错误在我的代码而不是布线。谢谢你的帮助!
# @kalenpw
import RPi.GPIO as GPIO
import time
#use raspberry pi pin numbers
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#GPIO output channels
GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
#RGB Light
GPIO.setup(15, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
#Make LED blink
def blink(pin):
GPIO.output(pin,1)
time.sleep(lightOnDuration)
GPIO.output(pin,0)
time.sleep(lightOffDuration)
return
#turn on RGB led based off blink duration
def RGBTurnOn():
#turn on red
if lightOnDuration < .5:
GPIO.output(15,1)
#turn on green
elif lightOnDuration < 2:
GPIO.output(13,1)
#turn on blue
else:
GPIO.output(11,1)
#Get number from user TODO fix
def getFloat(message):
while True:
try:
userInput = float(input(message))
except ValueError:
print("Error(01): please enter a number.")
continue
else:
return userInput
break
#Get integer from user TODO fix
def getInt(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Error(02): please enter an integer.")
continue
else:
return userInput
break
#Starts LED loop
def StartLoop():
RGBTurnOn()
for i in range(0,numberOfCycles):
blink(32)#5v green
blink(38)#5v yellow
blink(36)#5v blue
blink(40)#5v red
blink(37)#3v3 green
blink(35)#3v3 yellow
blink(33)#3v3 blue
blink(31)#3v3 red
#Get light on & off durations
lightOnDuration = getFloat("How man seconds for blink length?\n")
lightOffDuration = getFloat("How many seconds do you want between blinks?\n")
cycleDuration = (lightOnDuration + lightOffDuration) *8
cycleDurationString = str(cycleDuration) + " second(s)."
numberOfCycles = getInt("Enter total number of cycles please.\n"
"Each cycle will take aproximately " + cycleDurationString +"\n")
StartLoop()
GPIO.cleanup()
布线图片:
RGB:http://i.imgur.com/4RxSD24.jpg
LED:http://i.imgur.com/DmZRCVJ.jpg?1
左红色引脚31 gpio 6
LEFT blue pin 33 gpio 13
LEFT黄色针35 gpio 19
LEFT green pin 37 gpio 26
右红色针脚40 gpio 21
右蓝色针脚36 gpio 16
右黄色针38 gpio 20
右绿色针脚32 gpio 12
rgb red pin pin 15 gpio 22
rgb green pin 13 gpio 27
rgb blue pin 11 gpio 17
答案 0 :(得分:1)
你的编程对我来说似乎是正确的。
只是猜测:
从图片中我看到你的RGB-LED是共阴极型。因此,您应该为每个LED阳极添加一个单独的电阻 - 而不是为阴极添加一个电阻。 你没有,也许因此,红色LED吸收所有可用电流(如果绿色或蓝色设置为ON,则红色GPIO的状态不清楚。)
尝试在启动时或方法中将不需要的RGB引脚的GPIO设置为OFF。
def RGBTurnOn():
GPIO.output(15,0)
GPIO.output(13,0)
GPIO.output(11,0)
#turn on red
if lightOnDuration < .5:
GPIO.output(15,1)
#turn on green
elif lightOnDuration < 2:
GPIO.output(13,1)
#turn on blue
else:
GPIO.output(11,1)