Open moringaman opened 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.
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;
}
Hi I'm getting the following error running this, initially it was working but suddenly I started to get the following error