November 23, 2024, 04:52:03 AM

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.


program help/review

Started by nhphantom, October 29, 2008, 07:21:21 PM

Previous topic - Next topic

nhphantom

Hi, I was wondering if you could please look at this program and see if there is any way to make it better or any improvements that could be made.  The program works as it is, the only problem I have with it is that once it detects movement there is a delay of about 6 seconds roughly this may be a little long for the size of the area I am working in.   If someone could show me how to solve this or shorten the time.  The first part of the program, it is the check trigger part, is copied from somewhere else on this forum I believe. I think this is where the delay is. Any help with this would be greatly appreciated, like I said the program does work as is but I would like to make it a little better.  This is the first time I have done any programming with the prop one or basic stamp one.  Thanks for your help.

David Warren


' =========================================================================
'
'   File......Grave_jumper
'   Purpose...
'   Author....
'   E-mail....
'   Started...Aug.26,2008
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
  'Wait for motion sensor to detect child then turn on fog machine for 2 seconds
  'then air selenoind and flood light come on and hold for x number of seconds then
  'everything shuts off and remains off for x number of seconds, then everything resets


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN


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

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0
SYMBOL  Addr            =%00
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:
  DEBUG timer
  PAUSE 5
  DEBUG CLS                                      ' loop pad
  timer = timer + 5 * Trigger                    ' update timer
  IF timer < 50 THEN Check_Trigger               ' wait for 0.2 sec input
  DEBUG timer
  PAUSE 5
  DEBUG CLS

  ' prop code here
SEROUT Sio,Baud,("!RC4",Addr,"R",4,IsOn)         'fog mach. on
PAUSE 2000
SEROUT Sio,Baud,("!RC4",Addr,"R",1,IsOn)         'light on
SEROUT Sio,Baud,("!RC4",Addr,"R",2,IsOn)         'air solenid on
PAUSE 5000
SEROUT Sio,Baud,("!RC4",Addr,"X")                'all relays off
DEBUG CLS
PAUSE 10000

  GOTO Reset


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


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


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


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


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


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


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




JonnyMac

If your program works, congrats!  There are of course other styles; I have mine and have developed the way I code for BASIC Stamp processors after 14 years of doing it (I was one of the first Basic Stamp 1 customers).

Suggestion: Dump DEBUG.  This is useful for training or when you're having a problem, but it should not appear in an operational program -- especially since you're ultimately going to remove the PC and DEBUG becomes useless.  Here's an inside tip: DEBUG sends about 100 bytes to the PC at 4800 baud; this process takes 200 milliseconds so every DEBUG statement adds a ton of time, especially the two you have in your trigger loop.

Here's how I'd code your program -- I find this style is easier to understand, maintain, and it saves code space by reducing the number of SEROUT statements in the listing.

' =========================================================================
'
'   File...... Grave_Jumper.BS1
'   Purpose...
'   Author....
'   E-mail....
'   Started... 26 AUG 2008
'   Updated... 29 OCT 2008
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Wait for motion sensor to detect child then turn on fog machine for 2
' seconds then air selenoind and flood light come on and hold for x number
' of seconds then everything shuts off and remains off for x number of
' seconds, then everything resets


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400                ' B/R jumper removed


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

SYMBOL  relays          = B0
SYMBOL   Light          =  BIT0
SYMBOL   Solenoid       =  BIT1
SYMBOL   K3             =  BIT2
SYMBOL   Fogger         =  BIT3

SYMBOL  timer           = B2


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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000000                              ' set outputs

  SEROUT Sio, Baud, ("!!!!!RC4", %00, "X")
  relays = %0000

  PAUSE 10000                                   ' warm-up/delay


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

Main:
  timer = 0                                     ' reset timer

Check_Trigger:
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * Trigger                   ' update timer
  IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input

  Fogger = IsOn
  GOSUB Set_RC4
  PAUSE 2000

  Light = IsOn
  Solenoid = IsOn
  GOSUB Set_RC4
  PAUSE 5000

  GOTO Reset


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

Set_RC4:
  SEROUT Sio, Baud, ("!RC4", %00, "S", relays)
  RETURN

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


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