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
Содержание BASIC Stamp 1
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...