In many datasheets for Ultrasonic sensors, I see a 40ms to 60 ms delay for pinging should be implemented but nearly ALL examples I have seen do not use this delay. Are there any issues that occur if you do not implement this delay ?
(deleted)
Thanks. I wrote this code to add in the delay.
double distance(bool Ping = false) {
double distancecm = 0;
unsigned long Ping;
if (Ping || (millis() - savedDist >= 50)) {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
distancecm = pulseIn(echo, HIGH) * 0.034 / 2;
savedDist = millis();
}
return distancecm;
}
I don't have any hardware to test right now, but is this correct ? Is there any demo code with the 40ms delay for an ultrasonic sensor ??
Or should I just add a millisecond delay which will run every single time it is called. Is the ms delay a better solution or the code on the top ?