if- then statement format
Archiver
Posts: 46,084
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
>
> 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
IF motion = 0 AND glass = 1 AND noise = 1 AND door =1
then ...
can i have serval like that ?
- LaQuida
sunset97@SEAS.GWU.EDU writes:
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]