November 27, 2024, 03:07:11 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.


Prop1 PIR with Valve.....

Started by michilson, September 10, 2009, 09:45:07 PM

Previous topic - Next topic

michilson

Okay alittle background before the code to help me..

I have a Prop 1 and a Pir on Pin 7

I have a LED on Pin6 that i'd like to blink when the system is scanning the PIR  and be soild when it's running the program...

The Valve is one Pin0

here's the code i have come up with i think im still off alittle tho

When Pir is triggered i want it to randomize between 1 - 3 sec

hold the valve on for a 2 sec then turn off
and have a 5 sec reset time

then turn everything off and go back to flashing the LED!

Here's what i have let me know where i went wrong

Thanks for the help!




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


' =========================================================================


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


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

' -----[ Constants ]-------------------------------------------------------
SYMBOL  PIR = PIN7
SYMBOL SAT = PIN6
SYMBOL  Valve1 = 1
SYMBOL  No = 0
SYMBOL  YES = 1
SYMBOL  timer = W1
SYMBOL  delay = W2
SYMBOL tix1 = B2






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

Main:

IF Pir = No THEN Flash ' wait for "victim"
GOSUB Group1



' -----[ Subroutines ]-----------------------------------------------------
Flash:
HIGH Sat
PAUSE 200
LOW Sat
PAUSE 200



Group1:
HIGH SAT
RANDOM timer ' stir random generator
delay = timer // 5 + 1 ' create delay, 1 to 5 seconds
delay = delay * 1000
PAUSE delay ' hold for random delay
HIGH Valve1 ' open solenoid to lift prop
PAUSE 500 ' hold for 5 seconds
LOW Valve1 ' retract prop
LOW SAT
RETURN ' back

Run_Timer:
  IF tix1 = 0 THEN Timer_Done                    ' check for end of timer
    PAUSE 100                                   ' hold for 1 tic
    tix1 = tix1 - 1                               ' update tix count
    GOTO Run_Timer                              ' re-check for end of timer
  Timer_Done:
  RETURN




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

JonnyMac

Instead of trying to explain what you did wrong, let me show you how I'd do it right.  Please use PIN6 as your trigger (PIR) input and PIN5 as your status output as this allows you to test your program (and most others, using the Prop-1 Trainer).

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


' -----[ Program Description ]---------------------------------------------
'
' Sio     :: Serial IO to EFX-TEK accessories (RC-4, FC-4, etc.)
'            -- clip pin 1 of ULN2803 or replace with ULN2003
'
' 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  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  StatLed         = PIN5                  ' V+/OUT5
SYMBOL  Valve           = PIN0                  ' V+/OUT0


' -----[ 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  idx             = B2
SYMBOL  timer           = B3

SYMBOL  delay           = W4
SYMBOL  lottery         = W5


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

Reset:
 PINS = %00000000                              ' preset IOs
 DIRS = %00100001                              ' define output pins (1s)


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

Main:
 timer = 0                                     ' reset timer

Check_Trigger:
 FOR idx = 1 TO 20
   PAUSE 5
   timer = timer + 5 * PIR                     ' update activity timer
   RANDOM lottery                              ' stir random #
 NEXT
 StatLed = IsOn - StatLed                      ' toggle LED state
 IF timer < 100 THEN Check_Trigger             ' valid signal?

 StatLed = IsOn                                ' prop active

 delay = lottery // 4001 + 1000                ' delay 1 to 5 seconds
 PAUSE delay

 Valve = IsOn
 PAUSE 2000
 Valve = IsOff
 StatLed = IsOff

 PAUSE 5000
 GOTO Main


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


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


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

michilson

Are the:
SYMBOL  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  Baud            = OT2400                ' B/R jumper removed

Needed?

I dont have fc4 or any other units connected to the prop1?

My thinking are they are in your templete file and didn't get deleted but i might be wrong.

JonnyMac

True, and they don't do anything to harm the listing so leave them alone (and the Sio definition keeps you from using PIN7 as an input to maintain P1-T compatibility).  If you decide to "upgrade" your prop later, which we all do from time-to-time, then you're set and don't have scramble.

Focus on learning the stuff in the body of the code.  The program I wrote for you is a little subtle and and you can learn a couple neat tricks from it.
Jon McPhalen
EFX-TEK Hollywood Office

michilson

Okay thanks

Just making sure i do understand what i'm kind of looking at.

Thanks for the help.

one last question can you give me a little more detail on what this does?

FOR idx = 1 TO 20

Not sure if i understand what idx does!

Thanks for the help

JonnyMac

I'll give you a hint this time, but your "job" is to look up these things in the PBASIC help file so they really sink in.

The variable idx is used to count from 1 to 20 using a FOR-NEXT loop.  Within this loop is a 5ms PAUSE so the loop takes 100ms (0.1s, 5 x 0.02s) to run.  Inside that loop the PIR is being scanned; when it's active the variable called timer is incremented by 5 (the delay time); when the PIR is not active the value of timer is reset to 0 (cool little in-line math trick).

At the end of this loop the StatLed output pin is toggled -- but we don't use TOGGLE in this case because of the PIN5 definition (which won't work with TOGGLE).  Another neat trick: Since the LED can only be on (1) or off (0), subtracting its current value from IsOn (1) will invert the state (1 - 0 = 1; 1 - 1 = 0).  As long as there is no sensor input the Check_Trigger loop will keep running and this is what creates the StatLed flash.

If the PIR goes active and stays active for more than 100ms we consider that a good signal and the code drops through to the operational section, otherwise it loops back to Check_Trigger.  The PIR *could* come on in the middle of the loop so at the end the value of timer might only be 50; this is not considered a fully-qualified signal so the debounce loop would run one more time.

I think the rest is self explanatory.


Note To Everyone: We're in the thick of the Halloween build season and while we can write programs, we cannot take the time to explain each an every one of them -- please... the online help file will in fact help.  We also have documents attached to the Prop-1 and Prop-2 pages (see Downloads section of each) that give a brief overview of the commands typically used in prop control programs.
Jon McPhalen
EFX-TEK Hollywood Office

michilson

Thanks John that is vary helpful break down of what is going on.

I have looked at some of those help files but thier still alittle lacking in walk threw examples like this.

I am a very visual step by step learner and expanding those help files in the slow time will greatly help with this.

Just as a side note:

I'd love to see maybe a more interactive section that you can pick the componets you have and see a list of others users programs that have requested your help in programing might be a great way for more people to disscuss program code and learn what possibilities there units have..

Right now it is vary hard to search threw the forum for other programing ideas without spending hours looking at each post.


JonnyMac

A big part of the *problem* is what you've just done -- change topics in the middle of a thread. 

As topic changing is a violation of our guidelines, I'm locking the thread.  Oi....
Jon McPhalen
EFX-TEK Hollywood Office