Speed of sound from PING Sensor

All,

Just trying to calculate speed of sound based with a PING sensor utilizing measured time and distance. Velocity = distance over time. see below...

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200// maximum distance we want to ping for (in inches). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results
}

void loop() {
delay(1000); // Wait 1000ms (1 sec) between pings (about 1 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
unsigned int uS1Way;
unsigned int SNDSP; // experimental
unsigned int Dist = sonar.ping_cm(); //experimental
Serial.print("Sonar Time: ");
Serial.print(uS1Way = uS/2);
Serial.print(" usec");
Serial.print(" - ");
Serial.print("Distance: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println(" cm");
Serial.println(SNDSP = (Dist / uS1Way) * 10000); // experimental
}

The result is just a zero, any suggestions ?

TIA

What does the sketch print?
Not just a zero, I hope.

Please remember to use coffee tags when posting code

  unsigned int uS1Way;
  unsigned int SNDSP;  // experimental

Local variables are not initialized, unless YOU do it. You don't.

 Serial.print(uS1Way = uS/2);

This is stupid. When you no longer want to see the value, you can't just comment out this line. DO NOT DO THIS CRAP.

The result is just a zero, any suggestions ?

The result of what? Anonymous printing sucks. Print a variable's name and then it's value. Otherwise, you are just guessing that is being printed.

Please remember to use coffee tags when posting code

Just what IS a coffee tag?

Damned auto connect.

AWOL:
What does the sketch print?
Not just a zero, I hope.

Please remember to use coffee tags when posting code

Yes, reports a 0

waketech:
Yes, reports a 0

Just a zero?
No "Sonar time" or "distance"?
You have got problems.

Apart from all the other problems already pointed out, you have a problem with integer division which is why you are getting zero.

Serial.println(SNDSP = (Dist / uS1Way) * 10000); // experimental

Dist and uS1Way are declared to be integer, so if Dist is less than uS1Way, this will print zero. Change one or both to be float. And there's another such division to fix as well.

And I second PaulS's comment ". DO NOT DO THIS CRAP." in regard to using an assignment statement within a println statement.

And finally, I also second AWOL's comment that you learn How to post code properly.

Pete

AWOL:
Just a zero?
No "Sonar time" or "distance"?
You have got problems.

Sorry time and distance are coming our correctly

el_supremo:
Apart from all the other problems already pointed out, you have a problem with integer division which is why you are getting zero.Dist and uS1Way are declared to be integer, so if Dist is less than uS1Way, this will print zero. Change one or both to be float. And there's another such division to fix as well.

And I second PaulS's comment ". DO NOT DO THIS CRAP." in regard to using an assignment statement within a println statement.

And finally, I also second AWOL's comment that you learn How to post code properly.

Pete

Thanks for the float info, as I am new I am still not sure what you mean in regards to "using an assignment statement with a println statement", what does this mean in stupid newbie english :slight_smile:

Serial.print(uS1Way = uS/2);

KeithRB:
Serial.print(uS1Way = uS/2);

I know the command is the one questioned but why is it bad ?

When you put it as the argument on Serial.print like that, uS1Way value I believe not to be stored.

Good practice would be a bit more like this:

uS1Way = uS/2;
Serial.print(uS1Way);

As PaulS has pointed out, if you decide at a later date that you don't need that value printed you can't just comment the statement

  //Serial.print(uS1Way = uS/2);

because now the value of uS1Way is not being calculated and suddenly your program mysteriously stops working. Separate the assignment from the print.

  uS1Way = uS/2;
  //Serial.print(uS1Way);

Pete

hitbyatruck:
When you put it as the argument on Serial.print like that, uS1Way value I believe not to be stored.

The world is sadly full of mistaken beliefs.