November 21, 2024, 12:14:37 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.


Help with blink sparkle effect code

Started by imagineerdan, January 11, 2012, 12:38:33 PM

Previous topic - Next topic

imagineerdan

Hi, I am using the below code to create a sparkle effect similar to a fiber optic twinkle. It works really well the way it is but I need all the outputs (0-14) to turn on at once for 250 ms then for the rest of the code I have provided below to run. Then of course like the below code when pin 15 contact closure is off the code sits and waits to be triggered again. It works well how the sparkle stays on only when pin15 has a contact closure.

' =========================================================================
'
'   File...... Fireflies.BS2
'
'   E-mail....
'   Started...
'   Updated... November 2011
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

Trigger         PIN     15                      ' SETUP = DN

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

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

Yes             CON     1
No              CON     0

Pressed         CON     1
NotPressed      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 ]----------------------------------------------------
Start:

IF (Trigger = Pressed) THEN Main:

GOTO Start:


Main:
  DO
    FOR idx = 1 TO 3                            ' tumble random generator
      RANDOM lottery
    NEXT
    flyNum = lottery // 14                      ' 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 = 5                                     ' delay between flies ##################################
  PAUSE timer
  GOSUB Fade_Off                                ' deactivate

  timer = 5                                     ' delay between flies  #################################
  PAUSE timer

  oldFlies = oldFlies | mask                    ' mark this fly
  'IF (oldFlies = %11111111) THEN
    GOTO Reset
  'ELSE
    GOTO Start



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

Fade_On:
  timer = 1                                     ' randomize brighten time
  FOR idx = 0 TO 255 STEP 6                     ' Blink speed ON  #############################################
    PWM flyNum, idx, timer                      ' brighten the output
  NEXT
  HIGH flyNum                                   ' leave the output on
  RETURN


Fade_Off:
  timer = 1                                     ' randomize fade time
  FOR idx = 255 TO 0 STEP -6                    ' Blink Speed off ############################################
    PWM flyNum, idx, timer                      ' dim the output
  NEXT
  LOW flyNum                                    ' leave the output off
  RETURN


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


' -----[ User Data ]-------------------------------------------------------

JonnyMac

January 11, 2012, 01:04:18 PM #1 Last Edit: January 11, 2012, 01:06:02 PM by JonnyMac
I can do that in four lines of code:

  OUTS = $7FFF
  DIRS = $7FFF
  PAUSE 250
  OUTS = $0000


Put this block where you need the outputs to "burst" on for 250ms.  Note that you cannot ramp 15 outputs; just on and off control when dealing with multiple pins.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Thanks John, It seems where ever I put those lines in the code it just makes all the lights blink on and off over and over again, not a burst then a return to the random flicker... Is there a place to put your lines of code so it will burst, then proceed to the flicker code I have? thanks soo much

JonnyMac

Here's the thing: I suck at fixing brocken code (written by others, that is) which is why I just showed you how to turn the outputs on and then back off.  Please tell me EXACLTY how you want your program to behave, and I'll write you a fresh program that does what you want.  In the end you may learn a few new tricks.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Ok I understand totally here is what I am trying to do:
For as long as as pin 15 is triggered the following happens. Pins 0-14 all blink at once for 250ms then pin 0-14 all sparkle randomly one at a time. The time in between flashes can be globally defines as well as the pause between flashes. It loops throught sparkling each pin until pin15 is not contact closed anymore all pins go back to off. So each time it is triggered it does the above

JonnyMac

Give this a try.

Just a note: It's fine to copy-and-paste from other programs, but at some point optomization should be considered to remove redundant or unnecessary code.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

Trigger         PIN     15                      ' SETUP = DN


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

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

TrOn            CON     1
TrOff           CON     0

Yes             CON     1
No              CON     0


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

lottery         VAR     Word                    ' random value
timer           VAR     Word

theFly          VAR     Byte
lastFly         VAR     Byte
level           VAR     Byte


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

Reset:
  OUTS = $0000                                  ' all off
  DIRS = $7FFF


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

Main:
  timer = 0
  DO WHILE (timer < 100)                        ' debounce = 100ms
    PAUSE 5
    IF (Trigger = TrOff) THEN                   ' if no trigger
      timer = 0                                 '  reset timer
    ELSE
      timer = timer + 5                         '  else update for loop time
    ENDIF
    RANDOM lottery                              ' stir random # while waiting
  LOOP

Flash:
  OUTS = $7FFF                                  ' all on
  DIRS = $7FFF
  PAUSE 250                                     ' hold 250ms
  OUTS = $0000                                  ' all off

Light_Flies:
  RANDOM lottery
  theFly = lottery // 15                        ' randomize, 0 to 14
  IF (theFly = lastFly) THEN                    ' if repeat
    GOTO Light_Flies                            ' try again
  ELSE
    lastFly = theFly                            ' save for next cycle
  ENDIF

  GOSUB Fade_On

  RANDOM lottery
  timer = lottery // 6 + 5                      ' 5 to 10ms (too short?)
  PAUSE timer

  GOSUB Fade_Off

  BRANCH Trigger, [Reset, Light_Flies]          ' re-check Trigger input

  GOTO Reset

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

Fade_On:
  FOR level = 0 TO 255 STEP 6                   ' increase brightness
    PWM theFly, level, 1                        ' pwm the output
  NEXT
  HIGH theFly                                   ' keep on after pwm
  RETURN

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

Fade_Off:
  FOR level = 255 TO 0 STEP 6                   ' decrease brightness
    PWM theFly, level, 1                        ' pwm the output
  NEXT
  LOW theFly                                    ' ensure completely off
  RETURN


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

imagineerdan

This works great! I tweaked some of the timing but it is perfect thanks!!!

JonnyMac

Excellent.  Is this a personal project or something for WDI? (I live close to WDI offices)
Jon McPhalen
EFX-TEK Hollywood Office