May 15, 2024, 01:49:22 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.


Firely program

Started by imagineerdan, February 03, 2011, 11:34:08 AM

Previous topic - Next topic

imagineerdan

I have the prop-1 firefly program and it works great. I was wondering what one would look like for the prop-2?

JonnyMac

Here's my take.  It's adjusted for 16 outputs and the PWM call is adjusted for the 5x speed increase of the Prop-2 versus the Prop-1.

' =========================================================================
'
'   File...... Fireflies.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail....
'   Started...
'   Updated... 03 FEB 2011
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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


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

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

Yes             CON     1
No              CON     0


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

lottery         VAR     Word
oldFlies        VAR     Word
mask            VAR     Word
timer           VAR     Word

idx             VAR     Byte
flyNum          VAR     Byte
lastFly         VAR     Byte


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

Reset:
  oldFlies = $0000                              ' clear


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

Main:
  DO
    FOR idx = 1 TO 3                            ' tumble random generator
      RANDOM lottery
    NEXT
    flyNum = lottery // 16                      ' select "fly" (0 to 15)
  LOOP UNTIL (flyNum <> lastFly)                ' until new selection

  lastFly = flyNum                              ' save current fly
  mask = 1 << flyNum                            ' create pin mask
  IF ((mask & oldFlies) > 0) THEN Main          ' check play list

  GOSUB Fade_On                                 ' activate fly
  timer = lottery // 251 + 250                  ' 250 - 500 ms delay
  PAUSE timer
  GOSUB Fade_Off                                ' deactivate

  timer = lottery / 32                          ' delay between flies
  PAUSE timer

  oldFlies = oldFlies | mask                    ' mark this fly
  IF (oldFlies = $FFFF) THEN
    GOTO Reset
  ELSE
    GOTO Main
  ENDIF


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

Fade_On:
  timer = lottery / 16 // 5 + 1                 ' randomize brighten time
  FOR idx = 0 TO 255 STEP 3                     ' sweep from off to on
    PWM flyNum, idx, timer                      ' brighten the output
  NEXT
  HIGH flyNum                                   ' leave the output on
  RETURN


Fade_Off:
  timer = lottery / 32 // 5 + 1                 ' randomize fade time
  FOR idx = 255 TO 0 STEP -3
    PWM flyNum, idx, timer                      ' dim the output
  NEXT
  LOW flyNum                                    ' leave the output off
  RETURN


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


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

imagineerdan

I will give this a try thanks Jon!

imagineerdan

If I just want to use outputs 0-5 (so the first 6 outputs) Do I just change the

"flyNum = lottery // 16                      ' select "fly" (0 to 15)"

to : flyNum = lottery // 5

JonnyMac

No, you need to use 6 as the divisor.  The modulus (//) operator returns 0 to n-1 (where n is the divisor).

We posted a bit on math tricks, including using modulus, here:
-- http://www.efx-tek.com/php/smf/index.php?topic=49.0
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

I am not the biggest math person.. What would it look like in a code then for this with 0-5 being our active outputs? Thanks soo much

JonnyMac

It's not a math thing so much as a logic thing -- I'll bet if you'd gone with your gut you'd have been right.

There are two lines that change (marked in red).

' =========================================================================
'
'   File...... Fireflies.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail....
'   Started...
'   Updated... 28 MAR 2011
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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


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

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

Yes             CON     1
No              CON     0


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

lottery         VAR     Word
oldFlies        VAR     Word
mask            VAR     Word
timer           VAR     Word

idx             VAR     Byte
flyNum          VAR     Byte
lastFly         VAR     Byte


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

Reset:
  oldFlies = $0000                              ' clear


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

Main:
  DO
    FOR idx = 1 TO 3                            ' tumble random generator
      RANDOM lottery
    NEXT
    flyNum = lottery // 6                       ' select "fly" (0 to 5)
  LOOP UNTIL (flyNum <> lastFly)                ' until new selection

  lastFly = flyNum                              ' save current fly
  mask = 1 << flyNum                            ' create pin mask
  IF ((mask & oldFlies) > 0) THEN Main          ' check play list

  GOSUB Fade_On                                 ' activate fly
  timer = lottery // 251 + 250                  ' 250 - 500 ms delay
  PAUSE timer
  GOSUB Fade_Off                                ' deactivate

  timer = lottery / 32                          ' delay between flies
  PAUSE timer

  oldFlies = oldFlies | mask                    ' mark this fly
  IF (oldFlies = %00111111) THEN
    GOTO Reset
  ELSE
    GOTO Main
  ENDIF


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

Fade_On:
  timer = lottery / 16 // 5 + 1                 ' randomize brighten time
  FOR idx = 0 TO 255 STEP 3                     ' sweep from off to on
    PWM flyNum, idx, timer                      ' brighten the output
  NEXT
  HIGH flyNum                                   ' leave the output on
  RETURN


Fade_Off:
  timer = lottery / 32 // 5 + 1                 ' randomize fade time
  FOR idx = 255 TO 0 STEP -3
    PWM flyNum, idx, timer                      ' dim the output
  NEXT
  LOW flyNum                                    ' leave the output off
  RETURN


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


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

imagineerdan

Jon Excellent, this worked very well and I really value your "push" which really helps explain these programing methods with real examples. It has been a wonderful teaching aid! Thanks again!!!

imagineerdan

Jon, A few questions:

Is it possible to have a few fade on at once, or seemingly at once, then just one fade on at a time and so fourth? I would like a little more action or light in my backyard display. It seems that cycling through just one at a time seems less impactful.

I know the stamp works serially and can only execute one command at a time but it seems like there would be a way to alternate pwm for two outs, then whether or not the lottery chooses the routine with two flies on at once or just one could be random?


JonnyMac

Not with the Prop-2 because the PWM happens serially and in real-time; as soon as you try to switch to one the other will go off -- the BS2 is just not fast enough to do what you ask.

With the HC-8+ (which uses the Propeller processor) where we can dedicate a processor to dimming all eight outputs at once you will be able to do that an many more things.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Where is the HC-8+ available from?

JonnyMac

It's a new product from us that will be available in the near future.  We're doing final design testing now and then will roll it into production.  It has many advanced features, yet is the same size -- and we expect cost -- as a Prop-2.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

sounds great! Thanks Jon!