November 21, 2024, 12:01:18 PM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


LED Control & Fading

Started by Specter, March 21, 2009, 02:43:54 PM

Previous topic - Next topic

Specter

Hey guys-
I'm new here, and very new to this Prop-1 stuff. I need some code that will do a couple thing...

1) Take 5 different LEDs, and individually turn them on, then fade off. There is no fade on- I just want an abrupt turn on, then fade out. The 5 LEDs should turn on randomly in sequence, but should stay on for the same amount of time. Lastly- they CAN overlap, and I occasionally want them to. This means that when one turns on, it can be in process of fading out when another one turns on. Again, that should be somewhat random.

2) Take another LED (#6), fade it in, stay lit for ~15-20sec, then fade out. It should stay off for about another ~20sec or so, then repeat. This is NOT random.

I have NO IDEA where to start with on this code. I have 2 other props that will use similar code, with a few differences. Once this is put together, I should be able to adjust this code for the other props.

Is there someone that can help me put this together?
Thanks!

JonnyMac

You can only do one fade at a time with the Prop-1, and when you start a fade (up or down) you have to finish it before doing anything else.  If that's okay I'll whip up a demo.
Jon McPhalen
EFX-TEK Hollywood Office

Specter

huh. Bummer. Ok then- if that's the case... I'll take it- but then I'd like for it to be a random time in between each of the 5 LEDs (so, from the time one fades off, to when the next turns on). This can be 0-3 seconds...

I'd LOVE if you could do that for me! I'd sincerely appreciate it!

JonnyMac

Give this a try -- you can run it on a Prop-1 Trainer.

' =========================================================================
'
'   File...... Random_Faders.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 21 MAR 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Trigger         = PIN6                  ' SETUP = DN

SYMBOL  MstrPin         = PIN5
SYMBOL  Master          = 5

SYMBOL  Ch5             = 4
SYMBOL  Ch4             = 3
SYMBOL  Ch3             = 2
SYMBOL  Ch2             = 1
SYMBOL  Ch1             = 0


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  chPin           = B2                    ' channel pin, 0 to 4
SYMBOL  chLast          = B3
SYMBOL  level           = B4

SYMBOL  chDelay         = W3
SYMBOL  timer           = W4
SYMBOL  lottery         = W5


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00111111                              ' set outputs

  lottery = 1031


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

Main:
  RANDOM lottery
  IF MstrPin = 1 THEN Check_Master_Off


Check_Master_On:
  IF timer < 20000 THEN Pop_Fade

Master_On:
  FOR level = 0 TO 255
    PWM Master, level, 2
  NEXT
  HIGH Master                                   ' hold on
  timer = 0                                     ' reset master timer
  GOTO Pop_Fade


Check_Master_Off:
  IF timer < 15000 THEN Pop_Fade

Master_Off:
  FOR level = 255 TO 0 STEP -1
    PWM Master, level, 2
  NEXT
  LOW Master                                   ' hold on
  timer = 0


Pop_Fade:
  RANDOM lottery
  chPin = lottery // 5                          ' select pin, 0 to 4
  IF chPin = chLast THEN Pop_Fade               ' prevent repeat
    chLast = chPin
  HIGH chPin                                    ' pop on
  PAUSE 100
  FOR level = 255 TO 0 STEP -1                  ' fade out
    PWM chPin, level, 2
  NEXT
  LOW chPin
  timer = timer + 1380                          ' delay + fade timing

  RANDOM lottery
  chDelay = lottery // 3000                     ' 0 to 3 secs
  PAUSE chDelay
  timer = timer + chDelay                       ' update master timer

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

davisgraveyard

I used this idea to create 16 pairs of blinking and fading eyes in my bushes with a single Prop1.   Hooked 2 pair to each set of pins and located them in separate places.  The program below randomizes which pin turns on and whether it is a blink or a fade.  Check this one out on the trainer!

' {$STAMP BS1}

' -----[ I/O Definitions ]-------------------------------------------------


' -----[ Constants ]-------------------------------------------------------


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

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


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

Reset:
  lottery = 1031                                ' seed random value


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

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

  RANDOM lottery
  blink = lottery & %00000001
  IF blink  = 1 THEN  blink_eyes
  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
  PAUSE 2000
  GOTO Main

  blink_eyes:
  HIGH theLamp
  PAUSE 500                                  ' lamp fully on
  LOW theLamp                                   ' lamp fully off
  PAUSE 500                                  ' lamp fully on
  HIGH theLamp
  PAUSE 500                                  ' lamp fully on
  LOW theLamp                                   ' lamp fully off
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -----[ EEPROM Data ]-----------------------------------------------------

Specter

JonnyMac-
That works great!  I have a couple questions tho...

How do I control the rate of fade-off?

Also- you said that you can only have 1 LED on at a time. HOWEVER- on the trainer- the P5 comes on, and stays on while the others come on/off at random.

If that's the case, isn't it possible to have multiple LEDs turn on at the same time (or, overlap eachother)?

THANKS FOR THE HELP!

JonnyMac

Look up the PWM command in the Help file -- the "cycles" parameter can be used to speed up or slow down the fade rate -- within limits of the loop; you can also use STEP parameter of FOR-NEXT to speed things along.

What I said is that you can only fade one LED at a time -- this is because you have to do it in a loop with PWM.  When LEDs are full on (not fading) you can have as many on as you like.
Jon McPhalen
EFX-TEK Hollywood Office

Specter

aha! OK, cool! Thanks for the help! That makes sense (I think)!

JonnyMac

With a little investigating you can actually work out how long a PWM fade will be; you need to know how many cycles in the PWM command the loop iterations.  For example:

Fade1:
  FOR level = 0 TO 255
    PWM Eyes, level, 1
  NEXT
  HIGH Eyes


This fade will take 1.28 seconds.  How do I know that?  Each cycle in PWM takes 5ms, the loop has 256 levels (you have to count 0) -- 5ms x 256 = 1280ms. 

If we want to go twice as fast we can do this:

Fade2:
  FOR level = 0 TO 255 STEP 2
    PWM Eyes, level, 1
  NEXT
  HIGH Eyes


The STEP 2 modification of FOR-NEXT means we'll only run 128 times.  If we want to go slower we could do this:

Fade3:
  FOR level = 0 TO 255
    PWM Eyes, level, 2
  NEXT
  HIGH Eyes


Now we're running 10ms per step so the fade will be longer.

When using PWM to fade up LED type outputs you must add a HIGH command after the loop.  The PWM instruction was designed for charging RC circuits so it sets the pin to input (Hi-Z) state; using HIGH immediately after a fade up (not down) will hold the output you just faded up.



Jon McPhalen
EFX-TEK Hollywood Office

Clad In Shadows

July 18, 2009, 09:05:05 PM #9 Last Edit: July 18, 2009, 09:10:19 PM by Clad In Shadows
How would you make those LED's turn on and fade in order (0 ---> 7) ?
Just want to turn on LED 1 and fade out , turn on LED 2 and fade out....

JonnyMac

You can do that with nested loops.  In this program the outer loop controls which pin is used, the inner loop takes care of the fading.

' =========================================================================
'
'   File......
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 19 JUL 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'

' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


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

SYMBOL  pin             = B2
SYMBOL  level           = B3


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

Reset:


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

Main:
  FOR pin = 0 TO 7                              ' loop through all pins
    HIGH pin                                    ' turn on
    PAUSE 250                                   ' short delay
    FOR level = 255 TO 0 STEP -1                ' fade out
      PWM pin, level, 2                         ' modulate the pin
    NEXT
  NEXT
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

Clad In Shadows

cool , I need to try that.  Nested loops huh? Learning something everyday. Thanks Jon.

Clad In Shadows

Works perfect. I just changed the steps from -1 to -3 to make it fade a little faster and added a pause at the end.
Thanks again.

Vasago

Hi Jon,

I have a question on a hardware requirement for a following display.

Two child ghosts "yellow" and "green" on a teeter totter swing.

1. When PIR triggered one "Yellow" and one "Green" single LED lights come on to illuminate each side of the swing.

2. The swing begins to move to some ghostly sounds for 30 seconds.

3.  After 30 seconds the misic and the swing stops. 

4.  A second later the "Yellow" LED (that illuminates the yellow side of the swing) fades and a sequence of 8 (eight) consecutive "Yellow" LED's light up and fade in ORDER to simulate ghostly steps away from the swing in one direction.

5  When the last "yellow" light fades, the "Green" LED that illuminates the "green" side of the swing turns off followed by another sequence of eight "green" steps in a different direction.  Once the last "green" step fades, the prop resets and cannot be re-triggered for 2 minutes.

I have prop-1 controllers, but I have a feeling this would require more channels.  Maybe DC-16, or perhaps a prop-2 controller.

I would appreciate some assistance in picking the right hardware, and if possible a program for the new hardware.

Thanks you for your time.

Vasago





 

JonnyMac

Please take time to review our forum guidelines. 

-- http://www.efx-tek.com/php/smf/index.php?topic=266.0

#2 specifically forbids thread hijacking, i.e., changing the topic of a thread mid-stream.  This makes a mess of the forums and we have a zero-tolerance policy.  Start a new thread; this one has been corrupted and is now locked.
Jon McPhalen
EFX-TEK Hollywood Office