IF…THEN - BASIC Stamp Command Reference
Page 150
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
constants, or expressions. The following example is an IF…THEN
command with a simple condition:
IF 10 < 200 THEN Loop
This code will compare the number 10 to the number 200. If 10 is less than
200, the condition is true. In this case, 10 is less than 200 (and always will
be), so the program will jump (or GOTO) the label called Loop. Of course,
this is a silly example (10 is always less than 200 so this line will always
cause a jump to Loop). Most of the time, you'll use at least one variable in
your condition:
Value VAR WORD
Loop:
PULSIN 0, Value
DEBUG DEC Value, CR
IF Value < 4000 THEN Loop
DEBUG "Value was greater than 4000!"
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 Loop. 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 4000!"
On the BS2, BS2e, BS2sx and BS2p, 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 Loop
Here the BASIC Stamp evaluates the expression: 45 * 100 = 4500, 25 * 20 =
500, and 4500 – 500 = 4000. Then the BAISC Stamp performs the
comparison: is Value < 4000? Another example that is functionally the
same:
IF Value / 100 < 40 THEN Loop
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):
NOTE: For BS1's, change line 1 to
SYMBOL Value = W0
and line 4 to
DEBUG #Value, CR
1
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
A
SIMPLE FORM OF
IF…THEN
W
ATCH OUT FOR UNSIGNED MATH
COMPARISON ERRORS
2
e
2
sx
2
p
2
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...