Tips - LEGO MINDSTORMS Robots Manual

Unofficial guide
Table of Contents

Advertisement

.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.
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:
Page 166

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?

Questions and answers

Table of Contents