Page 177 - The Unofficial Guide to Lego Mindstorms Robots
P. 177
166
Poll accepts a Source and a Number that, taken together, describe the value you want to retrieve. The key to
u nderstanding Poll is the Parameter Table in the Technical Reference Document. This table simply lists out the possible
values for Source and Number and how they are interpreted. For example, a call to Poll 9, 1 would return the value
of input 2. A call to Poll 0, 11 would return the value of the twelfth variable.
Us ing Constants
As you might have guessed, symbolic constants in y our Visual Basic code can make Source and Number values a lot
easier to read. The Technical Reference Document even includes a set of constant definitions, RCXDat.bas (also available
online). You can incorporate this file as part of your Visual Basic project, but the constant names are not very descriptive
(SENVAL and VAR, for example).
You can easily define your own c onstants in the (Declarations) section of your code module. For example, you might add
the following definitions (the line s beginning with a single quotation mark are comments):
' Sources
P ublic Co nst VARIABLE = 0
Public Const SENSOR_VALUE = 9
' Sensor names
Public Const SENSOR_1 = 0
Public Const SENSOR_2 = 1
Public Const SENSOR_3 = 2
U sing these constants, the above Poll functions could be re written like this:
With DummySpiritForm.Spirit1
' …
Result = .Poll(SENSOR_VALUE, SENSOR_2
Result = .Poll (VARIABLE, 11)
' …
End With
It 's a lot more re adable with the symbolic constants. If you ever have to look at the code at some later date, you'll really
appreciate knowing what' s going on.
Using If and While
The Parameter Table is likewise the key to understanding the If and While functions. In essence, If compares two Poll
values, each described by a Source and a Number. In the following example, the If statement tests to see if the value of
i nput 3 is less than the constant value 100:
If 9, 2, 1, 2, 100