Reading ultrasonic sensor HCSR04, with Gadgeteer and Visual Basic

Leyendo el sensor ultrasónico HC-SR04 con Gadgeteer y Visual Basic

Este programa pretende ser un punto de partida para la lectura del sensor HC-SR04, pero por supuesto usted debe proporcionar las funciones Adicionales necesarias, tales como «tomar valores promediados« para obtener un mínimo de precisión 🙂

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports GTI = Gadgeteer.Interfaces
 
' This program is intended as a starting point reading the HC-SR04 sensor, 
' but of course you must provide adicional functions like "take values from average" to make a minimum of precission.
 
Namespace GadgeteerApp1
    Partial Public Class Program
        'Define GPIO's
        Private Echo As GTI.DigitalInput
        Private Trig As GTI.DigitalOutput
        Private Ticks As Integer = CInt(TimeSpan.TicksPerMillisecond / 1000)
        'Define socket number
        Private soketNum As Integer = 9
        'Main
        Public Sub ProgramStarted()
            Dim socket = Gadgeteer.Socket.GetSocket(soketNum, TrueNothingNothing)
            Echo = New GTI.DigitalInput(socket, Gadgeteer.Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.PullDown, Nothing)
            Trig = New GTI.DigitalOutput(socket, Gadgeteer.Socket.Pin.Four, FalseNothing)
            timer.Start()
            Debug.Print("Program Started")
        End Sub
        ' Just print distance into Debug windows
        Private WithEvents timer As GT.Timer = New GT.Timer(1000)
        Private Sub timer_Tick(timer As Gadgeteer.TimerHandles timer.Tick
            '
            Debug.Print(readSR04.ToString())
        End Sub
 
        'Define Max/Min for SR04 reading distances 
        Private Const Dist_MIN As Integer = 2       'cm
        Private Const Dist_MAX As Integer = 400     'cm
 
        'Define return values if reading fails (out of range)
        Private Const Flag_MIN As Integer = -999
        Private Const Flag_MAX As Integer = 999
 
        'This function returns distance in cm from SR04
        Function readSR04() As Integer
            Dim start As Double = 0
            Dim Distance As Integer = 0
            Dim Fails As Integer = 0
            'send a 10us TTL pulse
            Trig.Write(True)
            Thread.Sleep(10)
            Trig.Write(False)
            'wait for the echoe
            While Not Echo.Read
                Fails += 1
                If Fails > 500 Then
                    Thread.Sleep(0)
                    Exit While
                End If
                start = System.DateTime.Now.Ticks
            End While
            ' calculating distance
            While Echo.Read
                Thread.Sleep(0)
               Distance = CInt(((System.DateTime.Now.Ticks - start) / Ticks) / 58) + Dist_MIN
            End While
 
            If Distance < Dist_MAX Then
                If Distance >= Dist_MIN Then
                    Return Distance
                Else
                    Return Flag_MIN
                End If
            Else
                Return Flag_MAX
            End If
 
        End Function
 
    End Class
End Namespace
 
Saludos,
PepLluis,

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *