November 22, 2024, 06:50:02 PM

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.


PIR code

Started by crpalmer, September 23, 2008, 03:14:12 PM

Previous topic - Next topic

crpalmer

I see this blob of code all over the place:


Main:
  delay = 0

Check_Trigger:
  PAUSE 5
  delay = delay + 5 * Trigger
  IF delay < 100 THEN Check_Trigger


which I assume is to avoid spurious trigger of the prop by the PIR.  But doesn't that just delay the problem (it now goes off every 20 spurious triggers instead of every 1?).  Why isn't it something more like:


Main:
  delay = 0

Check_Trigger:
  PAUSE 5
  IF trigger = 0 THEN Main   ' reset delay
  delay = delay + 5
  IF delay < 100 THEN Check_Trigger


Cheers,
Chris.

JonnyMac

The magic is in this line:

  delay = delay + 5 * Trigger

... where Trigger is an input pin that when read will be one (PIR active) or zero (PIR off).  What may not be clear is that PBASIC does not use operator precedence like other computer languages, it evaluates expressions left-to-right.  So the first part of that line adds five to the variable called delay, the final part multiplies delay by one or zero.  So if the PIR is off the value of delay will be cleared (anything multiplied by zero is zero).
Jon McPhalen
EFX-TEK Hollywood Office

crpalmer

Thanks, the lack of operator precedence is a little hard to wrap you brain around...

JonnyMac

PBASIC2, the language used in the Prop-2, also evaluates left-to-right but does allow parenthesis in expressions to help out. In the Prop-2 I would normally code the debounce routine like this:

Main:
  timer = 0
  DO WHILE (timer < 100)
    PAUSE 5
    timer = (timer + 5) * Trigger
  LOOP


Note how the parenthesis in the expression, while not required, does remove ambiguity for experienced programmers.
Jon McPhalen
EFX-TEK Hollywood Office