yet more RGB tomfoolery

fathom

New Member
I was experimenting with pulsout to vary the brightness of an LED, its been done before but I wanted to
figure it out for myself. Using pulsout if you make your pulse longer (mark) and your off time shorter or
keep it the same (space) the effect will be that the led will be on longer than it is off and will be brighter.
PWM is the same but only the 18X you only have one and to drive an RGB led you need 3.

I used a FOR loop to count to 200 using b0, b0 is then used in Pulsout. As b0 increments the led gets brighter and when b0
reaches 200 the next part of the program decrements b0 which decreases the pulsout make the led dimmer.

do
let b=0
for b0 = 1 to 220
pulsout 0, b0
pause 10
next

for b1 = 1 to 220
pulsout 0, b0
pause 10
dec b0
next b1
pause 100
loop

The cycle lasts for about 5 seconds and the effect is very similar to the Apple charge indicator..

From there I was able to adapt the program to run an RGB led. To do this all I did was take the original program and
exploit the counters. In the second half of the program as b0 is counting down b1 is counting up so by adding a second
pulsout I was able to have one colour brightening up as another is dimming down.

I used 'w' variables as they allowed a larger number to be used as more on time means a brighter led but I got
rid of the short pauses as the w time is much larger and takes time to come to full brightness and an extra pause
made the cycle too long.

Intially the program brings the red led to full brightness, then the main loop starts and dims the red while bringing up
the green, then the green fades as the blue comes up, then the red comes up as the blue fades and then the cycle
repeats.

symbol rled = 0
symbol gled = 1
symbol bled = 2

setfreq m8

let w0 = 0
for w1 = 1 to 1200 'initially brightening red
pulsout rled, w0
next

main:
let w0 = 1200 'makes sure red starts at full brightness
for w1 = 1 to 1200
pulsout rled, w0 'dimming red
pulsout gled, w1 'brightening green
dec w0
next w1

for w0 = 1 to 1200
pulsout gled, w1 'dimming green
pulsout bled, w0 'brightening blue
dec w1
next w0

for w1 = 1 to 1200
pulsout bled, w0 'brightening red
pulsout rled, w1 'dimming blue
dec w0
next w1

goto main

By fiddling with the 1 to 1200 values can allow for the brightness differential in the led colours.

This is a work in progress and a lot more fun is to be had inducing all kinds of visual headaches with the
RGB led tape!
 

1968neil

Senior Member
This may help with your project, i used the pulse out command for the very same reason, not enough pwm outputs.
Its written for an 08M and works very well.
It will control a strip but you may need to upgrade the output Fet's for longer lengths of strip.

Have fun !

Regards
Neil


Code:
'**********************************************************************************
'* PICAXE 08M RGB LAMP  19/09/2010 Neil Scotford                                  *
'* Flame effect using sound command                                               *
'* Mode controlled by a Potentiometer with click positions                        *
'* RGB LED Pirahna common anode                                                   *
'* code uses 252 bytes of the available 256                                       *
'**********************************************************************************
setfreq m8            'faster to reduce flicker

symbol RED_LED=0        'Red LED output physical pin 7
symbol GRN_LED=1        'Green LED output physical pin 6
symbol BLU_LED=2        'Blue LED output physical pin 5
Symbol PL=800        'Pulse length mood--mode
Symbol PL1=400        'Pulse length flame-mode
Symbol Rep=5        'Number of repeats to get reasonably slow fade in mood mode
Symbol Rep1=4           'Number of repeats to get reasonably fast fade in flame mode
b4=5                'set initial step size

Initialise:
pause 300
high RED_LED  'turn off Red
high GRN_LED  'turn off Green
high BLU_LED  'turn off Blue
pause 300

main:
readadc  4,b0                 'read mode Potentiometer 1k ohm pot wiper has 1k in series to ADC4 physical pin 3
if b0< 3 then LampOFF         'if in position 1 do nothing  
if b0< 10 then Red            'Red Led On Green and Blue Off
if b0< 40 then Green          'Green On Red and Blue Off
if b0< 70 then Blue           'Blue On Red and Green Off
if b0< 99 then Yellow         'Red On Green on Blue Off
if b0< 130 then Sky_blue      'Red Off Green and Blue On
if b0< 160 then Purple        'ALL Leds On creates Purple
if b0< 190 then Flame_Red     'RED Flame Effect
if b0< 217 then Flame_Green   'GREEN Flame Effect
if b0< 247 then Flame_Blue    'BLUE Flame Effect
if b0> 254 then mood          'fade thru all colours                    
goto main

LampOFF:
high RED_LED
high GRN_LED
high BLU_LED
goto main

Red:
low RED_LED
high GRN_LED
high BLU_LED
goto main

Green:
high RED_LED
low GRN_LED
high BLU_LED
goto main

Blue:
high RED_LED
high GRN_LED
low BLU_LED
goto main

Yellow:
low RED_LED
low GRN_LED
high BLU_LED
goto main

Sky_blue:
high RED_LED
low GRN_LED
low BLU_LED
goto main

Purple:
low RED_LED
low GRN_LED
low BLU_LED
goto main


flame_Red:
high RED_LED
high GRN_LED
high BLU_LED
let b0=b0+1
sound RED_LED,(b0,255,b0,60,b0,10,b0,240)
goto main

flame_Green:
high RED_LED
high GRN_LED
high BLU_LED
let b0=b0+1
sound GRN_LED,(b0,255,b0,60,b0,10,b0,240)
goto main

flame_Blue:
high RED_LED
high GRN_LED
high BLU_LED
let b0=b0+1
sound BLU_LED,(b0,255,b0,60,b0,10,b0,240)
goto main


MOOD:
high RED_LED
high GRN_LED
high BLU_LED

'slow fade
'blue to red
for w0=1 to PL step b4
w1=PL-w0
for b6=1 to Rep
pulsout RED_LED,w0
pulsout BLU_LED,w1
next b6
next w0

'slow fade       
'red to green
for w0=1 to PL step b4
w1=PL-w0
for b6=1 to Rep
pulsout RED_LED,w1
pulsout GRN_LED,w0
next b6
next w0
b4=b4/2+1        'Convert to number for step size to control speed

'green to blue
for w0=1 to PL step b4
w1=PL-w0
for b6=1 to Rep
pulsout GRN_LED,w1
pulsout BLU_LED,w0
next b6
next w0
goto main
 
Last edited:

1968neil

Senior Member
Neil,,

any chance of getting a schematic of how this is done? I have never worked with rgb leds.

Thanks
RGB-Drver-Pebble-Layout.jpg

Layout using the very useful pebble program, worth downloading as it will save you heaps of time !
If you want to control longer strips you will need to upgrade the 2N7000 fets i used to something with a higher current rating and also need to be logic level versions.

Regards
Neil
 
Last edited:

Randy5140

Member
Neil,

Thanks,, I am just learning this electronics stuff,, I am 61 years old and am always trying to lean MORE!!! :)

I am really having a blast with all the picaxe stuff and really appreciate any and all information I get from you all!

Again,, Thanks
Randy
 
Top