Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Using Visual Basic

 

Notes:

 

  The Spirit.ocx is used to translate and transfer programs written in Visual Basic to the LEGO Mindstorms

     RCX. It bypassed the firmware installed with and used by LEGO.

  You do not have to load RCX firmware (it is bypassed)

  Unfortunately, Spirit.ocx was designed for use with SDK (System Development Kit) 1 which used a serial

     port.It will not work on SDK 2.0 which uses a USB port.

  A version that will work with SDK 2.0 is Spirit2.ocx.

  The program below is used to test the RCX. It sends LEGO assembly or LASM commends.

  Before using you must have SDK 2.0 installed and run the following

     regsvr32 C:\Windows\System32\Spirit2.ocx

  Select LEGO PBrick control from the Project Components menu

.

Sample code to test operations

Option Explicit

 

‘LegoVPB1 is the name of the form

Private Sub CmdDlgNext_Click()                                                'Program uses lots of command buttons

  LegoVPB1.DatalogNext TxtDlgNS.Text, TxtDlgNN.Text

End Sub

 

Private Sub CmdDlgUpload_Click()

Dim arr As Variant

Dim i, max As Integer

List1.Clear

 arr = LegoVPB1.UploadDatalog(TxtDlgF.Text, TxtDlgs.Text)

  If IsArray(arr) Then

    For i = LBound(arr, 2) + 1 To UBound(arr, 2) - 1

    'variable Typ=nr variable, HiByt*256+LoByt=valuee

    'Watch    Typ=128, number of minutes after 0:0 =HiByt*256+LoByt

    'no clock on display value=3? always???

    'sensor   Typ=64+nr_input(0,1 or 2) value=probebly depending on the mode

    'sensor type should be given first (otherwise no light etc)

    'for instance raw: no sensor or 'switch not in' about 1015, switch in about 50.

      List1.AddItem "Type: " & Str(arr(0, i)) & " LoByt: " & Str(arr(1, i)) & " HiByt: " & Str(arr(2, i))

    Next i

  Else

    MsgBox "Upload not a valid array"

  End If

End Sub

 

'VPB1 is the form

'PBAliveOrNot is

Private Sub CmdRCXAlive_Click()

  If LegoVPB1.PBAliveOrNot Then TxtResult = "True" Else TxtResult = "False"

End Sub

 

Private Sub CmdReconnect_Click()

  LegoVPB1.Reconnect

End Sub

 

Private Sub CmdSelectPrgm_Click()

  LegoVPB1.SelectPrgm TxtPrgm.Text

End Sub

 

Private Sub CmdSend_Click()

Dim lasm As Boolean

Dim result As Long

Dim i As Integer

 If ChkLasm = 1 Then lasm = True Else lasm = False

 LegoVPB1.Execute TxtCommand, lasm, result

 TxtResult = Str(result)

End Sub

'This is an example of how to use the LegoVBP.ocx

'to give immediate lasm (Lego Assambler) or Mindscript commands from a VB program

'To give a sound type 'sound 3' and do not select lasm

'You can also do this in VB-code by sending: LegoVPB1.Execute "sound 3"

'To give a command motor A(=1) on, type 'out 2,1' (2=on 0=Out 1=Float)

'You can also do this in VB-code by sending: LegoVPB1.Execute "out 2,1", LASM

 

 

'If you want the ocx on the form can be made invisible

 

Private Sub CmdBattery_Click()

  TxtResult = LegoVPB1.PBattery

End Sub

'=====================================================

'Output

'=====================================================

Private Sub CmdFloat_Click()

  LegoVPB1.Float (TxtMotor.Text)

End Sub

 

Private Sub CmdOff_Click()

  LegoVPB1.Off (TxtMotor.Text)

End Sub

 

Private Sub CmdOn_Click()

  LegoVPB1.Onn (TxtMotor.Text)

End Sub

 

Private Sub CmdSetDlg_Click()

   LegoVPB1.SetDatalog TxtDlgSize.Text

End Sub

 

Private Sub CmdSetFwd_Click()

  LegoVPB1.SetFwd (TxtMotor.Text)

End Sub

 

Private Sub CmdSetPower_Click()

Dim result As Long

'Dim motorlist As Integer

'Dim Power As Integer

'motorlist =

'Power = TxtPower.Text

 LegoVPB1.SetPower TxtMotor.Text, TxtPower.Text

End Sub

 

Private Sub CmdSetRwd_Click()

  LegoVPB1.SetRwd (TxtMotor.Text)

End Sub

Private Sub CmdAlterDir_Click()

  LegoVPB1.AlterDir (TxtMotor.Text)

End Sub

 

 

Private Sub CmdSetSensMode_Click()

  LegoVPB1.SetSensorMode TxtInput.Text, TxtInpMod.Text, TxtSlope.Text

End Sub

 

Private Sub CmdSetVar_Click()

Dim result As Long

  LegoVPB1.SetVar TxtVar.Text, TxtVarSource.Text, TxtVarNumber.Text

End Sub

 

Private Sub CmdSetWatch_Click()

Dim uur As Integer

Dim minuut As Integer

uur = Hour(Now): minuut = Minute(Now)

 LegoVPB1.SetWatch uur, minuut

End Sub

 

 

 

Private Sub CmdView_Click()

Dim result As Long

  LegoVPB1.SelectDisplay TxtViewSource.Text, TxtViewNumber.Text

End Sub

'Wait only for download

 

'=====================================================

'Sound

'=====================================================

Private Sub CmdSystSound_Click()

 LegoVPB1.PlaySystemsound (TxtSystemSound.Text)

End Sub

Private Sub CmdPlayTone_Click()

  LegoVPB1.PlayTone TxtFrequency.Text, TxtDuration.Text

End Sub

 

'=====================================================

'Input

'=====================================================

Private Sub CmdSetSensType_Click()

  LegoVPB1.SetSensorType TxtInput.Text, TxtType.Text

End Sub

Private Sub CmdPoll_Click()

  TxtPoll = LegoVPB1.Poll(TxtPollSource.Text, TxtPNumber)

End Sub

 

Private Sub Form_Load()

 

End Sub