November 01, 2024, 04:33:17 AM

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.


Running sequential flickerings lights

Started by imagineerdan, February 02, 2010, 12:06:34 PM

Previous topic - Next topic

imagineerdan

I would like to do a version of the below code for BS2 with 15 outputs. How would that look?
Thanks!

' =========================================================================
'
'   File...... Flicker_Sequence.BS1
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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


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

SYMBOL  FIRST_PIN       = 0
SYMBOL  LAST_PIN        = 7


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

SYMBOL  pinNum          = B2
SYMBOL  idx             = B3
SYMBOL  level           = B4

SYMBOL  lottery         = W5


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

Reset:
  DIRS = %11111111                              ' set outputs

  pinNum = FIRST_PIN


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

Main:
  IF pinNum > LAST_PIN THEN Reset

Flicker_Pin:
  FOR idx = 1 TO 100
    RANDOM lottery
    level = lottery
    PWM pinNum, level, 4                        ' 5ms x 4 x 100 = 2sec
  NEXT
  LOW pinNum

  pinNum = pinNum + 1
  GOTO Main


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


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

JonnyMac

As you can see, the code is almost identical -- you have to change the way to declare constants and variables, and adjust the PWM for the faster speed of the BS2, but the rest is exactly the same.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$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

FIRST_PIN       CON     0
LAST_PIN        CON     14


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

pinNum          VAR     Byte
idx             VAR     Byte
level           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %01111111 : DIRL = %11111111           ' set outputs

  pinNum = FIRST_PIN


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

Main:
  IF pinNum > LAST_PIN THEN Reset

Flicker_Pin:
  FOR idx = 1 TO 100
    RANDOM lottery
    level = lottery
    PWM pinNum, level, 20                        ' 1ms x 20 x 100 = 2sec
  NEXT
  LOW pinNum

  pinNum = pinNum + 1
  GOTO Main


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


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


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

imagineerdan

February 02, 2010, 05:00:48 PM #2 Last Edit: February 02, 2010, 06:42:15 PM by imagineerdan
That worked well Jon Thanks! I as I am testing it I may want more control and a little different of a look. I thought I remebered how to code this but I guess I dont because everything I have been trying have been not working.

I want to flash pin0 then pin2, then pin1 then pin3, then pin2 again etc. I am trying to hard code it so I can just play the sequince. Basically leds strobing really fast in a somewhat but not perfectly sequential way from pin 0 to pin 14 with some repeating. Does this make any sense?

To clarify more-so, I am trying to achieve a "sparkle" or Spark-like" effect moving through pins0-14


JonnyMac

Give this a try.  It stores the sequence in a DATA table, using READ with an index to get the new pin.  The end of a sequence is indicated by 99 in the table.  When this value is read the index is reset and the sequence starts again.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$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

FIRST_PIN       CON     0
LAST_PIN        CON     14


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

idx             VAR     Byte
pinNum          VAR     Byte
flicker         VAR     Byte
level           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %01111111 : DIRL = %11111111           ' set outputs


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

Main:
  READ Sparkle2 + idx, pinNum                   ' read from table
  IF pinNum <= LAST_PIN THEN                    ' check for end marker
    idx = idx + 1                               ' if okay, update for next
  ELSE
    idx = 0                                     ' restart
    GOTO Main
  ENDIF

Flicker_Pin:
  FOR flicker = 1 TO 100
    RANDOM lottery
    level = lottery
    PWM pinNum, level, 20                        ' 1ms x 20 x 100 = 2sec
  NEXT
  LOW pinNum

  GOTO Main


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


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


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

Sparkle1        DATA    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,99

Sparkle2        DATA    0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9
                DATA    8,10,9,11,10,12,11,13,12,14,13,99
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Thanks Jon that works well, I modified it a little bit to just flash rather thann flicker as well as to go from a button press. How can I make it go from 0-14 with a momentary button press and go through all outputs always starting from the beginning. Right now (see the below code) it only goes through the outputs if you hold the button pressed, then when you let go, starts where the sequence left off. Any thoughts?


' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$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

FIRST_PIN       CON     0
LAST_PIN        CON     14

Pressed         CON     1
NotPressed      CON     0

Trigger         PIN     15                      ' SETUP = DN


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

idx             VAR     Byte
pinNum          VAR     Byte
flicker         VAR     Byte
level           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %01111111 : DIRL = %11111111           ' set outputs


' -----[ Program Code ]----------------------------------------------------
Main:
  IF (Trigger = Pressed) THEN Start           '  De-bounce until trigger (pin 15) is pressed

GOTO Main:




start:
  READ Sparkle2 + idx, pinNum                   ' read from table
  IF pinNum <= LAST_PIN THEN                    ' check for end marker
    idx = idx + 1                               ' if okay, update for next
  ELSE
    idx = 0                                     ' restart
    GOTO Main
  ENDIF

Flicker_Pin:
  HIGH pinNum
  PAUSE 10                                       ' speed change
  LOW pinNum


  GOTO Main


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


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


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

Sparkle1        DATA    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,99

Sparkle2        DATA    0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9
                DATA    8,10,9,11,10,12,11,13,12,14,13,99

JonnyMac

Here you go.

Note: You should ALWAYS debounce trigger inputs to prevent false starts from electrical noise.  My program has a debounce loop that also stirs the random number (we use this a lot in prop code).  The button must be pressed to 100ms (0.1s) in order to start.  You can shorten this, but don't go below 25ms.

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

Yes             CON     1
No              CON     0

FIRST_PIN       CON     0
LAST_PIN        CON     14


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

timer           VAR     Byte
idx             VAR     Byte
pinNum          VAR     Byte
flicker         VAR     Byte
level           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %01111111 : DIRL = %11111111           ' set outputs


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

Main:
  timer = 0
  DO
    RANDOM lottery
    PAUSE 5
    timer = (timer + 5) * Trigger
  LOOP UNTIL (timer >= 100)

  idx = 0                                       ' reset index

Sparkle:
  READ Sparkle2 + idx, pinNum                   ' read from table
  IF pinNum <= LAST_PIN THEN                    ' check for end marker
    idx = idx + 1                               ' if okay, update for next
  ELSE
    GOTO Main
  ENDIF

Flicker_Pin:
  FOR flicker = 1 TO 100
    RANDOM lottery
    level = lottery
    PWM pinNum, level, 20                        ' 1ms x 20 x 100 = 2sec
  NEXT
  LOW pinNum
  GOTO Sparkle


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


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


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

Sparkle1        DATA    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,99

Sparkle2        DATA    0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9
                DATA    8,10,9,11,10,12,11,13,12,14,13,99
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

That is perfect what a cool look!! Thanks again Jon

imagineerdan

 I am trying to modify the below code so that pin 13 triggers the sequence the opposite direction (ie flicker pins 12 - 0). So the program would flicker 0-12 when triggered by pin 15, and would trigger 12-0 when pin 13 is triggered. What would this look like?



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

Yes             CON     1
No              CON     0

FIRST_PIN       CON     0
LAST_PIN        CON     12


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

timer           VAR     Byte
idx             VAR     Byte
pinNum          VAR     Byte
flicker         VAR     Byte
level           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %01111111 : DIRL = %11111111           ' set outputs


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

Main:
  timer = 0
  DO
    RANDOM lottery
    PAUSE 5
    timer = (timer + 5) * Trigger
  LOOP UNTIL (timer >= 100)

  idx = 0                                       ' reset index

Sparkle:
  READ Sparkle2 + idx, pinNum                   ' read from table
  IF pinNum <= LAST_PIN THEN                    ' check for end marker
    idx = idx + 1                               ' if okay, update for next
  ELSE
    GOTO Main
  ENDIF

Flicker_Pin:

HIGH pinNum

  PAUSE 25                                       ' speed change

LOW pinNum

  GOTO Sparkle


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


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


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

Sparkle1        DATA    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,99

Sparkle2        DATA    0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9
                DATA    8,10,9,11,10,12,11,13,12,14,13,99


JonnyMac

Since the PBASIC compiler can resolve a DATA table address and move it into a variable I would do it this way:

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


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


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


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

Trigger1        PIN     15                      ' SETUP = DN
Trigger2        PIN     13                      ' SETUP = DN


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

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

Yes             CON     1
No              CON     0

FIRST_PIN       CON     0
LAST_PIN        CON     12


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

flags           VAR     Byte
tr1            VAR      flags.bit7
tr2            VAR      flags.BIT5

timer           VAR     Byte
idx             VAR     Byte
pinNum          VAR     Byte
flicker         VAR     Byte
level           VAR     Byte
lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000           ' clear all
  DIRH = %00011111 : DIRL = %11111111           ' set outputs


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

Main:
  flags = %10100000                             ' assume active
  FOR timer = 1 TO 20
    RANDOM lottery
    PAUSE 5
    flags = flags & INH                         ' scan high pins
  NEXT

Check_Triggers:
  IF flags = %00000000 THEN Main                ' if no trigger, try again

  IF (tr1 = Yes) THEN                           ' Trigger 1 (P15)?
    idx = Sparkle2F                             ' forward
  ELSE
    idx = Sparkle2R                             ' reverse
  ENDIF

Sparkle:
  READ idx, pinNum                              ' read pin # from table
  IF pinNum <= LAST_PIN THEN                    ' check for end marker
    idx = idx + 1                               ' if okay, update for next
  ELSE
    GOTO Main
  ENDIF

Flicker_Pin:
  HIGH pinNum
  PAUSE 25
  LOW pinNum
  GOTO Sparkle


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


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


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

Sparkle1F       DATA    0,1,2,3,4,5,6,7,8,9,10,11,12
                DATA    99

Sparkle1R       DATA    12,11,10,9,8,7,6,5,4,3,2,1,0
                DATA    99

Sparkle2F       DATA    0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9,8,10,9,11,10,12,11
                DATA    99

Sparkle2R       DATA    12,10,11,9,10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0,1
                DATA    99
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

It works perfectly,and I see what you mean about changing the variable