November 23, 2024, 11:19:41 AM

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.


PIR CODE FOR PROP-1

Started by CraigN, September 28, 2009, 09:01:46 PM

Previous topic - Next topic

CraigN

John,

First of all thanks for the great product and support.

You wrote this code for me last week and it worked fine when I was using the Prop-1 with the prop trainer attached (which is great for learning).
Hooked up the devices, OUT0 to 12 vdc relay to 12 vdc wiper motor(separate power supply), 12 vdc light to OUT1, CARP 300 (power from prop-1) and V-trigger output from OUT5.
All works fine with prop trainer on board. 
I removed the trainer and installed the Parallax PIR on pins W/R/B 6 using the 3 wire cable provided as it shows in the docs. 
The jumper for P6 is in the down position. All still works fine except the program runs, resets and starts again, even with the PIR in a closed box to prevent accidental trigger.

Do I have to change/modify the ULN2803?

Note that I added a second PAUSE statement to extend the pause (if the value of PAUSE was > 130000 it was ignored? MAX VALUE???) to allow enough time for CARP to finish. This worked correctly with the trainer installed.

I have 2 PIR's so I swapped them out to rule out a faulty PIR.  Same problem, program runs, stops for ~30 seconds and restarts without apparent PIR activation.

I have searched through the forums and the code you provided looks correct.

Any suggestions would be appreciated.


code follows:


' =========================================================================
'   File......   Outhouse1.bs1
'   Purpose...  open door - turn on light - play audio - turn off light - close door.
'   Author.... JohnnyMac / Craig Newton
'   E-mail.... Craignewton@adelphia.et
'   Started...   9-26-09
'   Updated...   9-28-09  added 2 additional devices,  carp1 and doors1 for future - changed pneumatics to 12 vdc motor
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                   ' SETUP = DN
SYMBOL  CARP            = PIN5                   ' V+/OUT5 to V-Trig
SYMBOL  Doors1          = PIN2                  ' V+/OUT2
SYMBOL  CARP1           = PIN3                  ' V+/OUT3
SYMBOL  Light           = PIN1                    ' V+/OUT1
SYMBOL  Doors           = PIN0                   ' V+/OUT0


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

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

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400                ' B/R jumper removed


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

SYMBOL  timer           = B2


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00101111                              ' set output pins

  PAUSE 30000                                   ' let PIR warmp-up


' -----[ 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

  Doors = IsOpen
  PAUSE 1630                                    ' open door  - 12 vdc relay to wiper motor
  Doors = IsClosed

  Light = IsOn
  PAUSE 1000

Start_Audio:
  CARP = IsOn
  PAUSE 2000
  CARP = IsOff

  PAUSE 65000                                   ' adjust for audio + 5s (5000)

  PAUSE 85000                                   ' added to extend pause  - value > 130000 did not work - works ok with prop trainer installed.

  Light = IsOff
  PAUSE 2000                                       ' dramatic pause

  Doors = IsOpen
  PAUSE 1630                                    ' door close
  Doors = IsClosed


  PAUSE 500

Show_Delay:
  PAUSE 30000                                     ' 30s + 30s = 1min
  GOTO Reset


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


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


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

JonnyMac

Okay, there's more than one question mixed in here so let me see if I can pick them out.

1) I don't like PIRs -- they're always twitchy, especially when first powered on (as in the case when we're doing most of our testing).  That your code behaves fine with the trainer proves the code is fine.  Note that we only carry the Parallax sensor as a convenience for out customers.  I have always found that 100ms debounce works well, but maybe conditions with your prop require something longer.  You could bump the debounce period up to  250ms like this:

Main:
  timer = 0                                     ' reset timer

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



2) The maximum value that the Prop-1 or Prop-2 can deal with is 65535 ($FFFF), so your PAUSE 85000 is bogus.  In fact, due to the way numbers get truncated in microprocessors, PAUSE 85000 is evaluated as PAUSE 19464 which is significantly shorter.  65000 is easier to remember as a maximum number, so you can consider the longest possible pause -- using one statement -- is 65 seconds.

For really long delays you can put PAUSE in a loop.  For a 3-minute delay you could do this:

Pgm_Delay:
  FOR timer = 1 TO 3
    PAUSE 60000
  NEXT


Think of it this way: if you want to walk 30 feet you can't do it in one, 3-foot stride due to the limit of the length of you legs.  So what do you do?  You take several steps to cover the distance.  This is what I've done in the loop above using a convenient unit (1 minute) that falls within the maximum range for PAUSE.

Jon McPhalen
EFX-TEK Hollywood Office

CraigN

John,

Thanks for the information. 
I am still having a problem with the PIR.

I have tried increasing and decreasing the delay by .001 ms,  still no good. 

If I go above 256 ms the unit does not trigger, if I set it to 255 ms or less it keeps restarting the program. 

Any ideas?

Can the sensitivity be adjusted on the PIR using code?


So close I can almost taste it. :)


JonnyMac

Craighg,

No, the PIRs -- especially cheap ones that everyone craves -- are not adjustable and, as I have stated many times, very twitchy. 

I believe there is something going with your setup since you're getting the same behavior from two sensors.  When I am forced to test a PIR on my desk I put it into a coffee mug and turn the opening away from me and any other sources of IR (e.g., window with curtains open).    Remember, the PIR is a differential device; it settles and then looks for a change in the IR field in front of it.  Maybe you're being twitchy...  ;D ... and not giving the sensor enough time to settle down as you're testing.

BTW, the variable used for deboucing (timer) is a byte variable (B2) which means it can only hold values from 0 to 255 -- trying to use a value greater than this will confuse the program and give you odd behavior, just like trying to do a PAUSE with a value greater than 65535.

There's no h in my name, either!  ;)
Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Opps! Sorry about the "H"  Jon.  :-[

OK so the 255 is not what I thought it was. I am going to leave the program running and see if the PIR stabilizes. If not I guess it will b,  off the to internet to find another way to trigger the Prop-1..

I understand the pause problem better now also, thanks.

Working on the learning curve.
Craig (w/o the H)  ;D

CraigN

Jon,

Just a note that I hooked up the PIR to the CARP and it works perfectly.  When I hook it up to the Prop-1 it restarts the program and the end of the code.

Can the PIR trigger code be removed and have the prop-1 treat is as a NO contact?

Any last suggestions (from anyone)?

Grasping for a solution.

Thanks for your help.

Craig

JonnyMac

I'm at a loss; this is butt-simple program and this dicussion should have been over with after the second post.

Please post the code you're actually using (copy it right out of your editor without changing/editing anything) -- I'm of the belief that you'e modified  something without thinking it was important enough to tell me and that has created an unintended consequence.  Note, I'm not accusing you of being malicious, just not quite understanding a change you made to the code.

Or... just give me the IO setup and program requirements again (be specific and verbose) and I'll write you a fresh program.  But.... please try any code I post without changing it.  There is no magic to this.  Clearly the PIR works, clearly the Prop-1 works -- we just have to get the correct code into the controller.  And it's not like John and I haven't had any practice doing that...  ;D
Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Jon,

I agree it should be simple.

I don't know what's causing the issue but here's the code:

And no, I know you are not accusing, just trying to help. :)

Thanks.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN6                  ' SETUP = DN
SYMBOL  CARP            = PIN5                  ' V+/OUT5 to V-Trig
SYMBOL  Doors1          = PIN2                  ' V+/OUT2
SYMBOL  fogger          = PIN3                  ' V+/OUT3
SYMBOL  Light           = PIN1                  ' V+/OUT1
SYMBOL  Doors           = PIN0                  ' V+/OUT0


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

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

SYMBOL  IsOpen          = 1
SYMBOL  IsClosed        = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400                ' B/R jumper removed


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

SYMBOL  timer           = B2


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00101111                              ' set output pins

  PAUSE 30000                                    ' let PIR warmp-up


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

Main:
  timer = 0                                     ' reset timer

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

  fogger = IsOn
  PAUSE 3000
  fogger = IsOff

  Doors = IsOpen
  PAUSE 1630                                    ' let doors fully open
  Doors = isclosed

  PAUSE 1000

  Light = IsOn
  PAUSE 1000

Start_Audio:
  CARP = IsOn
  PAUSE 2000
  CARP = IsOff

  PAUSE 65000

'  PAUSE 45000                                   ' adjust for audio + 5s (5000)

  Light = IsOff
  PAUSE 2000                                     ' dramatic pause

  Doors = IsOpen
  PAUSE 1630                                   ' let doors fully open
  Doors = IsClosed

  PAUSE 2000

Show_Delay:
  PAUSE 30000                                     ' 30s + 30s = 1min
  GOTO Reset


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


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


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


JonnyMac

Something just crossed my mind: you do have the P6 SETUP jumper in the DN position, right?  If it's in the UP position then the controller will think the PIR is on all the time.
Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Jon,

Yes - P6 jumper is in the DN position (tried it UP/DN/OFF).

Thanks,
Craig

JonnyMac

You see, that's a bit of the *problem* -- just trying things without understanding the possible ramifications; kind of like checking to see if a gun is loaded by pulling the trigger.  ;D  The program code I provide clearly states that jumper should be in the DN position.  BTW, I'm assuming you're using a Parallax PIR that you bought from us or Parallax (possibly through RadioShack); if it's from another company the connections could be wrong. 

Here's my concern: You stated that the sensor works fine on the Cowlacious board; that's good news.  The bad new is, based on the Cowlacious docs, that the sensor connections are different than on the Prop-1.

Prop-1:  Signal, +5, Ground  (white, red, black)
CAR/P-300: Signal, Ground, +5 (see page 2 of Carl's docs)

Okay, let's do this.  You have some really wonky values in your version of the program, but it should work.  So... let's just look at what the PIR is doing.  Use this test program

' {$STAMP BS1}
' {$PBASIC 1.0}

Main:
  DEBUG CLS, PIN6
  PAUSE 100
  GOTO Main


With nothing in front of the sensor the DEBUG window should read

PIN6 = 0

The "0" will change to "1" when the sensor is active;  it might stay on a couple seconds after activated, but it should clear itself so long as you don't have movement in front of it.
Jon McPhalen
EFX-TEK Hollywood Office

CraigN

Jon,

Success! I rewired the PIR and it works.
I did not realize that the wiring was different. 

Sorry for all the trouble.

Thanks for your help!

Off to the Haunt!

Craig

ohh - the Wonky values (1630 ms) are for the 12vdc motor to open and close the door to the prop.  1630 ms = 180 degree motor rotation.
Original idea was to have a air cylinder open the door but I did not want to have to buy a larger air compressor until next year.

Happy Haunting!

JonnyMac

I rule!  ;D

Have fun, and a very successful Halloween season.
Jon McPhalen
EFX-TEK Hollywood Office