BASIC stamp manual v2.2
5: BASIC Stamp Command Reference – IF...THEN
BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com • Page 233
GOTO) to the label called Main. This is the simplest form of IF…THEN
and is the only form supported by PBASIC 1.0 and PBASIC 2.0.
The Condition(s) argument is very powerful and flexible. In the next few
pages we’ll demonstrate this flexibility in great detail and afterwards we’ll
discuss the additional, optional arguments allowed by PBASIC 2.5.
Here's a complete example of IF...THEN in action:
value VAR Word
Main:
PULSIN 0, 1, value
DEBUG DEC value, cr
IF value < 4000 THEN Main
DEBUG "Pulse value was greater than 3999!"
Here, the BASIC Stamp will look for and measure a pulse on I/O pin 0,
then compare the result, value, against 4000. If value is less than (<) 4000, it
will jump back to Main. Each time through the loop, it displays the
measured value and once it is greater than or equal to 4000, it displays,
"Value was greater than 3999!"
On all BS2 models, the values can be expressions as well. This leads to
very flexible and sophisticated comparisons. The IF…THEN statement
below is functionally the same as the one in the program above:
IF value < (45 * 100 – (25 * 20)) THEN Val_Low
Here the BASIC Stamp evaluates the expression: 45 * 100 = 4500, 25 * 20 =
500, and 4500 – 500 = 4000. Then the BASIC Stamp performs the
comparison: is value < 4000? Another example that is functionally the
same:
IF (value / 100) < 40 THEN Val_Low
It's important to realize that all comparisons are performed using
unsigned, 16-bit math. This can lead to strange results if you mix signed
and unsigned numbers in IF...THEN conditions. Watch what happens
here when we include a signed number (–99):
WATCH OUT FOR UNSIGNED MATH
COMPARISON ERRORS
All
2
All
2
All
2
ALL ABOUT CONDITION(S).
NOTE: For BS1's, change line 1 to
SYMBOL value = W0
and line 4 to
DEBUG #value, CR