我使用树莓派,tip120晶体管和python 2.7来控制LED灯条,除了颜色之间的淡入淡出外,一切都正常工作。我试图根据温度读数从一种颜色淡化到另一种颜色,目前我可以设置LED的颜色,但它不会褪色。这是我的代码到目前为止,我是python的新手,所以任何帮助将不胜感激。提前谢谢。
if (x <= 77):
ledred=100
ledgreen=100
ledblue=100
elif (x > 78) and (x < 80):
for i in range(ledred,-1,-1):
ledred=i
for j in range(ledgreen,-1,-1):
ledgreen=j
for k in range(ledblue,100,1):
ledblue=k
elif (x > 80) and (x < 100):
for i in range(ledred,-1,-1):
ledred=i
for j in range(ledgreen,100,1):
ledgreen=j
for k in range(ledblue,-1,-1):
ledblue=k
elif (x > 100) and (x < 110):
cycle=100
for i in range(ledred,100,1):
ledred=i
for j in range(ledgreen,100,1):
ledgreen=j
for k in range(ledblue,-1,-1):
ledblue=k
else (x > 110):
for i in range(ledred,100,1):
ledred=i
for j in range(ledgreen,-1,-1):
ledgreen=j
for k in range(ledblue,-1,-1):
ledblue=k
red.ChangeDutyCycle(ledred)
green.ChangeDutyCycle(ledgreen)
blue.ChangeDutyCycle(ledblue)
答案 0 :(得分:0)
好吧,在尝试几种不同的方法重写它之后,下面的代码几乎完全符合我的要求。它检查温度(x),然后根据温度初始化每个LED颜色通道(分别为红色,绿色,蓝色)的占空比变化。循环的每个循环将占空比向上或向下调整7,并且每种颜色的值都被限制为0和100.通过调整添加到w,y和z的数量,可以使转换更具戏剧性或更不显着。 。我添加了一个对象(fan1),刚刚完成蚀刻我的第一块电路板。我现在有一个功能齐全的raspberry pi 7风扇控制器,带有LED温度指示。这将添加到我的自定义案例构建中。我很感激所提供的帮助。谢谢,这是最终的工作代码:
#!/usr/bin/env python2.7
# script by Eric Johnsten utilizing info from:
#http://RasPi.tv/2013/how-to-use-soft-pwm-in-rpi-gpio-pt-2-led-dimming-and-motor-speed-control
#https://github.com/SimplyAutomationized/raspberrypi/tree/master/pidfanpi
import RPi.GPIO as GPIO # always needed with RPi.GPIO
import testtemp
import os
from time import sleep # pull in the sleep function from time module
GPIO.setmode(GPIO.BCM) #choose BCM or BOARD numbering schemes.
GPIO.setup(17, GPIO.OUT) #set GPIO 17 as output for fan1
#GPIO.setup(18, GPIO.OUT) #set GPIO 18 as output for fan2
GPIO.setup(26, GPIO.OUT) #Red Channel
GPIO.setup(19, GPIO.OUT) #Green Channel
GPIO.setup(13, GPIO.OUT) #Blue Channel
fan1 = GPIO.PWM(17, 100) #object fan1 for PWM on port 17 at 100 Hertz
#fan2 = GPIO.PWM(18, 100) #object fan2 for PWM on port 18 at 100 Hertz
blue = GPIO.PWM(26, 100) #object for red channel 100 Hertz
red = GPIO.PWM(19, 100) #object for green channel 100 Hertz
green = GPIO.PWM(13, 100) #object for blue channel 100 Hertz
fan1.start(0)
#fan2.start(0)
red.start(50)
green.start(50)
blue.start(50)
w=100
y=100
z=100
f=0
try:
while True:
#sleep(.02)
os.system('clear')
#get temperature from sensor and determine pwm fan speed
x = testtemp.read_temp()
print x
if (x <= 77):
w=w+7
y=y+7
z=z+7
if (w > 100):
w=100
if (y > 100):
y=100
if (z > 100):
z=100
if (w < 0):
w=0
if (y < 0):
y=0
if (z < 0):
z=0
red.ChangeDutyCycle(w)
green.ChangeDutyCycle(y)
blue.ChangeDutyCycle(z)
f=0
elif (x > 78) and (x < 80):
w=w-7
y=y-7
z=z+7
f=60
if (w > 100):
w=100
if (y > 100):
y=100
if (z > 100):
z=100
if (w < 0):
w=0
if (y < 0):
y=0
if (z < 0):
z=0
red.ChangeDutyCycle(w)
green.ChangeDutyCycle(y)
blue.ChangeDutyCycle(z)
fan1.ChangeDutyCycle(60)
elif (x > 80) and (x < 100):
w=w-7
y=y+7
z=z-7
f=70
if (w > 100):
w=100
if (y > 100):
y=100
if (z > 100):
z=100
if (w < 0):
w=0
if (y < 0):
y=0
if (z < 0):
z=0
red.ChangeDutyCycle(w)
green.ChangeDutyCycle(y)
blue.ChangeDutyCycle(z)
fan1.ChangeDutyCycle(70)
elif (x > 100) and (x < 110):
w=w+7
y=y+7
z=z-7
f=85
if (w > 100):
w=100
if (y > 100):
y=100
if (z > 100):
z=100
if (w < 0):
w=0
if (y < 0):
y=0
if (z < 0):
z=0
red.ChangeDutyCycle(w)
green.ChangeDutyCycle(y)
blue.ChangeDutyCycle(z)
fan1.ChangeDutyCycle(85)
elif (x > 110):
w=w+7
y=y-7
z=z-7
f=100
if (w > 100):
w=100
if (y > 100):
y=100
if (z > 100):
z=100
if (w < 0):
w=0
if (y < 0):
y=0
if (z < 0):
z=0
red.ChangeDutyCycle(w)
green.ChangeDutyCycle(y)
blue.ChangeDutyCycle(z)
fan1.ChangeDutyCycle(100)
print 'Setpoint: 77.0F \nTemp: '+str(testtemp.read_temp())+' \nFan Speed: ',str(f)+'%'
print w
print y
print z
sleep(0.02)
except KeyboardInterrupt:
fan1.stop() # stop Fan 1 PWM output
#fan2.stop() # stop Fan 2 PWM output
red.stop() # stop red leds
green.stop() # stop green leds
blue.stop() # stop blue leds
GPIO.cleanup() # clean up GPIO on CTRL+C exit
pass