int rangeInCm()
{
Wire.beginTransmission(112);
Wire.write(0);
Wire.write(0x51); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission();
delay(70);
Wire.beginTransmission(112);
Wire.write(byte(0x02));
Wire.endTransmission();
Wire.requestFrom(112, 2);
if(Wire.available() >= 2)
{
int reading = Wire.read();
reading = reading << 8;
reading |= Wire.read();
return reading;
}
else
return -1; // something went wrong
}
((uncompiled, untested)
Of course, that delay will prevent you doing other things, but you may not have other stuff to do.