Hi All,
I ordered an ultrasonic sensor and received it ok. I plugged it real quick on the bread board and it works fine. Only thing is, the sample sketch I found to test it returns a 1 digit measurement ? I don't get it, the variables are Long.
Here it is:
int pingPin = 13; int inPin = 12;
long microseconds;
void setup() { Serial.begin(9600); }
void loop() {
long duration, inches, cm;
digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); delayMicroseconds(2); pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT); duration = pulseIn(inPin, HIGH);
inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration);
Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();
delay(100); }
long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2;
}
Any suggestions to get a few more decimal places ?