- HOME -

Visual Basic and Lego SDK2

This material is based on the work of Jos van Kempen. 
See: http://www.medewerker.hro.nl/kemjt/ 

Lego's first SDK was relatively easy to use with Visual Basic. SDK2 is a little more complicated.

Download Lego's SDK2 from here.

STEP 1    Install the Lego Robolab software from the CDROM.

STEP 2    Install the Lego SDK2

STEP 3    Launch Visual Basic and go to Project/References.

STEP 4    Put a tick next to  LEGO VPBrick 2.0 Type Library 

You can confirm the VPBrick Library is installed by pressing F2. The Object Browser window appears ...

STEP 5    Create a new Form and add Command Buttons ...    

STEP 6    Insert the code below ... (or download the VB files here.)

WARNING
You can expect VB to crash when experimenting with your code. Save your work often.

 

The two commands used in this example are: "out" and "dir".

"dir" sets the direction of the motors. "out" turns the motors on, or off as indicated in the table below.

For further information see the VPB Help library (vpb.hlp) which is automatically installed in the Bin directory when you install the Lego SDK2.

See: C:\Program Files\LEGO Software\LEGO Mindstorms SDK\Bin\vpb.hlp

**************** Motors ON/OFF ******************
Changes the status of the listed outputs.

LASM Command: out Action, motors

Legal range for ‘action’: 0 (float), 1 (off) and 2 (on).
Legal range for ‘motors’: 1-7 (a bit mask) as below ...

Action Motors Selected
Mask bits MotorC  MotorB  MotorA
1 001  NO  NO  YES
2 010  NO  YES NO 
3 011  NO  YES YES
4 100  YES NO  NO 
5 101  YES NO  YES
6 110  YES YES NO 
7 111  YES YES YES

******************************************************


************* Motor Direction ***************
Changes the direction of the listed outputs.

LASM Command: dir Action, motors

Legal range for action:
0 (backward), 1 (change direction), 2 (forward).
Legal range for motors: 1-7 (a bit mask) as above.
 **********************************************

The code ... (This has not yet been tested with the USB Tower.)

Private WithEvents vpb As LEGOVPBrickLib.VPBrick
Private Port$ 'current port
' Based on the work of Jos van Kempen
' See: http://www.medewerker.hro.nl/kemjt/ 


Private Sub MotorA_Forward_Click()
  vpb.Execute "dir 0,1", LASM
End Sub

Private Sub MotorA_Reverse_Click()
  vpb.Execute "dir 2,1", LASM
End Sub

Private Sub Sound_Click()
  vpb.BrickType = vpb.Status(CheckBrickType)
  If vpb.Status(BrickStatus) = StatusReady Then
    vpb.Execute "sound 3"                                            ' This isn't a LASM command.
  Else
    GoTo except
  End If
Exit Sub
except:
  MsgBox "Error"
End Sub

Private Sub MotorA_ON_Click()
  vpb.Execute "out 2,1", LASM
End Sub

Private Sub MotorA_OFF_Click()
  vpb.Execute "out 1,1", LASM
End Sub

Private Sub Form_Activate()
On Error GoTo except
  FindPort
  OpenPort
Exit Sub
except:
  MsgBox "Error"
End Sub

Private Sub Form_Load()
  Set vpb = New LEGOVPBrickLib.VPBrick
  Port$ = ""
End Sub

Private Sub FindPort()
On Error GoTo except
  vpb.FindPort Port$
Exit Sub
except:
  MsgBox "Error"
End Sub

Private Sub OpenPort()
  On Error GoTo except
  vpb.Open Port$
Exit Sub
except:
  MsgBox "Error"
End Sub

Private Sub Form_Terminate()
  vpb.Close
End Sub