October 31, 2024, 07:30:56 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.


Controling 2 props with 2 pirs

Started by MassageMstr1, October 18, 2007, 02:31:47 AM

Previous topic - Next topic

MassageMstr1

Hey John,

I would like to control two different props with 2 different pirs.  In reading through the board and what I remember from your workshop I wrote and cut a pasted a code together.  Can you look it over and tell me if I'm on the right track or not.

This is going to be in the bathroom and I will have one pir at the door.  When triggered it turn on the celing crawler that I used the HB25 motor controller with last year.  I want a 3 second delay after triggered, then have her crawl forward full speed for 6 seconds.  Then pause for for 12 seconds, then crawl in reverse at 1/2 speed for 12 seconds.  Then have a 30 second paue before being able to be triggered again.

The second show or prop will be a ghoul that sits on the toilet tank with long arms that encircle the toilet seat.  The arms start off open, but when triggered by the second pir, pause for 10 seconds then close and pause for 10 seconds, then open again.  Then pause for 60 seconds before being triggered again.  The arms will be controlled with a 12 or 24VDC solenoid.

Here is what I came up with, let me know what you think or if you need any more info.

Thanks 
Daniel

' =========================================================================
'
'   File...... Bathroom Show
'   Purpose...
'   Author.... Daniel Venditti
'   E-mail....
'   Started...  Oct, 18, 2007
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

HB25           PIN     15            ' remove setup
PIR1            PIN     14            ' SETUP = DN
PIR2            PIN     13            ' SETUP = DN
Ghoul           PIN      1

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

IsOpen          CON     1
IsClosed         CON     0

T2400           CON     396                      ' slow baud
T38K4           CON     6                         ' fast baud
Open            CON     $8000                   ' for AP-8, RC-4, DC-16
Baud            CON     Open | T38K4

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

pirTimer        VAR     Byte

speed           VAR     Word                    ' 500 to 1000

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

Check_Motor:
  IF HB25 = 0 THEN Check_Motor                  ' allow HB25 to initialize
  LOW HB25                                               ' set for PULSOUT
  GOSUB Stop_Motor

Reset:
  GOSUB Stop_Motor
  PAUSE 30000                                           ' wait 30 seconds

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

Main:
  DO
    pirTimer = (pirTimer + PIR1) * PIR
    PAUSE 10
    LOOP UNTIL (pirTimer > 25)
    IF (pirTimer > 25) THEN
    GOSUB Show 1

  DO
   pirTimer = (pirTimer + PIR2) * PIR
   PAUSE 10
   LOOP UNTIL (pirTimer > 25)
   IF (pirTimer > 25) THEN
   GOSUB Show 2

  GOTO Main


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

  Stop_Motor:
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                      ' center = stop
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                      ' insurance command
  RETURN

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

  Set_Motor_Speed:
  PAUSE 6                                          ' command hold-off
  PULSOUT HB25, speed                      ' set speed/direction
  RETURN

' -------------------------------------------------------------------------
  Show1:
  PAUSE  3000                                   ' delay 3 seconds
  PULSOUT HB25, 200
  PAUSE  6000                                   ' run motor 6 sec

  GOSUB Stop_Motor
  PAUSE 15000                                   ' stop motor 15 sec

  PULSOUT HB25, 125
  PAUSE  12000                                  ' reverse motor 12 sec

  GOTO Reset                                    ' repeat

' -------------------------------------------------------------------------
  Show2:
  PAUSE 10000                                   ' delay 10 seconds
  Ghoul = IsClosed                               ' ghouls arms close
  PAUSE 20000                                   ' pause 20 seconds
  Ghoul = IsOpen                                ' ghouls arms open
  RETURN

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

JonnyMac

Dahniel,

Do the "shows" need to be able to run simultaneously?  Or does the show that's presently running block the other until it's finished?  I wrote about dual prop programming with the Prop-1 in Nuts & Volts, and as you'll see if you read the article, having two completely independent props running simultaneously requires programming that is somewhat involved.

http://www.parallax.com/dl/docs/cols/nv/vol7/col/NV135.pdf
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

October 18, 2007, 10:41:18 AM #2 Last Edit: October 19, 2007, 07:22:50 AM by JonnyMac
Your version seems to only want one at a time to run, so here's how I'd update your code to do that.  Note that my scan loop is a little cleaner, removing redundant code and correcting a bit of timing error introduced by simply stacking two scans, both padded with a PAUSE.  I also increase the scan rate as the BS2 is quite a bit faster than the BS1.

' =========================================================================
'
'   File...... Bathroom Show
'   Purpose...
'   Author.... Daniel Venditti
'   E-mail....
'   Started... Oct 18, 2007
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

HB25            PIN     15                      ' remove setup
PIR1            PIN     14                      ' SETUP = DN
PIR2            PIN     13                      ' SETUP = DN
Ghoul           PIN      1


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

IsOpen          CON     1
IsClosed        CON     0

T2400           CON     396                     ' slow baud
T38K4           CON     6                       ' fast baud
Open            CON     $8000                   ' for AP-8, RC-4, DC-16
Baud            CON     Open | T38K4


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

timer1          VAR     Byte
timer2          VAR     Byte
speed           VAR     Word


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

Check_Motor:
  IF HB25 = 0 THEN Check_Motor                  ' allow HB25 to initialize
  LOW HB25                                      ' set for PULSOUT
  GOSUB Stop_Motor

Reset:
  GOSUB Stop_Motor
  PAUSE 30000                                   ' wait 30 seconds


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

Main:
  timer1 = 0                                    ' reset debounce timers
  timer2 = 0
  DO
    PAUSE 5
    timer1 = (timer1 + 5) * PIR1                ' update timers
    timer2 = (timer2 + 5) * PIR2
    IF timer1 >= 250 THEN Show_1
    IF timer2 >= 250 THEN Show_2
  LOOP


Show_1:
  PAUSE 3000                                    ' delay 3 seconds
  PULSOUT HB25, 1000
  PAUSE  6000                                   ' run motor 6 sec
  GOSUB Stop_Motor
  PAUSE 15000                                   ' stop motor 15 sec
  PULSOUT HB25, 625
  PAUSE  12000                                  ' reverse motor 12 sec
  GOTO Reset                                    ' repeat


Show_2:
  PAUSE 10000                                   ' delay 10 seconds
  Ghoul = IsClosed                              ' ghouls arms close
  PAUSE 20000                                   ' pause 20 seconds
  Ghoul = IsOpen                                ' ghouls arms open
  GOTO Reset


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

Stop_Motor:
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                             ' center = stop
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                             ' insurance command
  RETURN

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

Set_Motor_Speed:
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, speed                           ' set speed/direction
  RETURN

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


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

MassageMstr1

The shows will definately start at different times, but there is a good chance that the second show will be triggered before the first one is completely finished.  Will that be a problem with this code?  Yes I see how you cleaned and reduced the code.  Thanks Jon!

JonnyMac

October 18, 2007, 05:16:06 PM #4 Last Edit: October 19, 2007, 07:23:32 AM by JonnyMac
You know... the reason we made the Prop-1 so cheap is so that prop builders could dedicate one per prop and not go broke.   ;D

Below you'll find a program that will have a "Scanners" type effect on some readers.  It's a simple (a truly relative term...) multi-tasker for your specific situation.  Be very careful when making changes.

CAUTION: The program compiles properly, but I have not tested as I don't have an HB25/motor setup -- you'll have to trust my programming skills....


' =========================================================================
'
'   File...... Bathroom_Advanced.BS2
'   Purpose...
'   Author.... Daniel Venditti; updated by Jon Williams (EFX-TEK)
'   E-mail....
'   Started... Oct 18, 2007
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

HB25            PIN     15                      ' remove setup
PIR1            PIN     14                      ' SETUP = DN
PIR2            PIN     13                      ' SETUP = DN
Ghoul           PIN      0


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

IsOpen          CON     1
IsClosed        CON     0

Running         CON     1
Stopped         CON     0

Yes             CON     1
No              CON     0


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

flags           VAR     Byte
show1          VAR     flags.BIT0              ' show 1 status
show2          VAR     flags.BIT1              ' show 2 status

timer1          VAR     Byte                    ' for PIR debouncing
pntr1           VAR     Byte                    ' data pointer
runTimer1       VAR     Word                    ' show 1 step timer
speed           VAR     Word                    ' HB25 speed

timer2          VAR     Byte                    ' for PIR debouncing
pntr2           VAR     Byte                    ' data pointer
runTimer2       VAR     Word                    ' show 2 step timer

stpStat         VAR     Byte                    ' step status


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

Check_Motor:
  IF HB25 = 0 THEN Check_Motor                  ' allow HB25 to initialize
  LOW HB25                                      ' set for PULSOUT
  GOSUB Stop_Motor

Reset:
  OUTL = %00000000                              ' clear outputs
  DIRL = %00000001                              ' configure output pins

  GOSUB Stop_Motor
  PAUSE 30000                                   ' wait 30 seconds


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

Main:
  PAUSE 25                                      ' time-base for program
  timer1 = (timer1 + 25) * PIR1                 ' update PIR timers
  timer2 = (timer1 + 25) * PIR2

  IF (timer1 >= 225) THEN                       ' wait for valid signal
    IF show1 = Stopped THEN                     ' not running?
      show1 = Running                           ' then start it
      pntr1 = 0                                 ' and reset sequence pointer
    ENDIF
  ENDIF

  IF (timer2 >= 225) THEN
    IF show2 = Stopped THEN
      show2 = Running
      pntr2 = 0
    ENDIF
  ENDIF


' -----------------------------------
' Handle Show 1 events (HB25 control)
' -----------------------------------

Run_1:
  IF (show1 = Running) THEN
    IF (runTimer1 > 0) THEN
      runTimer1 = runTimer1 - 1
    ELSE
      READ Show1_Recs + pntr1, Word runTimer1, Word speed, stpStat
      IF runTimer1 > 0 THEN
        runTimer1 = runTimer1 / 25              ' match loop time-base
      ENDIF
      IF speed > 0 THEN
        PULSOUT HB25, speed
      ENDIF
      IF stpStat = $FF THEN                     ' end of sequence?
        show1 = Stopped
        timer1 = 0
      ELSE
        pntr1 = pntr1 + 5                       ' point to next record
      ENDIF
    ENDIF
  ENDIF


' ---------------------------------
' Handle Show 2 events (OUT0..OUT7)
' ---------------------------------

Run_2:
  IF (show2 = Running) THEN
    IF (runTimer2 > 0) THEN
      runTimer2 = runTimer2 - 1
    ELSE
      READ Show2_Recs + pntr2, Word runTimer2, OUTL, stpStat
      IF runTimer2 > 0 THEN
        runTimer2 = runTimer2 / 25              ' match loop time-base
      ENDIF
      IF stpStat = $FF THEN                     ' end of sequence?
        show2 = Stopped
        timer2 = 0
      ELSE
        pntr2 = pntr2 + 4                       ' point to next record
      ENDIF
    ENDIF
  ENDIF

  GOTO Main


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

Stop_Motor:
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                             ' center = stop
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, 750                             ' insurance command
  RETURN

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

Set_Motor_Speed:
  PAUSE 6                                       ' command hold-off
  PULSOUT HB25, speed                           ' set speed/direction
  RETURN


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

' Data for Show 1 is:
' -- pause time (in milliseconds)
' -- hb25 speed (2 uS units)
' -- step flag: $00 = run, $FF = last step

Show1_Recs      DATA    Word  3000, Word    0, $00      ' pause 3 secs
                DATA    Word  6000, Word 1000, $00      ' start HB25, 6 secs
                DATA    Word 15000, Word  750, $00      ' stop HB25, 15 secs
                DATA    Word 12000, Word  625, $00      ' start HB25, 12 secs
                DATA    Word 30000, Word  750, $00      ' stop HB25, 30 secs
                DATA    Word     0, Word    0, $FF      ' end of sequence


' Data for Show 2 is:
' -- pause time (in milliseconds)
' -- outputs for OUT0..OUT7
' -- step flag: $00 = run, $FF = done

Show2_Recs      DATA    Word 10000, %00000000, $00      ' 10 sec delay
                DATA    Word 20000, %00000001, $00      ' ghoul on for 20 s
                DATA    Word 30000, %00000000, $00      ' PIR reset
                DATA    Word     0, %00000000, $FF      ' end of sequence


Jon McPhalen
EFX-TEK Hollywood Office

MassageMstr1

Thanks John.  I Definately trust your coding!!!  What can I change if needed.  Can I easily change the times of how long the HB25 runs (how long the celing crawler crawls)  or how long the arms stay closed and the pauses withoout screwing anything up?  Those would be the only things I would need to change, if at all.

JonnyMac

The one thing I'm suspicious of, Dahniel, is the timing values used by the HB25.  In one comment you suggest reversing the motor, but both values are on the same side of 750.  I think you may have pulled some numbers from a Prop-1 program; please check these values carefully before running the code, and do make sure you can pull the plug on the motor while testing in case a value is wrong (or, you could write an independent test program and then plug in those numbers).
Jon McPhalen
EFX-TEK Hollywood Office

MassageMstr1

It was from a prop1.  I didn't realize the value was different.  I looked the few thing here on the HB25, but didn't see anything for a prop2.  Good catch Jon!  So what are the values for prop2?  Looking at the code I see that 750 is stop.  The prop should crawl full speed forward and then 1/2 speed in reverse.

JonnyMac

For PULSOUT you'll take the Prop-1 values and multiply them by 5.  I corrected the listing above.
Jon McPhalen
EFX-TEK Hollywood Office