Page 173 - The Unofficial Guide to Lego Mindstorms Robots
P. 173
162
Figure 8-2.
Spirit.ocx in the tool palette
You should glance over Spiritl's properties to make sure they're set up properly. In particular, check the ComPortNo
p roperty 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
D ummySpiritForm.Spirit1.
Fill out the body of the HelloSpirit subroutine as follows:
Sub HelloSpirit ()
DummySpiritForm.Spiritl.InitCo mm
DummySpiritFor m.Spiritl.PlaySy stemSound 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.
It's little cumbersome to always refer to the full name of the Spirit.ocx control. A simpler syntax, using With, looks like this:
Sub HelloSpiritII ()
With DummySpiritForm.Spirit1
.InitComm
.PlaySystemSound 0
.CloseComm
End With
End Sub