PWM Fading 3 LEDs at One Time on 08M

billmoke

New Member
PWM Fading 3 LEDs at One Time on 08M

Attached is some coding that will PWM the 08M on pins 1,2, and 4. You should be able to obtain a rainbow of colors using red, green, and blue LEDs. The pwm command is used to provide bursts of PWM output to generate a pseudo analogue output, per Picaxe Manual 2.

There was a posting in August 2005, found in Archive 12, under the thread _pwm out on 08M_. At the time, Rick Harris responded with some code that he found and manipulated to produce a pwm output on the 08M. It seems to me the code was limited to fading any combinations of 1 or 2 LED pins, at any one time.

There is another post from September 2007, by Michael 2727, using a similar approach that fades the three pins 1,2, and 4 individually. That approach is under Code Snippet at http://www.picaxeforum.co.uk/showpost.php?p=50814&postcount=1.

Anyway, I started in the direction that Rick pointed and wrote some code that works all three 08M pins in all combinations simultaneously, as necessary. I have annotated the code so you can follow it.

Code:
'This code is based on a post, referenced to follow, that was in 
'PicAxe Archive 12, under the thread pwm out on 08M.
'That circuit faded, full off to full on, all the combinations 
'of either 1 LED or 2 LEDs at a time.  

'The code in my updated, modified version below includes a
'third LED into the mix.  The full spectrum of colors should 
'now be available for viewing, using the 08M.
  
'The post that got me to thinking about how to add a third
'LED is here at:
'http://www.picaxeforum.co.uk/showpost.php?p=19496&postcount=10
'
'The same caveat applies here, as was given in the August 2005
'post, I do not know where the very original source code code 
'came from, except as it was found here on the PicAxe forum.
 

' PWM Fading 3 LEDs at One Time
' For PICAXE-08M
' LEDs on pins 1,2,4


symbol cycle = b0
symbol loop1 = b1
symbol loop2 = b2
symbol hold =  b8
symbol condx = b9

' ***** main loop *****
hold=255
cycle=255
loop2=0
start:
low 1
low 2
low 4
' ***** end of main loop *****

' ***** LED control routine *****

nap 4
start3:
'at start3 pins 1,2,4 are LOW

condx=1 
'during C1A, pin 4 is incrementally pulsed to a high state
'then,  during C1, two things happen at one time.
'pin 4 is incrementally pulsed to a low state, and 
'pin 2 is incrementally pulsed to a high state.
'visually things are smooth.   
gosub fade ' 

condx=2
'at the immediate start of condx2, pin 2 has already 
'been pulsed high, but will immediately turn off 
'after leaving the 
'loop in C1. So, the first thing that needs to be done,
'to maintain pin 2 high is either set a HIGH 2 
'command, or set PWM to PWM 2,HOLD,1, where HOLD is a fixed
'high value (255) variable. Then, pin 4, pulsed high again.
'After completion of program point C2A, pins 2 and 4 are 
'now sitting at a HIGH state.
'Then comes C2, where both pins 2 and 4 are pulsed OFF,
'and pin 1 is pulsed HIGH, all 3 pins simultaneously.
'condx3 and condx4 work similarly. More comments trail this
'code.  
gosub fade

condx=3
gosub fade

condx=4
gosub fade

rest:
pause 500
restart:
goto start3

' *****PWM drive routine*****
fade:

for loop1 = 0 to cycle

if condx=1 then c1a
if condx=2 then c2a
if condx=3 then c3a
if condx=4 then c4a

C1A:
pwm 4,loop1,1
goto subloop1


C2A:
pwm 2,hold,1 ' or, use HIGH 2
pwm 4,loop1,1

goto subloop1

C3A:
pwm 1,hold,1
pwm 4,loop1,1

goto subloop1


C4A:
pwm 1,hold,1
pwm 2,hold,1
pwm 4,loop1,1

goto subloop1

subloop1:
next loop1


for loop1=cycle to 0 step -1 
loop2=loop2+1

if condx=1 then c1
if condx=2 then c2
if condx=3 then c3
if condx=4 then c4

C1: 


pwm 4,loop1,1
pwm 2,loop2,1

goto subloop2

end
C2:

pwm 2,loop1,1
pwm 4,loop1,1
pwm 1,loop2,1

goto subloop2

C3:

pwm 1,hold,1 'or use HIGH 1
pwm 4,loop1,1
pwm 2,loop2,1

goto subloop2

C4:

pwm 1,loop1,1
pwm 2,loop1,1
pwm 4,loop1,1

subloop2:
next loop1
loop2 = 0
return


'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
'There are three LEDS that are powered. Pin 0 was not used.
'Pin 0 did not respond linearly to the PWM command, but
'acted in either a full ON or a full OFF state.
'There are eight possible full on to full off combinations
'when using three LEDs. See below:
'Red Green Blue
'0	0	0
'0	0	1
'0	1	0	
'0	1	1
'1	0	0
'1	0	1
'1	1	0
'1	1	1
'The sequence of condx1, condx2, condx3, and condx4
'follow this sequence.  This is 'a brute force method 
'of cycling colors, and they all criss-cross each other.
'
'I only have the circuit prototyped on the board and
'it works. Some of the lights turn on and off just a 
'little bit quickly or slowly relative to each other,
'but it seems like they could behave perfectly if I
'matched up the current limit resistors to suit
'each color or LED type.

'Bill Mokoid
'February 4, 2008
With three LEDs, you can have a possible eight combinations of ON and OFF, including all three OFF and all three ON. The code supports the combinations of all three ON or OFF. The code also cycles all the LEDs through the phases were all three are partially ON, or partially OFF.

I added a chart to the code showing the flow of the LEDs turning on and off. There are a total of 8 states of full on and full off, implemented with basically 8 subroutines. You could insert more subroutines to vary or extend the fader sequence. Note how some pins need to be 'pinned' high to maintain them on as the fading sequence rises and falls.

It is interesting that software can be used to pulse three pins with the PWM command. There is not a lot to do about setting timings or adjusting speeds too much. You can play with the third parameter in the PWM command, _PWM pin,duty,cycles_ which is a 1 in the attached code. If you make the _cycle_ number larger, things start to slow down and flicker. As is, there is no flicker on my prototype board. I made no effort to try and condense or optimize the code. Its been many years since I did any kind of programming.

Bill M.
 
Top