November 23, 2024, 10:06:20 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 X PIR x RC4 x long delay question

Started by clinefx1, September 04, 2009, 11:42:23 PM

Previous topic - Next topic

clinefx1

Hey long time,

Could you have a look at this.  I got a program that determines movement then activates an rc4.  Then it goes into a long delay subroutine and resets it's timer if more movement is detected. It almost works.  I'm crashing in a subroutine battle I think. 

Thanks
Chris


' =========================================================================
'
'   File......
'   Purpose...
'   Author.... Chris Cline
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' ====================================================================
' -----[ I/O Definitions ]-------------------------------------------------
SYMBOL  Sio             = 7
SYMBOL  PIR             = PIN6                  ' SETUP = DN

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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  Baud            = OT2400
SYMBOL  Addr            = %11

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

SYMBOL  pirTimer        = B2
SYMBOL  Delaytimer      = B3

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

Reset:
SEROUT Sio, baud, ("!RC4", addr, "S", %0000)
PAUSE 500

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

Main:
  Delaytimer = 0

  HIGH 0
  pirTimer = pirTimer + PIR * PIR
  PAUSE 10
  IF pirTimer < 25 THEN Main
  LOW 0


SEROUT Sio, baud, ("!RC4", addr, "S", %1111)      'all on
PAUSE 500

Delaytimer = 10
  GOSUB Delay_Seconds

SEROUT Sio, baud, ("!RC4", addr, "S", %0000)       'all off a result of no movement
   PAUSE 500

GOTO main
  END

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

Delay_Seconds:
    GOSUB Check_movement
    IF delaytimer = 0 THEN Delay_Exit
    PAUSE 1000
    delaytimer = delaytimer - 1
    GOTO Delay_Seconds

Delay_Exit:
  RETURN

Check_Movement:
  pirTimer = pirTimer + PIR * PIR
  PAUSE 10
  IF pirTimer < 25 THEN movement_exit
  delaytimer = 10
  PAUSE 10
  RETURN

Movement_exit:
RETURN

JonnyMac

Nice to hear from you again, Chris.  Give this version a look -- I think it does what you want.

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


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

SYMBOL  Sio             = 7                     ' SETUP = UP, no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN


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

SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0

SYMBOL  Baud            = OT2400


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

SYMBOL  relays          = B0
SYMBOL   K1             =  BIT0
SYMBOL   K2             =  BIT1
SYMBOL   K3             =  BIT2
SYMBOL   K4             =  BIT3

SYMBOL  timer           = B2
SYMBOL  idx             = B3

SYMBOL  tix             = W5


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

Reset:
  relays = %0000
  GOSUB Set_Relays
  PAUSE 500


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

Main:
  GOSUB Check_Movement
  IF timer < 100 THEN Main

Lights_On:
  relays = %1111
  GOSUB Set_Relays
  PAUSE 500

Lights_Delay:
  tix = 100                                     ' set for 10s
  GOSUB Delay_Tix
  IF tix > 0 THEN Lights_Delay                  ' another 10s if movement

  GOTO Reset


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

Set_Relays:
  SEROUT Sio, Baud, ("!RC4", %11, "S", relays)
  RETURN

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

' Delay in 0.1 second units (pass in "tix")
' -- uses Check_Movement for delay

Delay_Tix:
  IF tix = 0 THEN Delay_Exit                    ' abort if tix expired
    GOSUB Check_Movement                        ' check PIR
    IF timer = 100 THEN Delay_Exit              ' abort on movement
      tix = tix - 1                             ' else update tix
      GOTO Delay_Tix

Delay_Exit:
  RETURN

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

' Scans PIR and creates 0.1-second delay

Check_Movement:
  timer = 0
  FOR idx = 1 TO 20
    timer = timer + 5 * PIR
    PAUSE 5
  NEXT
  RETURN
Jon McPhalen
EFX-TEK Hollywood Office

clinefx1

Thanks Jon-

Your code runs great on the hardware I have set up.  Boggles my mind how fast you can turn programs around.  Question: What would be the max delay I can use?


-Chris

JonnyMac

Well, I've got 15 years of BS1 programming practice, and nearly 30 years of BASIC under my belt.

The program is using the variable tix for delay units; as this is a word (W5) variable you can have values from 0 to 65535.  In the program you have timing units (created by Check_Movement) which means you can have a delay from 0.1 seconds to 6553.5 seconds.
Jon McPhalen
EFX-TEK Hollywood Office