May 20, 2024, 04:03:02 PM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


PWM for LED fade

Started by davisgraveyard, May 08, 2008, 05:15:06 PM

Previous topic - Next topic

davisgraveyard

I am creating a LED eye fading effect.   I will have 2 pairs of 12v LED eyes connected to each of the 8 outputs on a Prop-1  for a total of 32 LED's.  Each pair will be located in a different location.   Using RANDOM I can make it appear as though different pairs of eyes are fading in and out at different locations. 

I want the eyes to slowly fade up to full brightness and then slowly fade back down.   

The code is listed below.   Here is my question

I am doing this to fade the LED

FOR level = 0 to 255
   PWM theLamp, level, 4
NEXT

This gives me the smoothest fade so far.  I had to increase the duration and cycle through each duty value in order to make it slow enough.   I am still seeing a slight jump when fading up the LED which I imagine happens because the LED brightens up even with low voltage applied?    Just wondering if there is a better way of doing than what I am doing.   

Jeff




' {$STAMP BS1}

' -----[ Variables ]-------------------------------------------------------

SYMBOL  theLamp         = B2                    ' lamp to brighten or dim
SYMBOL  last            = B3                    ' last lamp adjusted
SYMBOL  level           = B4                    ' output level (for PWM)
SYMBOL  lottery         = W5                    ' random value

' -----[ Initialization ]--------------------------------------------------

Reset:
  lottery = 1031                                ' seed random value


' -----[ Program Code ]----------------------------------------------------

Main:
  RANDOM lottery                                ' tumble random value
  theLamp = lottery // 7                        ' select a lamp, 0 - 7
  IF theLamp = last THEN Main                   ' no repeats
    last = theLamp

  FOR level = 0 TO 255
    PWM theLamp, level, 4
  NEXT
  HIGH theLamp                                  ' lamp fully on

  FOR level = 255 TO 0 STEP -1
    PWM theLamp, level, 4
  NEXT
  LOW theLamp                                   ' lamp fully off
  GOTO Main

JonnyMac

I'm running your program on Prop-1 with external LEDs and it looks pretty good.  The small step on the low end probably has to do with the LED's forward voltage.  At very low PWM duty cycles the LED may not light long enough for our eyes to detect, hence it looks like a stepped fall-off.

BTW, you're using a divisor of 7 with modulus which means that only outputs 0 to 6 will be affected; if you actually want all eight then change the divisor to eight, as the modulus operator returns a value between zero and the divisor minus one.
Jon McPhalen
EFX-TEK Hollywood Office

davisgraveyard

Thanks.  I'll stick with what I have.    I think I might put a random delay between each loop so the eyes don't come on right after each other which looks too much like a patern.

My mistake on the modulus 7.   The comment had read 'select a lamp, 0-8.   I changed it to 0-7 but also mistakenly changed the // 7 too?


JonnyMac

On the Prop-1 you have pins 0 through 7, so to light them all you'll want to use:

  theLamp = lottery // 8
Jon McPhalen
EFX-TEK Hollywood Office