use Serial.begin(115200) that makes the serial output 12 times as fast
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
should not be used, just use : cm = duration/58; in your main code. It is not the main overhead but definitely faster...
Have you timed your ping(i) function, how many millis it takes?
Please try ping(i) as below : // just one lookup instead of looking it up every line of code
unsigned long ping(int i)
{
uint8_t pin = ultraSoundSignalPins[i];
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
return pulseIn(pin, HIGH) /58.138; //Listen for echo && convert to CM
}
2 cents,
Rob