Read Ultrasonic Sensor with MindSqualls

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
jamesstewy
Posts: 1
Joined: 22 Dec 2012, 01:13

Read Ultrasonic Sensor with MindSqualls

Post by jamesstewy »

Hello,

I am trying to create a simple program in VB.NET with the MindSqualls .NET libaray v2.2 (http://www.mindsqualls.net). The program simply records the distances measured by the ultrasonic sensor attached to my Mindstorms 2.0 brick.

My problem is I don't know how to read the Ultrasonic Sensor and the only code I can find on the internet crashes the program without an error.

At the moment I have successfully connected to the NXT with the code below,

Global Variables

Code: Select all

Dim brick As NxtBrick
Dim Sensor_Ultrasonic As New NxtUltrasonicSensor

Code: Select all

Function Connect(port As Integer) As Boolean
    Dim COMPortName As String
    COMPortName = "COM" & port
    Try
        brick = New NxtBrick(COMPortName)
        brick.Connect()
        If brick.IsConnected Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Return False
        MsgBox("Error: " & ex.Message)
        Label_Status.Text = "Error"
    End Try
End Function
If the function returns true, my current code sets up the ultrasonic sensor,

Code: Select all

brick.Sensor1 = Sensor_Ultrasonic
AddHandler Sensor_Ultrasonic.OnPolled, AddressOf Sensor_Ultrasonic_OnPolled
Sensor_Ultrasonic.PollInterval = 100
When the Ultrasonic Sensor polls, the following sub routine fires

Code: Select all

Sub Sensor_Ultrasonic_OnPolled(ByVal polledItem As NxtPollable)
    Dim sensor As NxtUltrasonicSensor = polledItem
    [b]Dim Distance As Single = sensor.DistanceCm[/b]

    Label1.Text = Distance.ToString
End Sub
However, the program crashes without an error when it runs

Code: Select all

Dim Distance As Single = sensor.DistanceCm
What am I doing wrong?

Thanks,

JamesStewy
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests