Page 178 - The Unofficial Guide to Lego Mindstorms Robots
P. 178
167
The value of input 3 is represented by the first 9, 2; the constant value 100 is represented by 2 100. What's that 1 in the
m iddle? That's the operator that's used for comparison . The available operators are shown in Table 8-1. As you might have
guessed, you can define constants to make these easier to understand.
Ta ble 8-1. Operators for If and While
Number Meaning
0 >
1 <
2 =
3 !=(not equal)
A good set of constants makes the If statement a lot easier to read:
' Sources
Public Const VAR IABLE = 0
Public Const CO NSTANT = 2
Public Const SEN SOR_VALUE = 9
' Sensor names
Public Const SENSOR_1 = 0
Public Const SENSOR_2 = 1
Public Const SENSOR_3 = 2
' Operators
Public Const GREATER = 0
Public Const LESS = 1
Public Const EQUAL = 2
Public Const NOT_EQUAL = 3
Sub UsingConstants(
With DummySpiritForm.Spirit1
.InitComm
' …
.If SENSOR_VALUE , SENSOR_3, LESS, CO NSTANT, 100
' Do stuff he re.
.EndIf
' …
.CloseComm
End With
End Sub
Once you've mastered If, the While loop is easy. Like If, it compares two values using an operator. The body of the
While (until an EndWhile) is executed until the comparison is false. The body of the following loop, for example, would
be executed as long as the value of input 3 was less than 100:
While SENSOR_VALUE, SENSOR_3, LESS, CONSTANT, 100
' Loop body
EndWhile