plasmaninjaa
Member
Hey guys, I ask a lot of questions, but never share anything. So I thought i'de just share this code.
I was looking for code for an rgb led color fading, but couldnt find anything. I even posted a thread about it.
I ended up just writing it myself, so heres what I came up with.
It's nothing special, just code for fading through the color spectrum with an rgb led or led strip.
It slows down a bit when it nears or leaves a common color (roygbiv)
hope this helps someone somewhere.
I was looking for code for an rgb led color fading, but couldnt find anything. I even posted a thread about it.
I ended up just writing it myself, so heres what I came up with.
It's nothing special, just code for fading through the color spectrum with an rgb led or led strip.
It slows down a bit when it nears or leaves a common color (roygbiv)
hope this helps someone somewhere.
Code:
symbol redpin = c.3 'pins selected for 20m2 chip
symbol greenpin = c.5
symbol bluepin = c.2
symbol pauseAmount = 10 'The higher, the slower
pwmout redpin, 100, 255 'initialize pwm
pwmout greenpin, 100, 0
pwmout bluepin, 100, 0
FadingToYellow: 'fades in green to make yellow
pause 200
b1 = 0
do
gosub accelerationCheck 'a subroutine that makes it go slower when closer to a common color
pwmduty greenpin, b1 'pwm output to a pin
pause pauseAmount 'pauses to slow down the color fading
if b1 = 255 then goto FadingToGreen 'goes to next color if fading is completed
loop
FadingToGreen: 'fades out red to make green
pause 200
b1 = 0
do
gosub accelerationCheck
pwmduty redpin, b2
pause pauseAmount
if b1 = 255 then goto FadingToBlue
loop
FadingToBlue: 'fades in blue to make blue
pause 200
b1 = 0
do
gosub accelerationCheck
pwmduty bluepin, b1
pause pauseAmount
if b1 = 255 then goto FadingToIndigo
loop
FadingToIndigo: 'fades out green to make indigo
pause 200
b1 = 0
do
gosub accelerationCheck
pwmduty greenpin, b2
pause pauseAmount
if b1 = 255 then goto FadingToViolet
loop
FadingToViolet: 'fades in red to make violet
pause 200
b1 = 0
do
gosub accelerationCheck
pwmduty redpin, b1
pause pauseAmount
if b1 = 255 then goto FadingToRed
loop
FadingToRed: 'fades out blue to make red
pause 200
b1 = 0
do
gosub accelerationCheck
pwmduty bluepin, b2
pause pauseAmount
if b1 = 255 then goto FadingToYellow
loop
AccelerationCheck:
if b1<=14 then 'checks to see if the fade is close to a common color (roygbiv)
b1 = b1 + 1
elseif b1<60 then
b1 = b1 + 3
elseif b1> 194 then
b1 = b1 + 3
elseif b1 > 239 then
b1 = b1 + 1
else
b1 = b1+5
endif
b2 = 255-b1 'makes b2 the opposite for fading out colors
return