B4R Library rNewPing

Johan Schoeman

Expert
Licensed User
This is my first attempt at a B4R library. Have wrapped NewPing for the HC-SR04 sensor (and I guess similar Ultrasonic sensors)

b4rLibFiles.zip - it contains the b4r wrapper files (xml file and folder with the .h and .cpp files. They need to go to your additional library folder (xml file and folder with the .h and .cpp files must go to the root of your additional library folder)
NewPing.zip - extract it and copy it to the Arduino library folder.
b4rNewPingLibrary.zip - the B4R sample project.

Sample Code:
B4X:
'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


Output:
B4X:
median in = 4
distance cm = 12
distance in = 4
microseconds = 667
median cm = 11
median in = 4
distance cm = 11
distance in = 4
microseconds = 671
median cm = 11
median in = 4
distance cm = 4
distance in = 4
microseconds = 667
median cm = 11
median in = 4
distance cm = 0
distance in = 4
microseconds = 671
median cm = 11
median in = 4
distance cm = 12
distance in = 4
microseconds = 671
median cm = 11
median in = 4
distance cm = 12
distance in = 4
microseconds = 671
median cm = 11
median in = 4
distance cm = 12
distance in = 4
microseconds = 671
median cm = 11
median in = 4
 

Attachments

Top