In all the prototypes I built using the Ping))) sensor, I have used the following code to convert the time to distance:
code code code...
bla bla bla...
somewhere in the loop {
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
constDist = microsecondsToInches(duration);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
void tareTheDist(){
tareDist = constDist;
Serial.print("TARE");
delay(500);
}
But while I was reading some of the documentation on the sensor on this site I found the following:
#include <Ping.h>
Ping ping = Ping(13,0,0);
void setup(){
Serial.begin(115200);
}
void loop(){
ping.fire();
Serial.print("Microseconds: ");
Serial.print(ping.microseconds());
Serial.print(" | Inches ");
Serial.print(ping.inches());
Serial.print(" | Centimeters: ");
Serial.print(ping.centimeters());
Serial.println();
delay(1000);
}
Well my initial thought is that while the first set will take less memory because I am not including “Ping.h”, but it just a bit more laborious with the code and requires you to keep track of more variables. The second seems easier to implement due to the fact that you don’t need manually use the delaymicroseconds() function.
So my question is, will using the latter make my sketches use less memory, and will it be more or less accurate?
P.S. I added the “void tareTheDist()” function because I’m really proud of it as it is the first function outside of setup() and loop() that I ever made myself!