Can I make this LED dimming effect better?

Blazemaguire

Senior Member
Hi guys,

I am still in my infancy of writing in BASIC, - I learnt PICAXE as a necessity for my teaching, but have so far been using PIC Logicator and flow charts.... so apologies if my code is hard to follow, or not in a traditional format, but i'm still learning!

Anyhow, all morning i've been working on this piece of code and have managed to get all the LED's connected to the eight outputs of a 20M brightening and dimming in a repeating cycle using the code below.

The effect looks quite cool, but what I want to know is, is there a way of making the LED's go even dimmer, or even brighter without them visually 'flickering'. In my code you can see the symbol 'Max_Limit'. - If I set this above 16 I get better peak brightness and darkness, but then the LED's 'flicker' presumably because my eye is catching up with the pulsing rate.

Is there anyway around this? (I will experiment with PWM later on) or have I just reached the limits of LED dimming / brightening abilities?

Anyhow, feel free to tear my code apart and suggest any improvements. - It make sense to my head.... I guess the test is if others can interpret it!



symbol max_limit = 16
symbol min_limit = 1
symbol cycle_time= 2

main:

b2=min_limit
b3 = max_limit
b5=0

goto flash_dim


flash_dim:
inc b5
if b2>= max_limit then goto recal_2
let pins = %11111111
pause b3


let pins = %00000000
pause b2
if b5 = cycle_time then goto recal
goto flash_dim



recal:
inc b2
dec b3
b5=0
goto flash_dim


recal_2:
inc b3
dec b2
b5=0

goto flash_bright

flash_bright:
inc b5
if b3>=max_Limit then goto recal
let pins = %11111111
pause b3

let pins = %00000000
pause b2
if b5 = cycle_time then goto recal_2
goto flash_bright


thanks Rob
 

eclectic

Moderator
Just a couplle of very quick changes to play with

At the start, change the frequency to 8MHz

Increase the "Max_limit".

Code:
#picaxe 20m
setfreq m8 ; added *****
symbol max_limit = 42 ;adjust this number


symbol min_limit = 1
symbol cycle_time= 2
e
 

Blazemaguire

Senior Member
Thanks Eclectic.

That has made the transitioning (fading) much smoother, and i 'think' it seems to be brighter.

It was playing with some of the ready made circuits you donated to the school that inspired me to have a go at this! - Do you do your dimming with PWM or with a similar method to my code?

I'm glad I forced myself away from the flowchart programming, as you coudlnt easily do this with Logicator. - Still i think it will be sometime before I get the kids using BASIC based on their ability levels.

Rob
 

eclectic

Moderator
Thanks Eclectic.

That has made the transitioning (fading) much smoother, and i 'think' it seems to be brighter.

It was playing with some of the ready made circuits you donated to the school that inspired me to have a go at this! - Do you do your dimming with PWM or with a similar method to my code?

I'm glad I forced myself away from the flowchart programming, as you coudlnt easily do this with Logicator. - Still i think it will be sometime before I get the kids using BASIC based on their ability levels.

Rob
PWMout for dimming. (If I remember correctly)

e
 

hippy

Ex-Staff (retired)
The techniques for controlling and dimming RGB LED's ( search on RGB* ) can probably be used, for example -

#Picaxe 20M
Loop
For b0 = 1 To 100
b1 = 101 - b0
pins = $FF : Pause b0
pins = $00 : Pause b1
Next
Loop

and ...

#Picaxe 20M
Loop
For b0 = 1 To 100
b1 = 101 - b0
pins = $FF : Pause b0
pins = $00 : Pause b1
Next
For b0 = 100 To 1 Step -1
b1 = 101 - b0
pins = $FF : Pause b0
pins = $00 : Pause b1
Next
Loop
 

Blazemaguire

Senior Member
ok.

Once again thanks!

I'll have to have a play with PWM. - Although am I right in thinking its the same principle, just more automated (and hence less code)?

Rob
 

Andrew Cowan

Senior Member
And it's faster (eg 40kHz).

Although, it isn't used with RGBs much as no chip has three PWM outputs. You can, however, use a master chip and 3 08Ms. (Or A master and 2 08Ms if you don't mind slightly more complex code).

A
 

Blazemaguire

Senior Member
Ohh ok!

So would that make it very easy to have the 'dimming' speed linked to the level of light detected with an LDR? (assuming using PWM?)

thats where i'm going next! - does the fact that the PWM part is running at the same time prevent it from being updated from say a seperate part of the programme reading the data from an LDR? (hope that made sense)

I would like to vary the dimming/brightening speed (so that would be cycle time) in real time in relation to an analogue input. - Either an LDR or a simple Potentiometer.

- Just getting one step ahead of a student who want to make something similar for a GCSE project next year..... think adjustable disco lighting.

Rob
 

Andrew Cowan

Senior Member
Yes - the code is:

PWMout pin,period,duty

The duty is only updated whenever that command is passed, so you can make it update as often or not as you want.

A
 

eclectic

Moderator
@Rob

This is a simple working demo.
08M and a 10k pot. Led/res.

All on an AXE091 board.
But, I think that you've go all of the bits, as separates.

Code:
;varies rate of dimming/brightening
; higher adc value = slower dimming
; adjust STEP values to alter
; See Man 2, p.139 and 142
; and   Man 3, p. 27-29

#picaxe 08M
main:
readadc 1,b0
sertxd (#b0," ")  ; just for system check

for b1 = 5 to 255 step 10 ; getting brighter
pwmout 2, 63,b1
pause b0
next

for b1 =255 to 5 step -10 ; getting dimmer
pwmout 2, 63,b1
pause b0
next

goto main
e
 

lanternfish

Senior Member
And it's faster (eg 40kHz).

Although, it isn't used with RGBs much as no chip has three PWM outputs. You can, however, use a master chip and 3 08Ms. (Or A master and 2 08Ms if you don't mind slightly more complex code).

A
:confused:A 28X1 can in HPWM single mode, or so I believe. Cheers
 

jcgalvezv

New Member
Look at this code I posted in a flashlight forum. The post mentions a driver, but the code works without it. In fact I tested using a regular LED.
 

inglewoodpete

Senior Member
No PWM commands on the 20M

Blaze, I note that you were initially using a 20M and, since then, there has been a lot of posts about PWM on other chips.

The 20M does not support the PWMOut command, so is an unfortunate choice for dimming lights. Have a look at the PWMOut command in the manual: just about every other PICAXE chip can support the hardware PWM function :rolleyes:
 

BeanieBots

Moderator
The 28X1 can have TWO PWMout signals BOTH via PWMout.
Both must run at the same frequency but can have different duties.
 

Blazemaguire

Senior Member
Thanks guys,

Point noted on the 20M and lack of PWM. - I do have some 18x's and 08m's lying around that I will experiment with.

I'll post back if i'm struggling!

Rob
 

BCJKiwi

Senior Member
That's a good point BB. However we still have a 'roadblock' if we want 3 pwms as pwmout on pin2 can't be run at the same time as hpwm so we are still stuck with 2.

hpwm can run 4 outputs at once, all with the same characteristics plus one pwmout.

So depending on what the requirements are, up to 5 simultaneous pwms are possible (28x1). As the OP appears to be running 8 LEDS all at the same settings then perhaps 2 LEDS on each of the hpwm outputs in 'single' mode might do the job?
 

BCJKiwi

Senior Member
For 8 LEDs on one pin additional hardware would probably be required as even with serial/parrallel layouts, insufficient voltage/current is likely.
Simpler to use use as many pins as needed to get the simplest layout - i.e. standard LEDS could be 4 per pin using 2 pins but it all depends on the LEDS.
 
Top