November 23, 2024, 04:39:51 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Trigger relay with PIR and door entry

Started by Jadams, January 05, 2009, 07:11:51 AM

Previous topic - Next topic

Jadams

I am trying to trigger a relay when the door opens using a Prop 1.  I have a NO contact on the door and a PIR in the room.  I want to trigger the relay when the door opens AND the PIR sees movement.  The relay needs to go off after 20 seconds even if the door is still open and movement is still present.  Once the door closes there will be still be movement in the room.  The relay should not trigger when I leave the room, only when I come in.

Thanks for your help. 
Jim Adams

JonnyMac

So does the N.O. switch on the door close when the door is closed?  I'm writing a program on that assumption.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Here's what I've come up with.  I think I have your logic... if a person is detected in the room and the door has been left open for 20 seconds then the relays is activated until the door closes.  Right?

This program is a tad advanced vis-a-vis what we normally do here, but this is what a professional would so so it's good to learn from.  There are subroutines setup to validate the Door and PIR inputs; these routines "debounce" the inputs to make sure that they are in fact active.  By using subroutines we reduce the amount of program space used.

Here's the program:

' =========================================================================
'
'   File...... Entry_Alarm.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 05 JAN 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  PIR             = PIN7                  ' SETUP = DN
SYMBOL  Door            = PIN6                  ' SETUP = DN (P6.W/P6.R)
SYMBOL  Relay           = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Closed          = 1
SYMBOL  Open            = 0


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

SYMBOL  flags           = B0
SYMBOL   doorFlag       =  BIT0
SYMBOL   pirFlag        =  BIT1

SYMBOL  idx             = B2


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

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


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

Main:
  GOSUB Check_Door
  IF doorFlag = Closed THEN Main

  GOSUB Check_PIR
  IF pirFlag = No THEN Main

  PAUSE 20000                                   ' entry delay

  GOSUB Check_Door
  IF doorFlag = Closed THEN Main

  Relay = IsOn
  PAUSE 1000                                    ' minimum relay time

Wait_For_Door:
  GOSUB Check_Door
  IF doorFlag = Open THEN Wait_For_Door
    Relay = IsOff                               ' off when door closes
    GOTO Main


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

Check_Door:
  doorFlag = Closed                             ' assume closed
  FOR idx = 1 TO 10
    doorFlag = doorFlag * Door
    PAUSE 10
  NEXT
  DEBUG doorFlag
  RETURN

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

Check_PIR:
  pirFlag = Yes                                 ' assume active
  FOR idx = 1 TO 10
    pirFlag = pirFlag * PIR
    PAUSE 10
  NEXT
  RETURN

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


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

Jadams

Jon,

I may have misled you on the assumptions, but this program will work perfectly for another project I'm working on.

I know this is strange logic, but I'm trying to trigger a prop for 20 seconds when someone enters the room and stays off while they are there.  Then,  re-set when they leave the room.
Current assumptions:

When the door is closed the switch is open - door open, switch closed
When I come into the room, door opens (switch closes), pir detects movement, relay closes for 20 seconds, then opens.
The relay stays open while I'm in the room and when I exit, (open and close door).
The relay only closes when I enter the room.



Thanks again, sorry for the confusion.
Jim Adams

JonnyMac

Okay, give this version a try -- but do not take my word for it; please check the logic very carefully.  I consolidated the sensors scanning into one routine and have tried to deal with false entries and exits.

' =========================================================================
'
'   File...... Prop_Entry.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 05 JAN 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  PIR             = PIN7                  ' SETUP = DN
SYMBOL  Door            = PIN6                  ' SETUP = DN (P6.W/P6.R)
SYMBOL  Relay           = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Open            = 1
SYMBOL  Closed          = 0


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

SYMBOL  flags           = B0
SYMBOL   doorFlag       =  BIT0
SYMBOL   pirFlag        =  BIT1

SYMBOL  idx             = B2


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

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


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

Main:
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Main                ' wait for door to open

Wait_For_Victim:
  PAUSE 500                                     ' let user enter
  GOSUB Check_Sensors
  IF pirFlag = Yes THEN Run_Prop                ' victim has entered
  IF doorFlag = Closed THEN Main                ' false entry
    GOTO Wait_For_Victim

Run_Prop:
  Relay = IsOn
  PAUSE 20000
  Relay = IsOff

Wait_For_ReOpen:
  PAUSE 500
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Wait_For_ReOpen

Wait_For_Close:
  PAUSE 500
  GOSUB Check_Sensors
  IF doorFlag = Open THEN Wait_For_Close
  IF pirFlag = Yes THEN Wait_For_ReOpen
    GOTO Main


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

Check_Sensors:
  doorFlag = Open                               ' assume open
  pirFlag = Yes
  FOR idx = 1 TO 10
    doorFlag = doorFlag & Door
    pirFlag = pirFlag & PIR
    PAUSE 10
  NEXT
  RETURN

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


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

JonnyMac

Here's a problem.... if the "victim" leaves before the relay timing is done then the program sticks.  Let me think about how to solve that -- you should think about it, too (i.e., don't wait on me for all the answers).
Jon McPhalen
EFX-TEK Hollywood Office

livinlowe

January 05, 2009, 11:29:51 AM #6 Last Edit: January 05, 2009, 11:44:20 AM by livinlowe
Jon-
Just trying to intellectially solve this programming problem (as I'm at work at can't verify on hardware) what if instead of a long pause of 20 seconds you put it in a For- Next loop that pauses 2 seconds 10 times but also checks to see if the door is opened in the loop. If it is kill the prop and drop out.

Such As:

' =========================================================================
'
'   File...... Prop_Entry.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 05 JAN 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  PIR             = PIN7                  ' SETUP = DN
SYMBOL  Door            = PIN6                  ' SETUP = DN (P6.W/P6.R)
SYMBOL  Relay           = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Open            = 1
SYMBOL  Closed          = 0


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

SYMBOL  flags           = B0
SYMBOL   doorFlag       =  BIT0
SYMBOL   pirFlag        =  BIT1

SYMBOL  idx             = B2
SYMBOL  idy = B3

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

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


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

Main:
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Main                ' wait for door to open

Wait_For_Victim:
  PAUSE 500                                     ' let user enter
  GOSUB Check_Sensors
  IF pirFlag = Yes THEN Run_Prop                ' victim has entered
  IF doorFlag = Closed THEN Main                ' false entry
    GOTO Wait_For_Victim

Run_Prop:
  Relay = IsOn
  For idy = 0 to 10
   PAUSE 2000
   GOSUB Check_Sensors
   IF doorFlag = Open THEN Kill_Prop
  NEXT
  Relay = IsOff

Wait_For_ReOpen:
  PAUSE 500
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Wait_For_ReOpen

Wait_For_Close:
  PAUSE 500
  GOSUB Check_Sensors
  IF doorFlag = Open THEN Wait_For_Close
  IF pirFlag = Yes THEN Wait_For_ReOpen
    GOTO Main

Kill_Prop:
  Relay = IsOff
    GOTO Main

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

Check_Sensors:
  doorFlag = Open                               ' assume open
  pirFlag = Yes
  FOR idx = 1 TO 10
    doorFlag = doorFlag & Door
    pirFlag = pirFlag & PIR
    PAUSE 10
  NEXT
  RETURN

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


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

And, once again, forgive me if it doesn't compile.
Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

I didn't check it but I agree, philosophically.  I think, though, for really bullet-proof operation the program will have to be designed as a state-machine.  I like programming that way but those programs are not easy to follow for newcomers.
Jon McPhalen
EFX-TEK Hollywood Office

Jadams

I appreciate the time your putting into this.  I'm beginning to follow the logic but am no where near able to troubleshoot this.  With the door open or closed the relay closes for 2 seconds with PIR detection then goes to reset.

Jim Adams

Jadams

After more testing, this will work.  The only issue is if someone stays in the room the PIR will continue to re-trigger the prop.  If everyone leaves the room, it's perfect.

Thanks
Jim Adams

livinlowe

Jim-
It shouldn't, I think if someone opens the door it will go to main. Then it is only checking to see if the door opens before checking the PIR sensor. I will try to check this on my PDB when I get home.
Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

Here's an update that should solve the lingering guest problem.  At the top of the program we check to make sure that the door is closed and that there is no movement in the room for a full five seconds.  When we're sure the room is clear we wait for the door to open, then for the PIR which triggers the prop.

After the relay opens we wait for the door to open again before jumping back to the top where, again, we make sure that the room is clear and the door is closed for five seconds before moving forward.

Where does the 5 seconds come from?  The idx1 loop runs 50 times which calls Check_Sensors where idx2 runs 10 times with a delay of 10 milliseconds internally; 50 x 10 x 10ms = 5000ms = 5 seconds.

' =========================================================================
'
'   File...... Prop_Entry.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 05 JAN 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  PIR             = PIN7                  ' SETUP = DN
SYMBOL  Door            = PIN6                  ' SETUP = DN (P6.W/P6.R)
SYMBOL  Relay           = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Open            = 1
SYMBOL  Closed          = 0


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

SYMBOL  flags           = B0
SYMBOL   doorFlag       =  BIT0
SYMBOL   pirFlag        =  BIT1

SYMBOL  idx1            = B2                    ' loop controllers
SYMBOL  idx2            = B3


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

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


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

Main:
  FOR idx1 = 1 TO 50                            ' must be clear for 5 secs
    GOSUB Check_Sensors
    IF doorFlag = Open THEN Main                ' abort on any activity
    IF pirFlag = Yes THEN Main
  NEXT

Wait_Entry:
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Wait_Entry          ' wait for door to open

Wait_For_Victim:
  PAUSE 400                                     ' let user enter
  GOSUB Check_Sensors
  IF pirFlag = Yes THEN Run_Prop                ' victim has entered
  IF doorFlag = Closed THEN Wait_Entry          ' false entry
    GOTO Wait_For_Victim

Run_Prop:
  Relay = IsOn
  PAUSE 20000
  Relay = IsOff

Wait_For_ReOpen:
  PAUSE 400
  GOSUB Check_Sensors
  IF doorFlag = Closed THEN Wait_For_ReOpen
    GOTO Main


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

Check_Sensors:
  doorFlag = Open                               ' assume open
  pirFlag = Yes
  FOR idx2 = 1 TO 10
    doorFlag = doorFlag & Door
    pirFlag = pirFlag & PIR
    PAUSE 10
  NEXT
  RETURN

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


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

Jadams

Jon,

You truly are a wizard.  This works much better than I had planned.  I'll do my best to learn this logic.  Thanks again for your time today, I learned a lot.  This project will forever be named 'the lingering guest'.
Jim Adams