To begin with, you should enter your VB or VBA environment. If you have a full version of VB installed, just start it up. If you have any of the Microsoft Office applications, you can use VBA.
In Word, for example, choose Tools > Macro > Visual Basic Editor.
Showing Spirit.ocx in the Tool Palette
To reference Spirit.ocx we first need to put it in a user form. Create a new form by choosing Insert > UserForm. A new form appears, along with a tool palette labeled Toolbox.
Spirit.ocx does not appear in the tool palette. To add it, choose Tools > Additional Controls. In the window that appears, scroll down to find Spirit Control in the list of available controls.∗ Click
on the box to its left to select it, then click on OK. You only have to follow this procedure once. When you use VB again later, the Spirit.ocx control will automatically show up in your tool
palette, as shown in Figure 8-2.
A new item with the familiar LEGO logo should now be in your tool palette. Select this item and place it on the form you just created. We won't use the form for anything except as a place to keep
the Spirit.ocx control. Change the name to something like DummySpiritForm. The control itself has a default name of Spiritil, which is just fine for our purposes.
∗ The Spirit Control item appears in the list only if you have already installed the LEGO MINDSTORMS software. During this installation, Spirit.ocx is registered with the system in such a way that
VB or VBA can find it later.
You should glance over Spiritl's properties to make sure they're set up properly. In particular, check the ComPortNo property to be sure you'll be talking to the right serial port.
Hello, Spirit
Now that you've placed a Spirit.ocx control on a form, let's write some code to use it. Choose the Insert > Module menu item to create a new code module. In the window that appears, type the
following to create a new subroutine:
Sub HelloSpirit
To call functions in Spirit.ocx, you need to reference the control by name. The dummy form you created is DummySpiritForm. It contains a Spirit.ocx control called Spiritl. The full name
of the control, then, is DummySpiritForm.Spirit1.
Fill out the body of the HelloSpirit subroutine as follows:
Sub HelloSpirit ()
DummySpiritForm.Spiritl.InitComm
DummySpiritForm.Spiritl.PlaySystemSound 0
DummySpiritForm.Spiritl.CloseComm
End Sub
To run this simple subroutine, make sure your RCX is on. Then click on the play button in the toolbar. If all goes well, you should hear your RCX play a simple beep.
Figure 8-2.
Spirit.ocx in the tool palette
Page 162
Need help?
Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?
Questions and answers