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:
Code:
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