November 21, 2024, 11:44:40 PM

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.


Program in loop

Started by Jadams, February 20, 2009, 12:00:42 PM

Previous topic - Next topic

Jadams

My program to turn on a relay keeps looping after the 3 second delay.  Can you help please?

Thanks

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


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


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


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

SYMBOL  PIR             = PIN6                  ' SETUP = DN


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

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


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

SYMBOL  timer           = B2
SYMBOL  lottery         = W5                    ' random value
SYMBOL  Relay           = 0

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

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

  PAUSE 20000                                        ' warm-up
  timer = 0                                               ' clear for PIR debounce


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

Main:
  RANDOM lottery                                     ' stir random value
  PAUSE 5
  timer = timer + 5 * PIR                          ' advance/clear timer
  IF timer < 250 THEN Main                      ' wait for valid signal

  PAUSE 3000                                          ' 3 second pre-delay
  HIGH Relay                                            ' Turn on camera
  PAUSE 30000                                        ' on for 30 seconds
  LOW Relay                                            ' Turn off
  PAUSE 1000                                          ' 1 second post-delay

  GOTO Main


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

Jim Adams

Jadams

Sorry, I stated the problem wrong.  After the 30 sec 'on' cycle, the relay turns off for 3 seconds, then the relay is back on for another 30 seconds and continues in this loop.
Jim Adams

BigRez

February 20, 2009, 12:53:11 PM #2 Last Edit: February 20, 2009, 01:14:12 PM by bigrez
Your timer variable doesn't get reset after the sequence. 

So, one way to fix this is to change the last line from GOTO MAIN to GOTO RESET, but doing that would add another 20 second delay (for PIR warmup.)  So, I've changed the source to change the trigger target and move the main target as follows:

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


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


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


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

SYMBOL  PIR             = PIN6                  ' SETUP = DN


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

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


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

SYMBOL  timer           = B2
SYMBOL  lottery         = W5                    ' random value
SYMBOL  Relay           = 0

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

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

  PAUSE 20000                                        ' warm-up

Main:
  timer = 0                                               ' clear for PIR debounce


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

Check_Trigger:
  RANDOM lottery                                     ' stir random value
  PAUSE 5
  timer = timer + 5 * PIR                          ' advance/clear timer
  IF timer < 250 THEN Check_trigger       ' wait for valid signal

  PAUSE 3000                                          ' 3 second pre-delay
  HIGH Relay                                            ' Turn on camera
  PAUSE 30000                                        ' on for 30 seconds
  LOW Relay                                            ' Turn off
  PAUSE 1000                                          ' 1 second post-delay

  GOTO Main


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

Jadams

Thanks for the advice, but sorry to say I still have the same issue.  I replaced the relay with a small lamp and do not have the problem.  The relay is a 12v with a 87ohm coil.  Could this be the problem?
Jim Adams

JonnyMac

I think you're getting immediate re-triggering because you never clear "timer" before jumping back to Main.  Change GOTO Main to GOTO Reset and that should cure the problem.
Jon McPhalen
EFX-TEK Hollywood Office

Jadams

I changed 'goto main' to 'goto reset'.  Same problem.
Jim Adams

Jadams

Time Out!!  It does work.  I had moved the 'warm up' from 20 seconds to 2.  This must be too short.  I moved it back to 20 and it works.  I'll have to experiment to see how low I can make it.  Thanks.
Jim Adams

JonnyMac

If you're using a PIR and there is movement in its field it will keep re-triggering; the 20-second hold-off gives you a minimum time between prop events when there is constant motion in the field.  Here's a version that will wait for the field to be clear for 10 seconds before attempting to re-trigger:

' {$STAMP BS1}


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


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


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

SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Relay           = PIN0                  ' use OUT0/V+


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

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


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

SYMBOL  timer           = W4
SYMBOL  lottery         = W5                    ' random value


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

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

  timer = 0

PIR_Clear:
  PAUSE 100                                     ' short delay
  timer = timer + 100                           ' update timer
  timer = 1 - PIR * timer                       ' clear timer if PIR active
  IF timer < 10000 THEN PIR_Clear


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

Main:
  timer = 0

Check_PIR:
  RANDOM lottery                                ' stir random value
  PAUSE 5
  timer = timer + 5 * PIR                       ' advance/clear timer
  IF timer < 200 THEN Check_PIR                 ' wait for valid signal

  PAUSE 3000                                    ' 3 second pre-delay
  Relay = IsOn                                  ' Turn on camera
  PAUSE 30000                                   ' on for 30 seconds
  Relay = IsOff                                 ' Turn off
  PAUSE 1000                                    ' 1 second post-delay

  GOTO Reset
Jon McPhalen
EFX-TEK Hollywood Office

Jadams

Thanks Jon, this code seems much more stable.  My first program would re-trigger without any movement.  I'm using this to turn on a remote wireless camera.  The next project is to start the VCR .  I'm planning to use some x10 gear unless someone has a better suggestion.  Maybe hack the remote and trigger with the prop 1 but that will be another thread.

Thanks for this forum.  It's a great resource for those of us who have to tinker.
Jim Adams