In this code the original programmer made some "don't"s
/*wait for measurement to complete. */
timeout= millis()+300;
while (!digitalRead(18)) {
if (millis()>timeout) {
return 0;
}
} //end wait digitalread
digitalRead(18) is the same as digitalRead(A4) which reads the status of SDA on an UNO (but not on a Mega2560). You should always use the A4 version because then the reader sees (without knowing the Arduino library internas) what the programmer wanted to do.
Then he did the timing wrong. that code should read:
/*wait for measurement to complete. */
uint32_t saved_millis= millis();
while (!digitalRead(SDA)) { // SDA is defined in pins_arduino.h
if (millis() - saved_millis > 300) {
return 0;
}
} //end wait digitalread
That way it works correctly even if the millis() value overflows and it works on all supported Arduino models.