I meant a more accurate measurement than just 4 cm. For example, 39.37mm. The serial monitor only supplies numeric values before the comma with the simple sktech.
Sorry, I did not get a clear understanding of your post.
Please try this and see if it helps. The call you are making, does this bit of math for you and returns as an int. This should help.
Also, please be aware that the accuracy might not be as accurate as you would like,
Temp, Humidity , altitude can all impact this.
unsigned int uS = sonar.ping();
float distance_cm = uS/57.0; // The constant "US_ROUNDTRIP_CM" = 57
Serial.print("Ping: ");
Serial.print(distance_cm,2);
Serial.println("cm");
The NewPing library uses the micros() function to time echo pulses. You can use sonar.ping(MaxDistanceInCM) to get the raw microsecond value as an unsigned int. Multiply by 0.344 to get the round-trip distance in mm and divide by 2 to get the one-way distance (or multiply by 0.172 directly). Because the micros() timer has a four-microsecond resolution your precision won't be any better than 0.688mm.
Who knows what precision the HC-SR04 is capable of but if you want to measure the Echo pulse with finer resolution you can use the Input Capture Register feature of Timer1. This will give you a pulse length resolution of 1/16th of a microsecond (0.0215 mm).