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
}
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.
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.
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