
.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:
Содержание MINDSTORMS Robots
Страница 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Страница 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Страница 25: ...Page 22 Attach the motor wires to output A and output C Next build the platform for the front bumpers ...
Страница 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Страница 41: ...Page 41 ...
Страница 43: ...Next build the support for the light sensor ...
Страница 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Страница 82: ......
Страница 84: ...Page 89 ...
Страница 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Страница 86: ...Page 90 Grabber Arm In Step 17 the half bushings go between the center block and the cams pear shaped pieces ...
Страница 87: ...Page 91 ...
Страница 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Страница 89: ...Page 93 Page 94 ...
Страница 90: ...Structural Support Page 95 ...
Страница 91: ...Idler Wheel Page 96 ...
Страница 92: ...Page 97 Drive Motor ...
Страница 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Страница 94: ...Grabber Arm Motor ...
Страница 95: ...Page 99 ...
Страница 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Страница 158: ......
Страница 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Страница 160: ...Page 176 ...
Страница 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...
Страница 162: ...The light sensor which is mounted on the bumper is attached to input 2 The touch sensor goes on input 1 Page 179 ...