November 22, 2024, 08:51:24 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.


loop before pir?

Started by tj, October 16, 2007, 04:37:53 PM

Previous topic - Next topic

tj

lets say i have leds on pins 0, 1, and 2 and i want them to cycle untill pir is tripped. would i use do/untill or for/next, idx?
im using the ( pause 10, timer = timer + 10 * pir, if pir is < 250 then main). also would it be in the initialization section, or main.
an example would be good
i'm getting the hang of the basics, lights on and off when i want, ect. now trying to add a little.

JonnyMac

You don't specify "how" the LEDs are to cycle, so I'm going to show you a little demo that causes the LEDs on P0 and P1 to alternate back and forth at a 2 Hz rate until the PIR is tripped.

' =========================================================================
'
'   File...... PIR_Flashy_Leds.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Led2            = PIN1
SYMBOL  Led1            = PIN0


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

SYMBOL  Baud            = OT2400

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


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

SYMBOL  ledTimer        = B2                    ' for LED flashing
SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear outputs
  DIRS = %00000111                              ' set output pins (P0..P2)

  PAUSE 20000                                   ' warm-up/inter-show delay

  PINS = %00000001                              ' start with Led1 on
  ledTimer = 0
  timer = 0                                     ' clear for PIR debounce


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10

Flash_Leds:
  ledTimer = ledTimer + 1
  IF ledTimer < 25 THEN Check_Trigger           ' 25 * 10 ms = 250
    PINS = PINS ^ %00000011                     ' flip LEDs
    ledTimer = 0

Check_Trigger:
  timer = timer + 10 * PIR                      ' advance/clear timer
  IF timer < 250 THEN Main                      ' wait for valid signal

Prop_Ctrl:
  PINS = %000000000                             ' clear leds

  ' other code here

  GOTO Reset


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


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


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

JonnyMac

October 16, 2007, 05:04:40 PM #2 Last Edit: October 16, 2007, 05:09:01 PM by JonnyMac
Was this what you meant?

' =========================================================================
'
'   File...... PIR_Cycle_Leds.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Led3            = PIN2
SYMBOL  Led2            = PIN1
SYMBOL  Led1            = PIN0


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

SYMBOL  Baud            = OT2400

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


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

SYMBOL  ledTimer        = B2                    ' for LED flashing
SYMBOL  ledPntr         = B3
SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear outputs
  DIRS = %00000111                              ' set output pins (P0..P2)

  PAUSE 20000                                   ' warm-up/inter-show delay

  PINS = %00000001                              ' start with Led1 on
  ledTimer = 0
  timer = 0                                     ' clear for PIR debounce


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10

Cycle_Leds:
  ledTimer = ledTimer + 1
  IF ledTimer < 25 THEN Check_Trigger           ' 25 * 10 ms = 250
    ledPntr = ledPntr + 1 // 3
    LOOKUP ledPntr, (%001, %010, %100), PINS
    ledTimer = 0

Check_Trigger:
  timer = timer + 10 * PIR                      ' advance/clear timer
  IF timer < 250 THEN Main                      ' wait for valid signal

Prop_Ctrl:
  PINS = %000000000                             ' clear leds

  ' other code here

  GOTO Reset


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


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


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

tj

October 16, 2007, 06:40:31 PM #3 Last Edit: October 16, 2007, 07:10:19 PM by tj
i think i understand. i don't know if theres any difference, but what i was doing with it is theres, two leds on each pin in a circular pattern


     2    3                 and it wil make a rotating pattern, not random  1 then 2 then 3.

1              1           
   
     3     2
         

This is what i tried and it loops ok, but doesn't set off pir, i assume because its only checking pir once every 1.5 sec (roughly) ?


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


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


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


' -----[ I/O Definitions ]-------------------------------------------------
  SYMBOL Pir = PIN6                  'Setup Dn

  SYMBOL Fog = PIN0

  SYMBOL Lights = PIN2

  SYMBOL Cyl = PIN1

  SYMBOL Spot1 = PIN3

  SYMBOL Spot2 = PIN4

  SYMBOL Spot3 = PIN5
' -----[ Constants ]-------------------------------------------------------

  SYMBOL IsOn      = 1

  SYMBOL IsOff     = 0

  SYMBOL IsUp      = 1

  SYMBOL IsDn     = 0



' -----[ Variables ]-------------------------------------------------------
   SYMBOL timer = B2                      'pir debounce




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

Reset:

   PINS = %00000000                      'clear everything

   DIRS = %00111111                      'configure pins

   PAUSE 15000                           'pir warm up

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

Main:

   Spot1 = IsOn

   PAUSE 350

   Spot1 = IsOFF

   PAUSE 50

   Spot2 = IsOn

   PAUSE 350

   Spot2 = IsOff

   PAUSE 50

   Spot3 = IsOn

   PAUSE 350

   Spot3 = IsOff

   PAUSE 10

   timer = timer + 10 * pir                'update pir timer

   IF pir < 250 THEN Main                  'wait for valid signal

   Fog = IsOn

   PAUSE 2000

   Lights = IsOn

   PAUSE 1500

   Fog = IsOff

   Cyl = IsUp

   PAUSE 3000

   Cyl = IsDn

   GOTO Main

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


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


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


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


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


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


' -----[ EEPROM Data ]-----------------------------------------------------

tj

i tried the second code you did and it works like i was thinking, if you have time maybe help me understand why you did it that way and why mine didn't work, thanks in advance. By the way i'm glad i went with this product, and not another, the help is nice. I really enjoy this thing so far even though i can only get it to do simple task right now.

JonnyMac

My program is at an intermediate level, so it might take you a while to fully understand it -- once you do, though, you can do lots of cool things with this strategy.

First... the overall loop the handles the lights and scans the sensor input has to take about 10 milliseconds (by design as this is used as a time base).  Your program doesn't work because you've got big PAUSE statements in the scan loop -- you could easily miss a valid sensor input in the middle of one of those PAUSE statements.  My program doesn't add any more PAUSE statements, it uses timers based on that 10 millisecond time base.  Here's the [added] LED processing:

Cycle_Leds:
  ledTimer = ledTimer + 1
  IF ledTimer < 25 THEN Check_Trigger           ' 25 * 10 ms = 250
    ledPntr = ledPntr + 1 // 3
    LOOKUP ledPntr, (%001, %010, %100), PINS
    ledTimer = 0


We start by adding one to the timer -- keeping in mind that one equates to one unit of 10 milliseconds.  If we want each LED to be on for 1/4 second (250 ms) then we need to count to 25 units.  If the ledTimer is less than 25 units we move on to checking the trigger input as before.  When we finally do reach 25 units then the program drops through to the next line where the current LED pointer is updated, and kept in the range 0..2 with the modulus operator ( // ).  I wrote about the modulus operator in the math tricks post.  Finally, we use LOOKUP to set the new state of the LEDs and reset the timer for the new LED state.  BTW, your program has a small off time between LEDs; that could be added, but it will cost another variable and some complication (see below)

This, in effect, is how Windows and other "multi-tasking" operating systems work: each gets a tiny piece of processor time to do its thing.


Here's my version of what you were attempting: on- and off-time for each LED.  This gives you a lot of flexibility in how you "cycle" the LEDs

' =========================================================================
'
'   File...... PIR_Cycle_Leds_v2.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Led3            = PIN2
SYMBOL  Led2            = PIN1
SYMBOL  Led1            = PIN0


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

SYMBOL  Baud            = OT2400

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


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

SYMBOL  ledTimer        = B2                    ' for LED flashing
SYMBOL  ledPntr         = B3
SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear outputs
  DIRS = %00000111                              ' set output pins (P0..P2)

  PAUSE 20000                                   ' warm-up/inter-show delay

  PINS = %00000001                              ' start with Led1 on
  ledPntr = 0                                   ' point to Led1
  ledTimer = 35                                 ' initialize Led1 timer

  timer = 0                                     ' clear for PIR debounce


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

Main:
  RANDOM lottery                                ' stir random value
  PAUSE 10

Cycle_Leds:
  ledTimer = ledTimer - 1
  IF ledTimer > 0 THEN Check_Trigger
    ledPntr = ledPntr + 1 // 6
    LOOKUP ledPntr, (%001, %000, %010, %000, %100, %000), PINS
    LOOKUP ledPntr, (35, 5, 35, 5, 35, 5), ledTimer

Check_Trigger:
  timer = timer + 10 * PIR                      ' advance/clear timer
  IF timer < 250 THEN Main                      ' wait for valid signal

Prop_Ctrl:
  PINS = %000000000                             ' clear leds

  ' other code here

  GOTO Reset


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


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


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

tj

i tried to look up Pntr, to see more about it in the bs1 help index. it doesn't show anything. also i see you used pin7, Sio, i thought that was for serial in/out for external stuff, ap-8, rc-4, ect which i haven't begain to try. what is it's purpose here?
i read some about the modulus, and i noticed on the code that used //3 cycled alot faster than //6 was it because of this or because
ledtimer was 35 instead of 0. this stuff is taking a little longer to understand, but i think if get one thing down at a time, i'll get it.
last thing for now you used baud 2400, what is its purpose here? i assume it has to do with Sio.

tj

i was noseing around in the prop 2 a bit is there anything like the "stampworks" literature for bs1, thats for bs2 correct. i look in the help section for bs1 and i don't always understand the examples. like i said before i somewhat understand how to do pauses, on, off and what the pir timer does, but know i'm trying to add to that and it's messing with a little.

JonnyMac

Not yet.... I've been delayed by other commitments, but there will be a book -- by me -- called "Prop-1 Programming: From Zero to Hero."  (Note: That's just an idea for the title, I don't know what it actually be)

Note: I'm locking this thread because you violated the guideline of changing topics mid-stream; it seams a small thing, I know, but the last question really should have been in its own thread.
Jon McPhalen
EFX-TEK Hollywood Office