'HC-SR04 Ultrasonic Module
'Vcc to Arduino 5V
'Gnd to Arduino Gnd
'Trig to Arduino pin 9
'Echo to Arduino pin 10
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Dim t As Timer
Dim max_distance As UInt = 500 'maximum distance in cm
Dim answer As ULong 'result of a call to a method in the library
Dim trigpin As Byte = 9 'the library will use pin 9 as trigger pin
Dim echopin As Byte = 10 'the library will use pin 10 as echo pin
Dim np As NewPing
Dim iterations As Byte = 10 'number of iterations when calling np.ping_median(iterations, max_distance)
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
t.Initialize("t_tick", 1000) 'Initialize the timer
np.Initialize(trigpin, echopin , max_distance) 'Initialize NewPing
t.Enabled = True
End Sub
Sub t_tick
answer = np.ping_cm(max_distance) 'get distance to object in centimeters
Log("distance cm = ", answer)
Delay(100)
answer = np.ping_in(max_distance) 'get distance to object in inches
Log("distance in = ", answer)
Delay(100)
answer = np.ping_median(iterations, max_distance) 'get the median time (microseconds) for "iterations" number of pings
Log("microseconds = ", answer)
Log("median cm = ", np.convert_cm(answer)) 'convert the median time (microseconds) received above to centimeters
Log("median in = ", np.convert_in(answer)) 'convert the median time (microseconds) received above to inches
End Sub