November 22, 2024, 06:32:59 PM

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.


counter / random

Started by J Pettis, September 19, 2007, 09:21:26 PM

Previous topic - Next topic

J Pettis

Jon,
  The code listed below is for a crated beast. The program works fine but the timing of the Cylinder action for the lid seems a little monotnous.  What I would like to do is cycle through the Cylinder portion X number of times to cover the length of the audio track. In addition to, I would like to set three different  pause times and have them randomly selected as well.

Also: What do you think of using a ping sonar as a safety device to ensure that a   TOT is a safe distance away before activating?

Thanks,
        Jarrett

' CRATED_BEAST.BS1
'

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


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  STROBE          = PIN2                  '
SYMBOL  FOG             = PIN1
SYMBOL  CYLINDER        = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0
' -----[ Variables ]-------------------------------------------------------

SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

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

  PAUSE 20000                                   ' warm-up / retrigger delay
  timer = 0                                     ' clear timer for PIR


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10
  timer = timer + 10 * PIR                      ' update PIR timer
  IF timer < 250 THEN Main                      ' wait for valid signal

  FOG = IsOn                                    ' start fogger

  STROBE = IsOn
  PAUSE 2000

  SEROUT Sio, OT2400, ("!AP8", %00, "P", 0)     ' start audio

  Cylinder = IsOpen                             ' open lid
  PAUSE 600
  Cylinder = IsClosed                            ' close lid
  PAUSE 600
  Cylinder = IsOpen                             ' open lid
  PAUSE 600
  Cylinder = IsClosed                            ' close lid
  PAUSE 600
  Cylinder = IsOpen                             ' open lid
  PAUSE 600
  Cylinder = IsClosed                            ' close lid
  PAUSE 600


  GOTO Reset


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


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


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

JonnyMac

September 20, 2007, 08:48:14 AM #1 Last Edit: September 20, 2007, 12:48:06 PM by JonnyMac
I can show you how to do random jumping for a specific period of time -- first, though:

-- about how long will the audio run?
-- where do you want to insert the randomized pause?

Ping's are not great people detectors (when uses as range finders) because bodies are soft and absorb the signal.  That said, if you can set it up as a sonic beam-break, then it works fine.

Here's a version of the program that shakes for the same duration as yours, although it does it randomly:

' =========================================================================
'
'   File...... CRATED_BEAST.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Strobe          = PIN2
SYMBOL  Fog             = PIN1
SYMBOL  Cylinder        = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0


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

SYMBOL  delay           = B2
SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

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

  PAUSE 20000                                   ' warm-up / retrigger delay
  timer = 0                                     ' clear timer for PIR


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10
  timer = timer + 10 * PIR                      ' update PIR timer
  IF timer < 250 THEN Main                      ' wait for valid signal

  Fog = IsOn                                    ' start fogger
  Strobe = IsOn
  PAUSE 2000

  SEROUT Sio, OT2400, ("!AP8", %00, "P", 0)     ' start audio

  timer = 0

Shake_It_Baby:
  Cylinder = Cylinder ^ 1                       ' toggle cylinder state
  RANDOM lottery
  delay = lottery // 51 + 100                   ' make 100 to 150
  PAUSE delay
  timer = timer + delay
  IF timer < 3600 THEN Shake_It_Baby            ' keep running?
    Cylinder = IsOff

  GOTO Reset


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


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


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

J Pettis

Jon,
   i was thinking that the length of the audio would be between 20 - 30 seconds.

  here is an example of the random pauses i would like:

   cylinder IsOpen
   pause 600
   cylinder IsClosed
   pause 1000
   cylinder IsOpen
   pause 700
   cylinder IsOpen
   pause 700
   cylinder IsClosed       

   goto reset

I figure you get the jist of it.

thanks!

JonnyMac

Do you really want the pulses that long?  Let me suggest an update that will run any value between 500 and 1000 -- like this:

Shake_It_Baby:
  Cylinder = Cylinder ^ 1                       ' toggle cylinder state
  RANDOM lottery
  delay = lottery // 501 + 500                  ' make 500 to 1000
  PAUSE delay
  timer = timer + delay
  IF timer < 20000 THEN Shake_It_Baby           ' keep running?
    Cylinder = IsOff

  GOTO Reset


The 20000 toward the end is 20 seconds -- update that once you know the length or your audio.  My trick is a little more sophisticated the on-pause-off, etc., but it gives much more realistic movement in this kind of prop, and it's never the same, cycle-to-cycle.
Jon McPhalen
EFX-TEK Hollywood Office

J Pettis

Jon,   that worked like a charm. As usual thanks for the help.

Jarret