November 22, 2024, 10:46:10 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.


Help with repeat function(or subroutine) of a program to shorten the code.

Started by john@thedevils-den, September 15, 2008, 08:13:29 PM

Previous topic - Next topic

john@thedevils-den

Can someone explain to me the correct way to write this code using a repeat function so I dont have to write the same things over and over.......what I am looking to do once triggered, 2 couches begin dancing around and the lights flicker on and off , all for a total time of about 1 minute or so. I have written this code so far and need some help cleaning it up and adding the light output(which will be a 120vac light through a 12vdc solid state relay). Any help would be great! I am not too concerned the length of time that the couches are on and off as long as it is fast enough to extend the cylinder which pushes the couch up off of the ground and slams it back down......

=========================================================================
'
'   File......Couch Props
'   Purpose... Animated couches
'   Author....John Sbandi
'   E-mail....john@thedevils-den.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Couch1          = PIN5
SYMBOL  Couch2          = PIN4

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

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

SYMBOL  Baud            = OT2400


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

SYMBOL  timer           = B2


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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * Trigger                   ' update timer
  IF Trigger = IsOff THEN Main                  ' wait for trigger
  PAUSE  3000
  Couch1 = IsOn
  PAUSE  2000
  Couch2 = IsOn
  PAUSE  1000
  Couch1 = IsOff
  PAUSE  2000
  Couch2 = IsOff
  PAUSE  3000
  Couch1 = IsOn
  PAUSE  1000
  Couch2 = IsOn
  PAUSE  1500
  Couch1 = IsOff
  PAUSE  1000
  Couch2 = IsOff
  PAUSE  3000
  Couch1 = IsOn
  PAUSE  2000
  Couch2 = IsOn
  PAUSE  1000
  Couch1 = IsOff
  PAUSE  2000
  Couch2 = IsOff
  PAUSE  3000
  Couch1 = IsOn
  PAUSE  1000
  Couch2 = IsOn
  PAUSE  1500
  Couch1 = IsOff
  PAUSE  1000
  Couch2 = IsOff



  GOTO Main


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


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


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


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


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


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


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


JonnyMac

Here's how we tend to approach these things: we random outputs.  The output is never the same one cycle to another and the output just seems more chaotic and scary.

' =========================================================================
'
'   File...... Couch_Props.BS1
'   Purpose... Animated couches
'   Author.... John Sbandi
'   E-mail.... john@thedevils-den.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  Couch1          = PIN5
SYMBOL  Couch2          = PIN4


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

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

SYMBOL  Baud            = OT2400


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

SYMBOL  lottery         = W0
SYMBOL   c1Enable       =  BIT0
SYMBOL   c2Enable       =  BIT1

SYMBOL  delay           = W4
SYMBOL  timer           = W5


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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00110000                              ' make P4-P5 outputs


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  RANDOM lottery                                ' stir random #
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * Trigger                   ' update timer
  IF Trigger = IsOff THEN Main                  ' wait for trigger
    PAUSE  3000

  timer = 0                                     ' reset timer

Shake_Rattle_Roll:
  RANDOM lottery                                ' stir random #
  Couch1 = c1Enable                             ' update outputs
  Couch2 = c2Enable
  delay = lottery // 501 + 250                  ' 0.25 to 0.75 secs
  PAUSE delay
  timer = timer + delay                         ' update timer
  IF timer < 60000 THEN Shake_Rattle_Roll       ' done?
    GOTO Reset


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


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


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

john@thedevils-den

Thanks Jon. can you tell me where I might learn the c1 and c2 enable codes are for and how to use them? I am also lost ont he Random Stir function as well. I am assuming I can modify this program to use more than 2 outputs, how would I do that?

Thanks for your help.

JonnyMac

Forgive me, this time of year I'm writing a lot of code for folks and don't have time for long explanations -- after Halloween I'll have more time.  Here's a couple hints that, combined with reading about the keywords used in the help file, should help you.

We're using a variable call lottery that will be randomized with the RANDOM function.  By assigning lottery to variable location W0 we have access to individual bits (I'm using BIT0 and BIT1 which are part of W0).  For clarity these bits are aliased (renamed) c1Enable and c2Enable.  So, we randomize lottery (W0) which randomizes c1Enable (BIT0) and c2Enable (BIT1).  These values are copied to the pins that are set as outputs.  When either of the enable variables is 1 the output will be on; when it is 0 the output will be off.

Yes, you can expand the program to more outputs.

Here's a link to an article I wrote (11 years ago!) on PBASIC1 programming -- the language used in the Prop-1.  It should help.
-- http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol1/col/nv30.pdf
Jon McPhalen
EFX-TEK Hollywood Office