Trying to port arduinos ultrasonicsensor lib

Hey, my readings are nonsense…
Perhaps someone can look over it give some good advise?
I’m using the HCSR04 sensor driven with 5V. pins 15 & 16 on the daisy are 5V tolerant. so I use them with the sensor. Sending a 3.3V volt trigger gate.

any help would help :slight_smile:
cheers!

main.cpp

#define U_SONIC_PIN_TRIGGER D15
#define U_SONIC_PIN_ECHO D16

void InitEcholot()
{
    echolot.Init(U_SONIC_PIN_TRIGGER, U_SONIC_PIN_ECHO);
}
float GetEcholotDistance(){

    return echolot.measureDistanceCm();
}

HCSR04.h

#pragma once

#include "daisy_seed.h"
using namespace daisy;

class UltraSonicDistanceSensor {
 public:
    /**
     * @param triggerPin  Digital pin that is used for controlling sensor (output).
     * @param echoPin  Digital pin that is used to get information from sensor (input).
     * @param maxDistanceCm  Maximum distance sensor can measure, defaults to 4m for HC-SR04.
     *                       You might want to set this value if you are using different sensor than HC-SR04
     *                       or if you don't care about distances larger than whatever you will set it to
     *                       (therefore reducing time it takes for a single measurement).
     * @param maxTimeoutMicroSec  Single measurement will never take longer than whatever value you provide here.
     *   You might want to do this in order to ensure your program is never blocked for longer than some specific time,
     *   since measurements are blocking.
     *   By default, there is no limit on time (only on distance). By defining timeout, you are again limiting the distance. 
     */
    UltraSonicDistanceSensor(){};
    ~UltraSonicDistanceSensor(){};

    /**
     * Measures distance by sending ultrasonic waves and measuring time it takes them
     * to return.
     * @returns Distance in centimeters, or negative value if distance is greater than 400cm.
     */
    void Init(Pin triggerPin, Pin echoPin, 
            unsigned short maxDistanceCm = 400, 
            unsigned long maxTimeoutMicroSec );
    
    float measureDistanceCm();

    /**
     * Measures distance by sending ultrasonic waves and measuring time it takes them
     * to return. Measurement can not exceed duration calculated with maxDistanceCm or maxTimeoutMicroSec.
     * @param temperature  Temperature in degrees celsius
     * @returns Distance in centimeters, or negative value if distance is greater than 400cm.
     */
    float measureDistanceCm(float temperature);

    unsigned short maxDistanceCm;
    unsigned long maxTimeoutMicroSec;
    
 private:
    unsigned long pulseInSimpl(GPIO pin, bool state, unsigned long maxloops);
    GPIO triggerPin_, echoPin_;

    
};


void UltraSonicDistanceSensor::Init(
        Pin triggerPin, Pin echoPin, unsigned short maxDistanceCm, unsigned long maxTimeoutMicroSec) {

    this->maxDistanceCm = maxDistanceCm;
    this->maxTimeoutMicroSec = maxTimeoutMicroSec;

    echoPin_.Init(echoPin, GPIO::Mode::INPUT, GPIO::Pull::NOPULL);
    triggerPin_.Init(triggerPin, GPIO::Mode::OUTPUT);
}

float UltraSonicDistanceSensor::measureDistanceCm() {
    //Using the approximate formula 19.307°C results in roughly 343m/s which is the commonly used value for air.
    return measureDistanceCm(19.307);
}

float UltraSonicDistanceSensor::measureDistanceCm(float temperature) {
    unsigned long maxDistanceDurationMicroSec;

    // Make sure that trigger pin is LOW.
    triggerPin_.Write(0); 
    System::DelayUs(2);
    // Hold trigger for 10 microseconds, which is signal for sensor to measure distance.
    triggerPin_.Write(1); 
    System::DelayUs(10);
    triggerPin_.Write(0); 


    float speedOfSoundInCmPerMicroSec = 0.03313 + 0.0000606 * temperature; // Cair ≈ (331.3 + 0.606 ⋅ ϑ) m/s

    // Compute max delay based on max distance with 25% margin in microseconds
    maxDistanceDurationMicroSec = 2.5 * maxDistanceCm / speedOfSoundInCmPerMicroSec;
    if (maxTimeoutMicroSec > 0) {
    	maxDistanceDurationMicroSec = std::min(maxDistanceDurationMicroSec, maxTimeoutMicroSec);
    }

    

    // Measure the length of echo signal, which is equal to the time needed for sound to go there and back.
    unsigned long durationMicroSec = pulseInSimpl(echoPin_, 1, maxDistanceDurationMicroSec); // can't measure beyond max distance

    float distanceCm = durationMicroSec / 2.0 * speedOfSoundInCmPerMicroSec;
    if (distanceCm == 0 || distanceCm > maxDistanceCm) {
        return -1.0 ;
    } else {
        return distanceCm;
    }
}

 unsigned long UltraSonicDistanceSensor::pulseInSimpl(GPIO pin, bool state, unsigned long maxloops)
 {
    unsigned long width = 0;
    // wait for any previous pulse to end
    while (pin.Read() == state)
        if (--maxloops == 0)
            return 0;

    // wait for the pulse to start
    while (pin.Read() != state)
        if (--maxloops == 0)
            return 0;

    // wait for the pulse to stop
    while (pin.Read() == state) {
        if (++width == maxloops)
            return 0;
    }
    return width;
 }

nobody with an idea?