B4R Library rSRF04-Easy Use Ultrasonic ranging

Discussion in 'B4R Libraries' started by santook, Feb 13, 2017.

  1. santook

    santook Member

    Example:
    Code:
    #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
     

    Attached Files:

  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Thanks for the library = easy :)

    Suggestion
    Current to get the distance, the method dissensor.GetDistance is used:
    Code:
    dissensor.GetDistance
    Instead of a method, it is also possible to use the property Distance:
    Code:
    dissensor.Distance
    Change
    Code:
    Double GetDistance(void);
    To
    Code:
    Double getDistance(void);
     
    Peter Simpson likes this.
  3. santook

    santook Member

    Update Library to standard:
    Code:
    #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
     

    Attached Files:

    Peter Simpson likes this.
  4. rwblinn

    rwblinn Well-Known Member Licensed 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
    Code:
    #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
    Code:
    #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
    Code:
    dissensor.Initialize(2,3)
    Do While(True)
         
    Log("Dis=",dissensor.Distance,"CM"
         Delay(
    2000)
    Loop
     
    Peter Simpson likes this.
  5. santook

    santook Member

    I am very sorry, in the Update2, I did not compile and debug after modification. Update2 error has been corrected
    Code:
    #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
     

    Attached Files:

    Peter Simpson likes this.
  6. jahswani

    jahswani Active Member Licensed User

    Will this work on ESPs ?
     
Loading...