*SOLVED* Have I been using Ping))) the hard way this whole time?

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! :wink:

So my question is, will using the latter make my sketches use less memory, and will it be more or less accurate?

With the entire code, we have no way to determine the first, and without your hardware and physical environment, no way to determine the 2nd

Try it both ways, let us know what you find out. Maybe you'll find out your code is actually performing very well.

So I tried out both codes. Turns out that while the first one takes about 1.6k less memory, it is definitely harder to keep track of all the variables though. I think it worth it because the code is a lot nicer to read!

How about the accuracy?

Accuracy is the same. The equation used inside the ping.h function is the same I was using manually. So doing it manually is just a bit more laborious, but saves memory!

Without seeing all your code I don't know what's missing but why not convert the bit you supplied to a function?

//Call with this
constDist = Ping( pingPin);

long Ping(int pingPin){
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  long duration = pulseIn(pingPin, HIGH);
  return duration / 74 / 2;
  }