November 21, 2024, 03:36:12 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.


Connecting / Using a PIR

Started by JonnyMac, June 19, 2007, 09:35:11 AM

Previous topic - Next topic

JonnyMac

June 19, 2007, 09:35:11 AM Last Edit: June 19, 2007, 10:15:21 AM by JonnyMac
We carry the Parallax PIR in our catalog because it connects to the Prop-1 (or Prop-2 or Prop-SX) very easily and is quite simple to use.  That said, PIRs can be finicky and have spurious (very short, false event) outputs caused by undesired sources.  One solution is to shield the PIR as demonstrated by our friend, Scary Terry:

http://www.scary-terry.com/itw/pirsensor/pirsensor.htm

I also suggest "debouncing" the PIR input.  What this means is that the controller will not respond to the PIR input until it has gone active and stayed active for some specified time; this doesn't have to be a long time (in human terms), about 1/4 second is plenty.

Let's get connected first:



Note that the PIR is attached to P6 and that the P6 SETUP jumper is set to the DN position.  The SETUP jumper holds the pin (P6) low until the PIR drives it high (its active output state).  In the example above there are two valves connected; one to OUT0, the other to OUT1.

Below you'll find a program that debounces the PIR sensor and provides mechanisms for doing fixed and random delays.


' =========================================================================
'
'   File...... PIR_Demo.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  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Valve2          = PIN1
SYMBOL  Valve1          = PIN0


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0


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

SYMBOL  tix             = B2                    ' for timing sub(s)
SYMBOL  tixMin          = B3                    ' min for random time
SYMBOL  tixMax          = B4                    ' max for random time
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear everything
  DIRS = %00000011                              ' make valve pins outputs

  tix = 200                                     ' wait 20.0 secs
  GOSUB Run_Timer

  tix = 0                                       ' reset timer for PIR


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10
  tix = tix + PIR * PIR                         ' add 10 or clear
  IF tix < 25 THEN Main                         ' for 1/4 sec (25 x 10 ms)

  tixMin = 10
  tixMax = 50
  GOSUB Random_Timer                            ' random, 1.0 to 5.0 secs

  Valve1 = IsOn
  tix = 30                                      ' hold 3.0 secs
  GOSUB Run_Timer
  Valve1 = IsOff

  Valve2 = IsOn
  tix = 15                                      ' hold 1.5 secs
  GOSUB Run_Timer
  Valve2 = IsOff

  GOTO Reset                                    ' reset everything


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

' Pass tixMin and tixMax -- tixMax must be greater than tixMin
' -- code falls through to Run_Timer for actual timing execution

Random_Timer:
  RANDOM lottery                                ' re-stir random value
  tix = tixMax - tixMin + 1                     ' calculate span
  tix = lottery // tix + tixMin                 ' randomize tix

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

' Pass tix
' -- each tick = 1/10 second

Run_Timer:
  IF tix = 0 THEN Timer_Done
    PAUSE 100
    tix = tix - 1
    GOTO Run_Timer

Timer_Done:
  RETURN

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


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


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


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


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