Fade out LEDs question


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2004
    Posts
    92

    Default Fade out LEDs question

    I have a circuit working that basically flashes LED's in different sequences and then turns all LED's off upon button presses. It works good but what I'd like to do is add a sequence that instead of the LED's simply turning off, they fade out so as to look sort of like a "comet tail".

    I'm using a PIC 12F675 and the code is below. If I could just be pointed in the right direction to know where to start I may be able to figure it out.

    Thanks for any "pointing"

    Code:
    ANSEL=0
    CMCON=7
    
    
    Input GPIO.5
    Low GPIO.0
    Low GPIO.1
    Low GPIO.2
    Low GPIO.4
    
     
    
    loop:
     
    IF GPIO.5 = 0 Then
    Pause 500 
    GoSub pressI:
    EndIF
    
    GoTo loop
    
     
    
     
    
    
    pressI:
    
    High GPIO.0
    Pause 50 
    Low GPIO.0
    Pause 50
    High GPIO.0
    Pause 50 
    Low GPIO.0
    Pause 50
    High GPIO.1
    Pause 50 
    Low GPIO.1
    Pause 50
    High GPIO.1
    Pause 50 
    Low GPIO.1
    Pause 50
    High GPIO.2
    Pause 50 
    Low GPIO.2
    Pause 50
    High GPIO.2
    Pause 50 
    Low GPIO.2
    Pause 50
    
    IF GPIO.5 = 0 Then
    Pause 500 
    GoSub pressII:
    EndIF
    
    GoTo pressI
    
    
    pressII:
    
    High GPIO.0
    Pause 75 
    Low GPIO.0
    Pause 75
    High GPIO.1
    Pause 75 
    Low GPIO.1
    Pause 75
    High GPIO.2
    Pause 75 
    Low GPIO.2
    Pause 95
    High GPIO.2
    Pause 75 
    Low GPIO.2
    Pause 75
    High GPIO.1
    Pause 75 
    Low GPIO.1
    Pause 75
    High GPIO.0
    Pause 75 
    Low GPIO.0
    Pause 95
    
    IF GPIO.5 = 0 Then 
    Pause 500
    GoSub pressIII:
    EndIF
    
    GoTo pressII
    
    
    pressIII:
    
    High GPIO.2
    Pause 75 
    Low GPIO.2
    Pause 75
    High GPIO.1
    Pause 75 
    Low GPIO.1
    Pause 75
    High GPIO.0
    Pause 75 
    Low GPIO.0
    Pause 95
    
     
    
    IF GPIO.5 = 0 Then 
    Pause 500
    GoSub pressIIII:
    EndIF
    
    GoTo pressIII
    
     
    
    pressIIII:
    
    High GPIO.4
    Pause 5 
    Low GPIO.4
    Pause 5
    
    IF GPIO.5 = 0 Then 
    Pause 500
    GoSub strobe:
    EndIF
    
    GoTo pressIIII
    
    
    strobe:
    
    High GPIO.4
    Pause 25 
    Low GPIO.4
    Pause 25
    High GPIO.4
    Pause 25 
    Low GPIO.4
    Pause 25
    High GPIO.4
    Pause 25 
    Low GPIO.4
    Pause 25
    High GPIO.4
    Pause 25 
    Low GPIO.4
    Pause 1000
    
    
    IF GPIO.5 = 0 Then 
    Pause 500
    GoSub loop:
    EndIF
    
    GoTo strobe

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    The only way i know of to fade a LED is by using PWM, so you would need to apply a decaying pulse width to each pin, but steping to the next pin before the previous pin had reached zero, it would give you a night rider effect if you bounced it up and down the pins

  3. #3
    Join Date
    Dec 2005
    Location
    So Cal and loving it
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Caps?

    If you dont mind extra components, a small capacitor before your limiting resistors would give the affect your looking for.

    Paul
    +-------------------------------------------------------------------+
    | PBP 2.47/2.50 | MCS+ 3.0.0.5 | U2 Prog | Vista x64 | NO SLEEP!!!!!!! |
    +-------------------------------------------------------------------+

  4. #4
    Join Date
    Sep 2007
    Posts
    50


    Did you find this post helpful? Yes | No

    Default

    I use Darrel Taylor's DT_INTS-14 (SPWM_INT - Multiple Software PWM) to achive this very well.


    http://darreltaylor.com/DT_INTS-14/SPWM.html
    Best Regards,

    Kurt A. Kroh
    KrohTech

    “Goodbye and thanks for all the fish”

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Do a search for something to the effect of slow speed pwm.
    What you want is Pulse Width Modulation. You turn an LED on and off, faster than your eyes can detect that your turning it on and off really fast (above about 25 times per second in general), but you vary the time it's actually on during that on and off period. Do a search on wiki for 'persistence of vision' and you should get the idea.

    Code:
    'your general setup code like you had before
    
    brightness var byte 'can vary from 0 (off) to 255 (full brightness)
    pwmcount var word
    led var gpio.0
    output led
    low led
    button var gpio.5
    input button ' i assume your button is on gpio.5
    
    main:
    pwmcount = pwmcount + 1
    if brightness < pwmcount then
    high led
    else
    low led
    endif
    if pwmcount = 0 then 'check state of button whenever pwmcount rolls over to zero
         if button = 0 then 'if button pressed (not pressed?), decrease brightness
              if brightness > 0 then 'only if brightness isn't already zero
                   brightness = brightness - 1 'decrease it
              endif
         endif
    else 'if the button isn't pressed (or is pressed? depending on how you have it wired),
         if brightness < 255 then 'increase the brightness if it isn't already at maximum
              brightness = brightness + 1 'increase it
         endif
    endif
    goto main
    This will only run one LED, with one button. Push it, it gets brighter, let go, it gets dimmer.
    Not sure how well it'll run, if it'll flicker or not, or even if it'll be agonizingly slow.
    See what happens...

  6. #6
    Join Date
    Dec 2005
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    Hello. That is funny, I just finished my project on a 12F675 with RGB LEDs and the fade in fade out thing for my PSP. I also finally got an interrupt to work to switch modes/colors at any time, so I am extremely happy and have this site to thank! Here is a poor quality video link and let me know if it is something close to what you are trying to do...



    I am pressing a button to get the different colors then the fade effect goes through all the colors as the final mode then there is an all off, so like 9 presses gets from red to fade then off. I am a bit embarrassed to post the code since it is pretty ugly to just blink some leds. I can e-mail it to you...

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by gl73 View Post
    I am pressing a button to get the different colors then the fade effect goes through all the colors as the final mode then there is an all off, so like 9 presses gets from red to fade then off. I am a bit embarrassed to post the code since it is pretty ugly to just blink some leds. I can e-mail it to you...
    http://web.ndak.net/jdgrotte/kromatoobz/kromatoobz.html
    Wish I had a movie to go with it...

  8. #8
    Join Date
    Dec 2005
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    That is great! Is this a product of yours? The menu/controller interface with more than one button easily impresses me. I am sure I'll be able to get more buttons for menus to work as I progress, hehehe. After getting the "On Interrupt" to finally work, blinking LEDs have become quite fun! Now to figure out how to have a hold button in the interrupt handler do what I want, so far no beans

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by gl73 View Post
    After getting the "On Interrupt" to finally work, blinking LEDs have become quite fun! Now to figure out how to have a hold button in the interrupt handler do what I want, so far no beans
    I think the key you might be looking for is...keep the interrupt handler as short as possible. Set a flag, save a number, do something, just don't do much. Do the main thing (i.e. button press handling, mode changing, etc) in your main loop. If you're doing the fading using PWM and interrupts, then the only thing the interrupt should do is run the PWM and set the LEDs, nothing else. Check for a keypress in your main loop, and don't use any pauses to keep from getting 'double hits' on the buttons. When you detect a keypress, set a byte value and have it count down with the PWM interrupt and don't accept any keypresses until that counter is back at zero.

Similar Threads

  1. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  2. Max7219 question
    By jamied in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th December 2008, 16:33
  3. Use of a PIC
    By Edwardo in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 22nd March 2007, 14:11
  4. Using LEDs as light sensors
    By skimask in forum Code Examples
    Replies: 3
    Last Post: - 30th December 2006, 22:19
  5. controlling leds with the switches
    By ilteris in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th October 2005, 21:02

Members who have read this thread : 0

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