May 15, 2024, 12:14:08 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Multiple inputs

Started by dgme, May 16, 2012, 10:36:49 AM

Previous topic - Next topic

dgme

I would like to have two different triggers run two separate code sequences on a Prop2.  What is the best way to have the program check for a trigger from both and then run the appropriate code?  Below is my guess as to what it should look like but I'm wondering if there is a better way.  Also this is my first code for a Prop2, I've used the Prop1 before, so there may be some other problems in my code.

' =========================================================================
'
'   File....... Opening sequence.BS2
'   Purpose.... Start the show
'   Author..... EFX-TEK (www.efx-tek.com)
'   E-mail..... teamefx@efx-tek.com
'   Started....
'   Updated.... 16 MAE 2012
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Runs opening show sequence button on P13 is pressed and runs second part of show when P12 is pressed
'
'
' Replace the ULN2803 on P8..P15 with a ULN2003 to allow the pot circuit
' on P15 to operate correctly.


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


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

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


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

IsOn            CON     1
IsOff           CON     0


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

valve1  PIN        0
valve2  PIN        1
valve3  PIN        2
valve4  PIN        3
valve5  PIN        4


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

Reset:


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

Main:
IF (Trigger1 = Isoff) THEN Alternative               'wait for first trigger activation
HIGH valve1                                                      'turn on valve1
PAUSE 1000                                                      'hold for 1 second
LOW valve1                                                       'turn off valve1

HIGH valve2                             'turn on valve2
PAUSE 1000                              'hold for 1 second
LOW valve2                              'turn off valve2

HIGH valve5                             'turn on valve5
PAUSE 1000                              'hold for 1 second
LOW valve5                              'turn off valve5

Alternative:
IF (Trigger2 = Isoff) THEN Main        'wait for second trigger activation
HIGH valve3                            'turn on valve3
PAUSE 1000                             'hold for 1 second
LOW valve3                             'turn off valve3

HIGH valve4                            'turn on valve4
PAUSE 1000                             'hold for 1 second
LOW valve4                             'turn off valve4

HIGH valve5                             'turn on valve5
PAUSE 1000                              'hold for 1 second
LOW valve5                              'turn off valve5

  GOTO Main

JonnyMac

May 16, 2012, 11:03:05 AM #1 Last Edit: May 16, 2012, 11:10:42 AM by JonnyMac
It's always best to debounce inputs.  Even though the Prop-1 and Prop-2 are not terribly fast, they are fast enough to interpret an electrical noise pulse (e.g., from a nearby walkie-talkie) as a trigger input when an input sampled the way your code does.  What we need to remember is that the wires we connect to the controller act like antennas turning radio waves into electrical pulses.

Here's a template that follows your channel assignments and debounces both inputs.  If one of the other is active, that sequence will run.  If neither or both are active, the inputs are sampled again.

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


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


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


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

Trigger1        PIN     13                      ' SETUP = DN
Trigger2        PIN     12                      ' 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 ]-------------------------------------------------------

triggers        VAR     Byte
idx             VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000
  DIRH = %00000000 : DIRL = %00000000


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

Main:
  triggers = %11                                ' arm both triggers
  FOR idx = 1 TO 20                             ' 100ms debounce
    PAUSE 5                                     ' delay between samples
    triggers = triggers & IND                   ' sample trigger pins
    RANDOM lottery
  NEXT

  IF (triggers = %10) THEN
    GOTO Sequence_1
  ELSEIF (triggers = %01) THEN
    GOTO Sequence_2
  ELSE
    ' none or tie
    GOTO Main
  ENDIF

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

Sequence_1:

  GOTO Main

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

Sequence_2:

  GOTO Main


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


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


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

dgme

Thanks for the insight.  I didn't think to debounce and eventually this will be for a one and done stage show that takes time to reset effects so false triggering would be very bad.

dgme

I've have gotten a bit deeper into this project now and I not clear on one thing.  How would I change which the pins the triggers are on.  In my original setup I though it would be P12 and P13 but then I wanted to move it to P13 and P14 so I changed from

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

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

to


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

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

but the triggers remain on P12 and P13.

I assume I need to change something in the following section of code but I can't figure out what.

Main:
  triggers = %11                                ' arm both triggers
  FOR idx = 1 TO 20                             ' 100ms debounce
    PAUSE 5                                     ' delay between samples
    triggers = triggers & IND                   ' sample trigger pins
    RANDOM lottery
  NEXT

  IF (triggers = %10) THEN
    GOTO Sequence_1
  ELSEIF (triggers = %01) THEN
    GOTO Sequence_2
  ELSE
    ' none or tie
    GOTO Main
  ENDIF

I know I could use P12 and P13 as inputs and move one of my outputs to P14 for an easy fix but I would like to understand what is going on here in case I want to ad a third trigger in the future.  Here is the entire code.  At this point my programs are cobbled together from different examples that I think do what I want so there may be unnecessary Constants or Variables or Initialization parts but it seems to function correctly, other than the triggers.

' =========================================================================
'
'   File....... Opening sequence.BS2
'   Purpose.... Start the show
'   Author..... EFX-TEK (www.efx-tek.com)
'   E-mail..... teamefx@efx-tek.com
'   Started....
'   Updated.... 16 MAE 2012
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Runs opening show sequence button on P13 is pressed and runs second part of show when P12 is pressed
'
'
' Replace the ULN2803 on P8..P15 with a ULN2003 to allow the pot circuit
' on P15 to operate correctly.


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


' -----[ I/O Definitions ]-------------------------------------------------
Sio             PIN     15                      ' no ULN / SETUP = UP
Trigger1        PIN     14                      ' SETUP = DN
Trigger2        PIN     13                      ' SETUP = DN

air0  PIN 0
air1  PIN 1
air2  PIN 2
air3  PIN 3
air4  PIN 4
air5  PIN 5
air6  PIN 6
air7  PIN 7
air8  PIN 8
air9  PIN 9
air10 PIN 10
pillar  PIN 11
podium  PIN 12


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

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T38K4       CON     45
  #CASE BS2PX
    T2400       CON     1646
    T38K4       CON     84
#ENDSELECT

SevenBit        CON     $2000
Inverted        CON     $4000
Open            CON     $8000
Baud            CON     Open + T38K4                    ' Baud jumper in

Addr            CON     %00                             ' %00 - %11

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

TrOn            CON     1
TrOff           CON     0

Yes             CON     1
No              CON     0


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

triggers        VAR     Byte
idx             VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %00000000 : OUTL = %00000000
  DIRH = %00000000 : DIRL = %00000000
SEROUT Sio, Baud, ["!RC4", %00, "X"]          ' set RC-4 all off

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

Main:
  triggers = %11                                ' arm both triggers
  FOR idx = 1 TO 20                             ' 100ms debounce
    PAUSE 5                                     ' delay between samples
    triggers = triggers & IND                   ' sample trigger pins
    RANDOM lottery
  NEXT

  IF (triggers = %10) THEN
    GOTO Sequence_1
  ELSEIF (triggers = %01) THEN
    GOTO Sequence_2
  ELSE
    ' none or tie
    GOTO Main
  ENDIF

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

Sequence_1:
HIGH air0                             'turn on air0
PAUSE 300                              'hold for 1 second
LOW air0                              'turn off air0

PAUSE 1000

HIGH air1                             'turn on air1
PAUSE 300                              'hold for 1 second
LOW air1                              'turn off air1

PAUSE 500

HIGH air2                            'turn on air2
PAUSE 300                             'hold for 1 second
LOW air2                             'turn off air2

HIGH air3                            'turn on air3
PAUSE 300                             'hold for 1 second
LOW air3                             'turn off air3

HIGH air4                             'turn on air4
PAUSE 300                              'hold for 1 second
LOW air4                              'turn off air4

HIGH air5                             'turn on air5
PAUSE 300                              'hold for 1 second
LOW air5                              'turn off air5

HIGH air6                             'turn on air6
PAUSE 300                              'hold for 1 second
LOW air6                              'turn off air6

HIGH pillar                          'tips over pillar
PAUSE 2000                            'pillar tips
LOW pillar                            'pillar on the ground

PAUSE 10000

SEROUT Sio, Baud, ["!RC4", %00, "R", 1, 1]
PAUSE 1000
SEROUT Sio, Baud, ["!RC4", %00, "R", 1, 0]

PAUSE 300

SEROUT Sio, Baud, ["!RC4", %00, "R", 2, 1]
PAUSE 1000
SEROUT Sio, Baud, ["!RC4", %00, "R", 2, 0]

PAUSE 300

SEROUT Sio, Baud, ["!RC4", %00, "R", 3, 1]
PAUSE 1000
SEROUT Sio, Baud, ["!RC4", %00, "R", 3, 0]

PAUSE 300

SEROUT Sio, Baud, ["!RC4", %00, "R", 4, 1]
PAUSE 1000
SEROUT Sio, Baud, ["!RC4", %00, "R", 4, 0]

  GOTO Main

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

Sequence_2:


HIGH podium                             'lowers sand bag
PAUSE 4000

SEROUT Sio, Baud, ["!RC4", %00, "S", %1111]         'all fire machines on
PAUSE 1500
SEROUT Sio, Baud, ["!RC4", %00, "S", %0000]         'all fire machines off

PAUSE 1500

SEROUT Sio, Baud, ["!RC4", %00, "S", %1111]         'all fire machines off
PAUSE 1500
SEROUT Sio, Baud, ["!RC4", %00, "S", %0000]         'all fire machines on

PAUSE 3000                             'hold for 3 seconds
LOW podium                              'shoots sand bag into the air


  GOTO Main


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


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


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

JonnyMac

The update is easy when you understand the architecture of the BASIC Stamp 2.  Here's the fix:

Main:
  triggers = %0110                              ' arm both triggers (on IND)
  FOR idx = 1 TO 20                             ' 100ms debounce
    PAUSE 5                                     ' delay between samples
    triggers = triggers & IND                   ' sample trigger pins
    RANDOM lottery
  NEXT


  IF (triggers = %0100) THEN                    ' P14 active?
    GOTO Sequence_1
  ELSEIF (triggers = %0010) THEN                ' P13 active?
    GOTO Sequence_2
  ELSE
    ' none or tie
    GOTO Main
  ENDIF


The red lines are updated to make the code easier to understand. Here's what's happening: In the debounce loop we are scanning 4-bit port IND which comprise the inputs P15..P12.  By showing all four bits in the code things become a little more obvious; you slid your inputs up by one pin so we have to slide the test bits left by one to match.
Jon McPhalen
EFX-TEK Hollywood Office