PDA

View Full Version : User Selectable LED Brightness



Tissy
- 8th April 2005, 15:23
Can anyone help with what i hope is a simple routine.

I want to select the fade level of an LED. I am achieving this by PWM.

I want it so when a button is pushed and kept down, the LED gets brighter, using PWM this is achieved by 0 - 255.

When it reach 255 the LED flashes. When you release the button it stays at this level.

I am almost there on this part, but when you release the button the LED goes back to low brightness.

Heres what i have come up with so far:


TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
CMCON = 7

SecSwitch VAR PORTA.5
Red VAR PORTB.1

Bright VAR Byte
storedloop VAR Byte

bright = 1

main:
pwm red, bright, 5

If secswitch = 0 then bright = bright + 1
If bright = 255 then gosub flash

Goto main

Flash:
For storedloop = 1 to 3
High red
Pause 100
Low red
Pause 100
Next storedloop
Return



I want it so that if you push and hold the button again, it decreases the brightness.

If during the increase / decrease stage, if you release the button but then push and hold it again, it changes direction.

For example, you hold the button down it gets brighter, you release the button it stays at the level. You push and hold the button again and it decreases in brightness. Release the button, push and hold again it increases.

Can anyone help please. If this can be cracked, it could be handy for a user selectable LCD backlight application. Which this isn't what its for for me.

As i'm using the MCLR as an input for the switch, is there a way to set this fuse in PBP, i'm using MeLabs Serial Programmer. I have to set it manually each time, which is a pain. In PBP i thought it was:

@ __config _MCLRE_OFF

but it doesn't like it.

Many thanks,

Steve

NavMicroSystems
- 8th April 2005, 16:22
Try this

TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
CMCON = 7

SecSwitch VAR PORTA.5
Red VAR PORTB.1

Bright VAR Byte
storedloop VAR Byte

bright = 1

main:
pwm red, bright, 5

If secswitch = 0 then
If bright < 255 then bright = bright + 1
If bright = 255 then gosub flash
endif

Goto main

Flash:
For storedloop = 1 to 3
High red
Pause 100
Low red
Pause 100
Next storedloop
Return



As i'm using the MCLR as an input for the switch, is there a way to set this fuse in PBP, i'm using MeLabs Serial Programmer. I have to set it manually each time, which is a pain.
It depends on your PIC type and the assembler you are using (PM, or MPASM)

see: Presetting Configuration Fuses (http://www.picbasic.co.uk/forum/showthread.php?t=543&highlight=setting+configuration)

Tissy
- 8th April 2005, 16:30
Thanks for that Ralph, i see where i went wrong on the increase.

Having reached maximum brightness (255), how would i now make it decrease brightness back to 0. If i make it bright = bright - 1 then it only goes 1 step down, then back up again.

I want it to perform like a conventional houshold light touch dimmer.

Can you help, thanks.

Steve

NavMicroSystems
- 8th April 2005, 16:42
Steve,

There are many possible ways.

Before I post a ready to run solution, I'll give you a hint that should help you to get started and let you do some homework.

You could assign an extra variable (Flag VAR BIT) to memorize what the last action performed was: "increase" or "desrease" brightness.

Dependend on FLAG you enter the decrease or increase loop when the button is pressed.

Have you checked Presetting Configuration Fuses (http://www.picbasic.co.uk/forum/showthread.php?t=543&highlight=setting+configuration)

Let us know how you are getting on.

Tissy
- 8th April 2005, 17:30
Thank for the 'Hint', this is what i have come up with so far. I think i am along the right lines, but when you hit the button, it continously dims to OFF and there it stays. Am i on the right lines, before i go off on a tangent ?? Can you suggest anymore hints??

Heres what i have:



TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
CMCON = 7

SecSwitch VAR PORTA.5
Red VAR PORTB.1

Bright VAR Byte
storedloop VAR Byte

flag VAR Byte

bright = 254
flag = 0
Low red

Main:
If secswitch = 0 then gosub pwmroutine
Goto Main


PWMRoutine:
While (Bright !=255) and (flag = 0)
PWM red,Bright,5
bright = bright - 1
Wend
While (bright !=0) and (flag = 1)
PWM red,Bright,5
bright=bright + 1
Wend
Return

Flash:
For storedloop = 1 to 3
High red
Pause 100
Low red
Pause 100
Next storedloop
Return


Cheers,

Steve

NavMicroSystems
- 8th April 2005, 19:30
Steve,

there is a number of things wrong now.
in your main loop there will be no PWM output until you press the button.

Once you have pressed the button it will execute the "decrease brighness" loop since flag=0
(flag is always 0 in your case!!!)
Since you don't check for "key still pressed?" in the While...Wend Loop
it counts bright from 254 (preset value) down to zero.
with the next decrement (down from zero) bright will be 255.

From there on none of the conditions to output PWM
is true or will ever become true. so nothing happens

NavMicroSystems
- 10th April 2005, 18:24
Steve,

regarding your PM,

I think it has all been discussed alredy there (http://www.picbasic.co.uk/forum/showthread.php?t=1258)