PDA

View Full Version : Fade out LEDs question



Sam
- 15th June 2008, 17:01
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"



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

malc-c
- 15th June 2008, 21:12
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

locko
- 16th June 2008, 07:58
If you dont mind extra components, a small capacitor before your limiting resistors would give the affect your looking for.

Paul

krohtech
- 18th June 2008, 00:00
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

skimask
- 18th June 2008, 04:25
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.



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

gl73
- 22nd June 2008, 10:14
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...

http://www.youtube.com/watch?v=v3B951IvR-k

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

skimask
- 22nd June 2008, 10:16
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 :D
Wish I had a movie to go with it...

gl73
- 22nd June 2008, 10:45
http://web.ndak.net/jdgrotte/kromatoobz/kromatoobz.html :D
Wish I had a movie to go with it...

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

skimask
- 22nd June 2008, 10:50
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.