Shop OBEX P1 Docs P2 Docs Learn Events
if- then statement format — Parallax Forums

if- then statement format

ArchiverArchiver Posts: 46,084
edited 2001-01-07 18:26 in General Discussion
On 7 Jan 01 at 12:23, La Quida Brown wrote:

>
> can i write a statement for the BS2 like:
>
> IF motion = 0 AND glass = 1 AND noise = 1 AND door =1
> then ...

Yes. Lets also assume these represent states of external sensors,
and that the outputs of those sensors are applied to your Stamp's I/O
pins. With some thoughtful design up front, you can greatly
minimize your programming chores.

Lets say the output of the motion sensor is applied to I/O 0, the
glass sensor to I/O 1, the noise sensor to I/O 2 and the door sensor
to I/O 3. Lets also assume all sensor outputs are active high (or
have been inverted to make them active high).

Now, because I/O's 0 through 3 can be collectively addressed as INA,
you can use a simple statement like:

waitForAlarm:
IF INA = 0 THEN waitForAlarm

This checks all sensors simultaneously and very rapidly so that a
momentary activation is not likely to be missed. If any of the
sensors are active INA will be non-zero and, as Jon pointed out, you
could then use the BRANCH statement based on INA or some other
technique to determine which sensor(s) is/are active and do the
right thing.

You could even skip the IF statement and simply do:

waitForAlarm:
BRANCH INA,[noparse][[/noparse]waitForAlarm, motion, glass, motion_glass,...

Bottom Line: your Stamp's resources are limited. Make the most of
them by taking advantage of your Stamp's built-in efficiencies.

> can i have serval like that ?

Yes, but see Bottom Line, above...


Steve

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-01-07 17:23
    can i write a statement for the BS2 like:

    IF motion = 0 AND glass = 1 AND noise = 1 AND door =1
    then ...

    can i have serval like that ?

    - LaQuida
  • ArchiverArchiver Posts: 46,084
    edited 2001-01-07 18:26
    [font=arial,helvetica]In a message dated 1/7/01 11:25:24 AM Central Standard Time,
    sunset97@SEAS.GWU.EDU writes:


    can i write a statement for the BS2 like:

    IF motion = 0 AND glass = 1 AND noise = 1 AND door =1
    ·then ...

    can i have serval like that ?



    Yes, but PBASIC is more efficient. ·You could, for example, do something like
    this:

    ' code start ----

    alarms ··VAR ····Nib
    motion ··VAR ····alarms.Bit0
    glass ····VAR ····alarms.Bit1
    noise ····VAR ····alarms.Bit2
    door ·····VAR ·····alarms.Bit3

    CheckOut:
    ·alarms = InA

    ·BRANCH alarms, [noparse][[/noparse]CheckOut, GlassAlarm, ............

    ' code end ----

    BRANCH is very powerful and can save you from writing a lot of complicated
    IF-THEN statements. ·Be sure to download the latest manual from Parallax and
    read up on BRANCH.

    -- Jon Williams
    -- Dallas, TX

    [/font]
Sign In or Register to comment.