User Selectable LED Brightness


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154

    Default User Selectable LED Brightness

    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

  2. #2
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Try this
    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 
           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
    Last edited by NavMicroSystems; - 8th April 2005 at 16:27.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  3. #3
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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

    Let us know how you are getting on.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  5. #5
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    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:

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

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  7. #7
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Steve,

    regarding your PM,

    I think it has all been discussed alredy there
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. Free Project - 245 LED Display
    By T.Jackson in forum Code Examples
    Replies: 221
    Last Post: - 16th August 2009, 04:59
  3. new and need help
    By smeghead in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd November 2008, 20:19
  4. LCD will not start
    By btaylor in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 24th May 2007, 02:30
  5. can't even flash an LED
    By bruno333 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 28th April 2005, 13:27

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts