
.While 0, 0, 2, 0, 0
.SetFwd "02"
.Wait 2, 50
.SetRwd "02"
.Wait 2, 50
.EndWhile
.EndOfTask
Pause 5
.CloseComm
End With
End Sub
Tasks 1 and 2 both use
While
and
EndWhile
to loop forever. Task 1 plays a song over and over, while task 2 runs the outputs in forward and reverse.
Tips
You've already seen a lot of the functionality of
Spirit.ocx
exposed in NQC. Although the syntax in Visual Basic is different, the basic functions are the same. LEGO's official Technical Reference
Document (see the ''Online Resources") does a good job describing the syntax of
Spirit.ocx
commands; I won't attempt to duplicate that work. Instead, this section contains down-to-earth
information on using
Spirit.ocx.
Retrieving Input and Timer Values
Although the Technical Reference Document makes it clear how to reset timer values and how to configure the RCX's inputs, you may be left wondering how to retrieve the value of an input or
timer. The answer is a versatile function called
Poll
.
Poll
can return many values from the RCX, including input values (in several different forms), timer values, variable values, the current
status of the RCX's outputs, and the last value received over the IR port.
Page 166
Poll
accepts a
Source
and a
Number
that, taken together, describe the value you want to retrieve. The key to understanding
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.
Using Constants
As you might have guessed, symbolic constants in your 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 constants in the (
Declarations
) section of your code module. For example, you might add the following definitions (the lines beginning with a single quotation
mark are comments):
' Sources
Public Const VARIABLE = 0
Public Const SENSOR_VALUE = 9
' Sensor names
Public Const SENSOR_1 = 0
Public Const SENSOR_2 = 1
Public Const SENSOR_3 = 2
Using these constants, the above
Poll
functions could be rewritten like this:
Summary of Contents for MINDSTORMS Robots
Page 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Page 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Page 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Page 41: ...Page 41 ...
Page 43: ...Next build the support for the light sensor ...
Page 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Page 82: ......
Page 84: ...Page 89 ...
Page 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Page 87: ...Page 91 ...
Page 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Page 89: ...Page 93 Page 94 ...
Page 90: ...Structural Support Page 95 ...
Page 91: ...Idler Wheel Page 96 ...
Page 92: ...Page 97 Drive Motor ...
Page 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Page 94: ...Grabber Arm Motor ...
Page 95: ...Page 99 ...
Page 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Page 158: ......
Page 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Page 160: ...Page 176 ...
Page 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...