rvnash / HC-SR04

Driver for the HC-SR04 range finder
Other
1 stars 0 forks source link

Error Running Library #1

Open moringaman opened 1 year ago

moringaman commented 1 year ago

Hi I'm getting the following error running this, initially it was working but suddenly I started to get the following error

-SR04.o lib/HC-SR04/src/HC-SR04.cpp
lib/HC-SR04/src/HC-SR04.cpp: In member function 'long unsigned int HC_SR04::triggerAndMeasurePulse()':
lib/HC-SR04/src/HC-SR04.cpp:80:1: error: control reaches end of non-void function [-Werror=return-type]
   80 | }
      | ^
lib/HC-SR04/src/HC-SR04.cpp:50:26: warning: 'duration' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |     unsigned long start, duration;
      |                          ^~~~~~~~
cc1plus: some warnings being treated as errors
make[2]: *** [../build/target/user/platform-13-mHC-SR04/src/HC-SR04.o] Error 1
make[2]: Leaving directory `/firmware/user'
make[1]: *** [user] Error 2
make[1]: Leaving directory `/firmware/modules/boron/user-part'
make: *** [modules/boron/user-part] Error 2
rvnash commented 1 year ago

I'm glad you find this library useful, but I am not actively maintaining it. My last checkin here was 2017.

That said, maybe I can give some pointers to resolving this problem. The code in question is quite small, and you should be able to just copy it into you project and modify it until it builds properly.

My guess is that the problem is around the use of the macro ATOMIC_BLOCK. The compiler thinks that there is a path to return from the function without executing a return statement. I don't see how, but perhaps something about that macro changed in the Particle library. Try moving the return duration; statement to be the very last statement in the function. That may do it.

Also, you can get rid of the warning by just setting duration = 0; at the beginning. In fact, maybe that is what the compiler is complaining about. Perhaps returning a potentially initialized variable is the same thing as "reaches end of non-void function". If so, the compiler's error is a bit misleading.

Good luck, and I hope you can work this out. If not, I'm sure there must be other drivers for this hardware available.

rvnash commented 1 year ago

Try this code:

unsigned long HC_SR04::triggerAndMeasurePulse()
{
    // Response pulse usually starts in under 500 uSecs, wait up to 2ms to be sure.
    const unsigned long timeoutHigh = 2000;
    // Response pulse should be shorter than about 29ms (allow 5 meter as limit)
    const unsigned long timeoutLow = 29000;
    unsigned long start, duration=0;

    // Timing is crucial here, so cannot allow other threads or interrupts
    // Maximum ammount of time in this block is limitted to 10 + 2,000 + 29,000 uS
    // Or 31 milliseconds
    ATOMIC_BLOCK() {
        // Send the 10 uSec pulse
        pinSetFast(trigPin);
        delayMicroseconds(10);
        pinResetFast(trigPin);

        start = micros();
        while (pinReadFast(echoPin) != HIGH) {
            duration = micros() - start;
            if (duration >= timeoutHigh) {
                // Didn't recieve a pulse
                return 0;
            }
        }

        start = micros();
        while (pinReadFast(echoPin) != LOW) {
            duration = micros() - start;
            if (duration >= timeoutLow) {
                // Pulse lasted longer than the 4 meter range limit
                return 0;
            }
        }
    }
    return duration;
}