B4R Library rSRF04-Easy Use Ultrasonic ranging

Example:
B4X:
#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
    Public dissensor As SRF04    'Create Object
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    dissensor.Initialize(2,3)    'Initialize IO
    Do While(True)
        dissensor.Ranging    'Ranging
        Log("Dis=",dissensor.GetDistance,"CM")    'Get Distance
        Delay(2000)
    Loop
   
End Sub
 

Attachments

  • rSRF04.zip
    1.4 KB · Views: 516

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for the library = easy :)

Suggestion
Current to get the distance, the method dissensor.GetDistance is used:
B4X:
dissensor.GetDistance
Instead of a method, it is also possible to use the property Distance:
B4X:
dissensor.Distance
Change
B4X:
Double GetDistance(void);
To
B4X:
Double getDistance(void);
 

santook

Member
Update Library to standard:
B4X:
#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
    Public dissensor As SRF04     'Create Sensor Object
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    dissensor.Initialize(2,3)    'Init IO
    Do While(True)
        dissensor.getDistance    'Ranging
        Log("Dis=",dissensor.Distance,"CM")    'Get Distance Property
        Delay(2000)
    Loop
   
End Sub
 

Attachments

  • rSRF04.zip
    1.4 KB · Views: 455

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

thanks for the update.
Based on my suggestion post #2, this is another possible option (I had in mind) with one method and the distance property.
Note: have not tested the code yet as no SR04 sensor at hand.

h file
B4X:
#pragma once
#include "B4RDefines.h"

namespace B4R
{
    //~shortname: SRF04
    class SRF04
    {

        private:
            Byte tripin;
            Byte echpin;

        public:
            void Initialize(Byte TriggerPin, Byte EchoPin);
            Double getDistance(void);
           
    };
}

cpp file
B4X:
#include "B4RDefines.h"
namespace B4R
{
    void SRF04::Initialize(Byte TriggerPin, Byte EchoPin)
    {
        tripin = TriggerPin;
        echpin = EchoPin;
        pinMode(TriggerPin, OUTPUT);
        pinMode(EchoPin, INPUT);
        digitalWrite(tripin, HIGH);
        digitalWrite(echpin, LOW);
    }

    Double SRF04::getDistance(void)
    {
        Double dis;
        digitalWrite(tripin, HIGH);
        digitalWrite(tripin, LOW);
        dis = pulseIn(echpin, HIGH) / 58.0;
        digitalWrite(tripin, HIGH);
        digitalWrite(echpin, LOW);
        return dis;
    }
}

B4R Code Snippet
B4X:
dissensor.Initialize(2,3)
Do While(True)
     Log("Dis=",dissensor.Distance,"CM") 
     Delay(2000)
Loop
 

santook

Member
I am very sorry, in the Update2, I did not compile and debug after modification. Update2 error has been corrected
B4X:
#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
    Public dissensor As SRF04   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    dissensor.Initialize(2,3)
    Do While(True)
        Log("Distance:",dissensor.GetDistance,"CM")
        Delay(1000)
    Loop
End Sub
 

Attachments

  • rSRF04.zip
    1.4 KB · Views: 500
Top