LED Fader

#1
There are many ways you could fade an LED/s up and
down, here are just a couple.
A simple single LED.
Three fading LEDs chasing.

Code:
#picaxe 08m 'Sets the Program Editor config for an 08M Chip.

'This will fade an LED on Pin1 / Leg6, OFF to Full ON to OFF, etc, etc.
  
Main:

Up:
if b5 = 255 then goto Down 
inc b5      'Increments the b5 value each time around. 
'pause 1    'Pause values above 10 will flicker.   
'pause 2
'pause 4
'pause 8 
pwm 1,b5,1  'PWM Pulse on Pin1, value of b5, once. 
goto Up
Down:
if b5 = 0 then goto Up
dec b5      'Decrements the b5 value each time around.
'pause 1    'Pause values above 10 will flicker.
'pause 2
'pause 4
'pause 8
pwm 1,b5,1  'PWM Pulse on Pin1, value of b5, once.
goto Down
Code:
'LED Fade Up then Down
'For PICAXE-08M
'Michael 2727 2005
'LEDs on Pins 0,1,2 on Schools Experimenter PCB ( AXE092 )
'The LEDs will chase 0/Red, 1/Yellow, 2/Green, 1/Yellow 0/Red, etc, etc.

#picaxe 08m 'Sets the Program Editor config for an 08M Chip.
symbol rampup=b0
symbol rampdwn=b1 

main:

'[B]Edit: [/B] low 0     See notes in post below.
low 1
low 2
low 4
'                         '** Set all pins Low **
pause 20
'                         '*********Set which Pins/LED to pulse next here, values stored in b5 **                  
b5=1                        ' Pin 1
gosub Fade                '          The first RETURN comes back to HERE.  
b5=2                        ' Pin 2 
gosub Fade                '          The second RETURN comes back to HERE.
b5=4                       ' Pin 4 
gosub Fade                '         The third RETURN comes back to HERE ect.
b5=2                       ' Pin 2                                                                    
gosub Fade                '                                                                              
'                           '*******************************************************
goto main         ' Back to start      
' ***** PWM ramp LED up /down drive section  *****
Fade:
pause 20
' UPWARD FADE SECTION
for rampup=1 to 255 step 1           '** To speed up the change/fade step value = 1 to 15 **
pwm b5,rampup,1                      '** LED 1 on first, 1 to 255 back to 1 **
pause 3                              '** To slowdown the change/fade, insert pause 1 to 10 here **
next rampup
' DOWNWARD FADE SECTION
for rampdwn=225 to 1 step -1         '** To speed up the change/fade step value to 1 to 15 **
pwm b5,rampdwn,1
pause 3                              '** To slowdown the change/fade insert pause 1 to 10 here **
next rampdwn
low 0 
pause 300                            '** Slight delay between the next LED used in the sequence **
return
                   '** By connecting the outputs to a MOSFET transistor
                   '** you should be able to make a string of DC Low
                   '** Voltage Christmas lights fade up and down.
Good Luck !
 
Last edited:
#3
With the #1 Code open in the Program Editor -
Look at the Top Tool Bar and select View then Options.
In the Options window select Editor.
In the middle of the window in the [ Compiler ] section
select Enhanced then Apply it at the bottom.
(close the window)
Back in the program and it should now work OK.

Copy the line below to the top of the #2 Code.
#picaxe 08m
This Auto Configures the Program Editor to an 08M Chip.
Pin 0 should also work with PWM, although the current
picaxe_manual2.pdf only states pin 1, 2 and 4.
The manual may need correcting.

I have an AXE092 Schools Experimenter Board which
has Output LEDs on pins 0, 1 and 2, this used to work
fine. You can use pin 4 also. Servo should work on all
4 output pins as well. ( pin3 is always an input only and
pin 0 is always an output only )

Good Luck !
 

Pekari

Senior Member
#4
Thanks for help. Now dec & inc works.
But i can't get work pwm in pin 0. It's only ON or OFF even i paste that #picaxe 08m
at the top of #2 Code.
 
#5
Update : As setup by the firmware Pin 0 (08M) can work with PWM
but may not always perform as expected.
Therefore it is not listed as an option in the current picaxe_manual2.pdf

So I have updated the original #2 code above, it now
uses pins 1, 2 and 4, in that order then reversed 4, 2, 1 etc.

Good Luck !
Again :)

PS: The above code is only for 08M chips.
As I don't have any 18, 28 or 40 chips I can't verify a
working/tested code for those chip types.
But if someone has any of them and would like to post a
suitable code using PWMOUT or PULSOUT or some other
clever method, go for it !

K-I-S-S and include comments if the code may not be
apparent to beginners :)
 
Last edited:

Pekari

Senior Member
#6
Some samples

These works in PICAXE 18X.
Pwmout-test:

Code:
'	Led fader to PICAXE 18X by Pekari
main:
for b1=0 to 255
pwmout 3,255,b1
next
for b1=255 to 0 step -1
pwmout 3,255,b1
next
goto main
Pulsout test:
Code:
'	Running LED light row to PICAXE 18X by Pekari
'	8 LED to out-pin 0-7.
main:
for b1=0 to 7
b2=b1*36
pulsout 0,b2
b2=b1+1 b2=b2 and 7 b2=b2*36
pulsout 1,b2
b2=b1+2 b2=b2 and 7 b2=b2*36
pulsout 2,b2
b2=b1+3 b2=b2 and 7 b2=b2*36
pulsout 3,b2
b2=b1+4 b2=b2 and 7 b2=b2*36
pulsout 4,b2
b2=b1+5 b2=b2 and 7 b2=b2*36
pulsout 5,b2
b2=b1+6 b2=b2 and 7 b2=b2*36
pulsout 6,b2
b2=b1+7 b2=b2 and 7 b2=b2*36
pulsout 7,b2
next
goto main
 
#7
LED fading with Servo operation

is it possible to move a servo to a position then fade a LED up/down....move servo again then fade the LED etc....

The manual says that these two comands cant be used together???:confused:
 
#8
I haven't made the circuit, but I seem to understand the code to fade an LED. My question is do you need some filtering components to convert the PWM square waves to an analouge-ish signal??
 
#9
I haven't made the circuit, but I seem to understand the code to fade an LED. My question is do you need some filtering components to convert the PWM square waves to an analouge-ish signal??
Short answer no. this works well just as it is directly driving the LEDs from the picaxe.
 
Top