May 15, 2024, 12:17:58 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.


Shifting through code by triggering same pin

Started by imagineerdan, August 07, 2012, 11:43:52 AM

Previous topic - Next topic

imagineerdan

I am working with the attached code and would like the effect to change sequince when I trigger the input pin each of three times. So when the pin is triggered the first time I would like code to flash though pins 14, 13, 10, 9, 7, 3, 0.
then when I trigger the same pin again it will flash through pins 14, 13, 12, 11, 10, 9, 8, 1. Then when I trigger the last time it will flash through pins 14, 13, 12, 11,10, 9, 8, 7, 2,1,0

Then when triggered again its back to the first state of progression? Is it possible to also keep all these numbers as variables which I can change output pins around etc?



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

SYMBOL Trigger=        PIN7                     ' SETUP = DN


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




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

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


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

Reset:

  DIRS = %10000000                              ' make LEDs outputs
  PINS = %01111111

' -----[ Program Code ]----------------------------------------------------
Main:

IF Trigger=1 THEN Chaser

GOTO Main:



Chaser:
  FOR theLamp = 14 TO 1                        ' loop through outputs




    speed = 1                                 ' change over all speed
    FOR level = 0 TO 255 STEP 8               ' change freq
      PWM theLamp, level, speed
    NEXT

  NEXT

  GOTO Main:

JonnyMac

Here's one way to do it:

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

timer           VAR     Word
addr            VAR     Word                   ' address of sequence

seqNum          VAR     Byte                   ' current sequence #
pinNum          VAR     Byte                   ' pin # to pulse
entries         VAR     Byte                   ' # entries in sequence


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

Reset:


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

Main:
  timer = 0
  DO WHILE (timer < 100)                       ' debounce = 100ms
    PAUSE 5
    IF (trigger = TrOff) THEN
      timer = 0
    ELSE
      timer = timer + 5
    ENDIF
    PAUSE 5
  LOOP

  ' get address of sequence

  LOOKUP seqNum, [Sequence0, Sequence1, Sequence2], addr

  READ addr, entries                           ' read entries in sequence
  addr = addr + 1                              ' advance address pointer

  DO
    READ addr, pinNum, Word timer              ' read pin # and timing
    HIGH pinNum                                ' pin on
    PAUSE timer                                ' hold
    LOW pinNum                                 ' pin off
    entries = entries - 1                      ' update entries count
    addr = addr + 3                            ' advance address pointer
  LOOP UNTIL (entries = 0)

  seqNum = (seqNum + 1) // 3                   ' update sequence #

  GOTO Main


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


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


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

' Note: Timing element _MUST_ be set as Word

Sequence0          DATA    7                      ' 7 entries in table
                   DATA   14, Word 100            ' pin, timing (ms)
                   DATA   13, Word 100
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    7, Word 100
                   DATA    3, Word 100
                   DATA    0, Word 100

Sequence1          DATA    8
                   DATA   14, Word 100
                   DATA   13, Word 100
                   DATA   12, Word 100
                   DATA   11, Word 100
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    8, Word 100
                   DATA    1, Word 100

Sequence2          DATA   11
                   DATA   14, Word 100
                   DATA   13, Word 100
                   DATA   12, Word 100
                   DATA   11, Word 100
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    8, Word 100
                   DATA    7, Word 100
                   DATA    2, Word 100
                   DATA    1, Word 100
                   DATA    0, Word 100
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

That looks great I cant wait to try it! Is it possible to make all of the pins addressed in each sequence flash at once and do specific things like that in each sequence? thanks Jon!

JonnyMac

So long as you still want to pulse outputs this update allows you to do one pin or multiples.  With a group of N pins, the first N-1 pins in the group will have a time value of 0.  Note, too, that instead of LOW pinNum at the end of a pin that all outputs are turn off to enable group control.

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

timer           VAR     Word
addr            VAR     Word                   ' address of sequence

seqNum          VAR     Byte                   ' current sequence #
pinNum          VAR     Byte                   ' pin # to pulse
entries         VAR     Byte                   ' # entries in sequence


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

Reset:


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

Main:
  timer = 0
  DO WHILE (timer < 100)                       ' debounce = 100ms
    PAUSE 5
    IF (trigger = TrOff) THEN
      timer = 0
    ELSE
      timer = timer + 5
    ENDIF
    PAUSE 5
  LOOP

  ' get address of sequence

  LOOKUP seqNum, [Sequence0, Sequence1, Sequence2], addr

  READ addr, entries                           ' read entries in sequence
  addr = addr + 1                              ' advance address pointer

  DO
    READ addr, pinNum, Word timer              ' read pin # and timing
    HIGH pinNum                                ' pin on
    IF (timer > 0) THEN
      PAUSE timer                              ' hold
      OUTS = $0000                             ' all off
    ENDIF
    entries = entries - 1                      ' update entries count
    addr = addr + 3                            ' advance address pointer
  LOOP UNTIL (entries = 0)

  seqNum = (seqNum + 1) // 3                   ' update sequence #

  GOTO Main


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


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


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

' Note: Timing element _MUST_ be set as Word

Sequence0          DATA    7                      ' 7 entries in table
                   DATA   14, Word   0            ' pin, timing (ms)
                   DATA   13, Word   0
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    7, Word 100
                   DATA    3, Word 100
                   DATA    0, Word 100

Sequence1          DATA    8
                   DATA   14, Word 100
                   DATA   13, Word 100
                   DATA   12, Word 100
                   DATA   11, Word 100
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    8, Word 100
                   DATA    1, Word 100

Sequence2          DATA   11
                   DATA   14, Word 100
                   DATA   13, Word 100
                   DATA   12, Word 100
                   DATA   11, Word 100
                   DATA   10, Word 100
                   DATA    9, Word 100
                   DATA    8, Word 100
                   DATA    7, Word 100
                   DATA    2, Word 100
                   DATA    1, Word 100
                   DATA    0, Word 100
Jon McPhalen
EFX-TEK Hollywood Office