November 21, 2024, 11:18:29 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.


Help tweaking code

Started by hauntcast, October 03, 2014, 05:03:10 AM

Previous topic - Next topic

hauntcast

Can you change this code so only the lid shakes violently for 6 seconds and plays 3rd sound  when triggered? I don't want the base to shake this year. Thank you in advance.


' {$STAMP BS1}
' {$PBASIC 1.0}

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

SYMBOL  Sio             = 7                     ' SETUP = UP (no ULN)

SYMBOL  Ping            = 6                     ' SETUP = DN

SYMBOL  Lid             = 3                     ' solenoid for lid
SYMBOL  Base            = 2                     ' solenoid for base


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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Trigger         = 1                     ' 10 uS trigger pulse
SYMBOL  Scale           = 10                    ' raw x 10.00 = uS

SYMBOL  RawToIn         = 889                   ' 1 / 73.746 (with **)

SYMBOL  IsHigh          = 1                     ' for PULSOUT
SYMBOL  IsLow           = 0

SYMBOL  Baud            = OT2400                ' Baud for vmusic


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

SYMBOL  status          = B0
SYMBOL   playing        =  BIT7

SYMBOL  thePin          = B1

SYMBOL  idx             = B2
SYMBOL  theWAV          = B3

SYMBOL  rawDist         = W3                    ' raw measurement
SYMBOL  refDist         = W4                    ' reference distance
SYMBOL  measDist        = W5                    ' measured distance

SYMBOL  timer           = W3                    ' for shake timing
SYMBOL  delay           = W4                    ' toggle timing
SYMBOL  lottery         = W5                    ' random #


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

Power_Up:
  PAUSE 3000                                    ' let AP-16+ boot up

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00001100                              ' set outputs

  SEROUT Sio, Baud, ("!AP16", %00, "X")         ' stop if playing


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

Main:
  GOSUB Get_Sonar                               ' get sensor value.
  refDist = rawDist ** RawToIn                  ' set ref value to inches.

Wait:                                           ' check if audience present.
  RANDOM lottery                                ' stir random #
  FOR idx = 1 TO 2                              ' confirm trigger has been
    PAUSE 125                                   ' triggered for 1/4 second
    GOSUB Get_Sonar                             ' 125ms x 2
    measDist = rawDist ** RawToIn
    IF measDist >= refDist THEN Wait
  NEXT

Shake_Lid:
  theWAV = 0                                    ' Play 1st sound
  GOSUB Play_WAV

  thePin = Lid                                  ' shake lid for ~13s
  timer = 13000
  GOSUB Shake_It
  PAUSE 1500                                    ' quiet about 1.5s

Check_Distance:                                 ' Check if audience still present,
  GOSUB Get_Sonar                               ' if still present, continue.
  measDist = rawDist ** RawToIn                 ' if further away, jump to Finale.
  IF measDist >= refDist THEN Finale

Shake_Base:
  theWAV = 1                                    ' Play 2nd sound
  GOSUB Play_WAV

  thePin = Base                                 ' shake base for ~13s
  timer = 13000
  GOSUB Shake_It
  PAUSE 1500                                    ' quiet about 1.5s

Finale:
  theWAV = 2                                    ' Play 3rd sound
  GOSUB Play_WAV

  timer = 11000                                 ' match SFX02 timing

Shake_Both:
  RANDOM lottery
  thePin = lottery & %00001100                  ' randomze lid & base
  PINS = PINS &/ %00001100 | thePin             ' update outputs
  IF timer <= 100 THEN Done
    PAUSE 100
    timer = timer - 100
    GOTO Shake_Both

Done:
  PAUSE 15000                                   ' Wait 15 seconds before playing again
  GOTO Reset


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

' This subroutine triggers the Ping sonar sensor and measures
' the echo pulse.  The raw value from the sensor is converted to
' microseconds based on the Stamp module in use.  This value is
' divided by two to remove the return trip -- the result value is
' the distance from the sensor to the target in microseconds.

Get_Sonar:
  LOW Ping                                      ' make trigger 0-1-0
  PULSOUT Ping, Trigger                         ' activate sensor
  PULSIN  Ping, IsHigh, rawDist                 ' measure echo pulse
  rawDist = rawDist * Scale                     ' convert to uS
  rawDist = rawDist / 2                         ' remove return trip
  RETURN

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

' Put file # to play in AP-16+
' -- add starting location(s) of file names manually

Play_WAV:
  SEROUT Sio, Baud, ("!AP16", %00, "PS", theWAV, 1)
  RETURN

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

' Shake one output -- defined by "thePin"

Shake_It:
  TOGGLE thePin
  RANDOM lottery
  delay = lottery // 101 + 100                  ' random, 100 to 200
  IF timer < delay THEN Shake_Exit
    PAUSE delay
    timer = timer - delay
    GOTO Shake_It

Shake_Exit:
  PAUSE timer
  LOW thePin
  RETURN


' -----[ EEPROM Data ]-----------------------------------------------------

JackMan

Here ya go. The shake time for the lid has been changed to 6s.
The sections for the base shake have been commented out (')


' {$STAMP BS1}
' {$PBASIC 1.0}

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

SYMBOL  Sio             = 7                     ' SETUP = UP (no ULN)

SYMBOL  Ping            = 6                     ' SETUP = DN

SYMBOL  Lid             = 3                     ' solenoid for lid
SYMBOL  Base            = 2                     ' solenoid for base


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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Trigger         = 1                     ' 10 uS trigger pulse
SYMBOL  Scale           = 10                    ' raw x 10.00 = uS

SYMBOL  RawToIn         = 889                   ' 1 / 73.746 (with **)

SYMBOL  IsHigh          = 1                     ' for PULSOUT
SYMBOL  IsLow           = 0

SYMBOL  Baud            = OT2400                ' Baud for vmusic


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

SYMBOL  status          = B0
SYMBOL   playing        =  BIT7

SYMBOL  thePin          = B1

SYMBOL  idx             = B2
SYMBOL  theWAV          = B3

SYMBOL  rawDist         = W3                    ' raw measurement
SYMBOL  refDist         = W4                    ' reference distance
SYMBOL  measDist        = W5                    ' measured distance

SYMBOL  timer           = W3                    ' for shake timing
SYMBOL  delay           = W4                    ' toggle timing
SYMBOL  lottery         = W5                    ' random #


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

Power_Up:
  PAUSE 3000                                    ' let AP-16+ boot up

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00001100                              ' set outputs

  SEROUT Sio, Baud, ("!AP16", %00, "X")         ' stop if playing


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

Main:
  GOSUB Get_Sonar                               ' get sensor value.
  refDist = rawDist ** RawToIn                  ' set ref value to inches.

Wait:                                           ' check if audience present.
  RANDOM lottery                                ' stir random #
  FOR idx = 1 TO 2                              ' confirm trigger has been
    PAUSE 125                                   ' triggered for 1/4 second
    GOSUB Get_Sonar                             ' 125ms x 2
    measDist = rawDist ** RawToIn
    IF measDist >= refDist THEN Wait
  NEXT

Shake_Lid:
  theWAV = 0                                    ' Play 1st sound
  GOSUB Play_WAV

  thePin = Lid                                  ' shake lid for ~6s
  timer = 6000
  GOSUB Shake_It
  PAUSE 1500                                    ' quiet about 1.5s

Check_Distance:                                 ' Check if audience still present,
  GOSUB Get_Sonar                               ' if still present, continue.
  measDist = rawDist ** RawToIn                 ' if further away, jump to Finale.
  IF measDist >= refDist THEN Finale

'Shake_Base:
  'theWAV = 1                                    ' Play 2nd sound
  'GOSUB Play_WAV

  'thePin = Base                                 ' shake base for ~13s
  'timer = 13000
  'GOSUB Shake_It
  'PAUSE 1500                                    ' quiet about 1.5s

Finale:
  theWAV = 2                                    ' Play 3rd sound
  GOSUB Play_WAV

  timer = 11000                                 ' match SFX02 timing

'Shake_Both:
  'RANDOM lottery
  'thePin = lottery & %00001100                  ' randomze lid & base
  'PINS = PINS &/ %00001100 | thePin             ' update outputs
  'IF timer <= 100 THEN Done
    'PAUSE 100
    'timer = timer - 100
    'GOTO Shake_Both

Done:
  PAUSE 15000                                   ' Wait 15 seconds before playing again
  GOTO Reset


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

' This subroutine triggers the Ping sonar sensor and measures
' the echo pulse.  The raw value from the sensor is converted to
' microseconds based on the Stamp module in use.  This value is
' divided by two to remove the return trip -- the result value is
' the distance from the sensor to the target in microseconds.

Get_Sonar:
  LOW Ping                                      ' make trigger 0-1-0
  PULSOUT Ping, Trigger                         ' activate sensor
  PULSIN  Ping, IsHigh, rawDist                 ' measure echo pulse
  rawDist = rawDist * Scale                     ' convert to uS
  rawDist = rawDist / 2                         ' remove return trip
  RETURN

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

' Put file # to play in AP-16+
' -- add starting location(s) of file names manually

Play_WAV:
  SEROUT Sio, Baud, ("!AP16", %00, "PS", theWAV, 1)
  RETURN

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

' Shake one output -- defined by "thePin"

Shake_It:
  TOGGLE thePin
  RANDOM lottery
  delay = lottery // 101 + 100                  ' random, 100 to 200
  IF timer < delay THEN Shake_Exit
    PAUSE delay
    timer = timer - delay
    GOTO Shake_It

Shake_Exit:
  PAUSE timer
  LOW thePin
  RETURN


' -----[ EEPROM Data ]-----------------------------------------------------