First, your code is badly indented (remember to press ^T in the Arduino IDE to format it properly) and thus unreadable. After proper formatting, here is what loop() looks like:
void loop() {
new_time = millis(); /* check if to run this time */
if (new_time >= next_time) {
/* Calculate distance */
echotime = GetEchoTime();
/* only scale valid readings 0 is timeout or 1 is no echo
realistically minimum accurate or physical range is 3cm */
if (echotime > MAX_ERROR) {
// Valid number convert to cm
distance = echotime;
distance += SCALE_CM_ROUND; // add in half bit rounding
distance /= SCALE_CM;
}
/* catch errors first */
if (echotime <= MAX_ERROR || distance > MAX_RANGE) {
if (echotime > 0 && echotime <= MAX_ERROR)
Serial.println("MAX");
} else if (distance < MIN_RANGE)
Serial.println("MIN");
else
Serial.println(int(distance)); // In range output distance
next_time = new_time + INTERVAL; // save next time to run
}
}
Now the simplest answer as to why "MAX" is never displayed is because the conditions are not reached. However, I'm not sure that the logic in your ifs is correct. You have an if/else if/else main test, and inside the if another if, and that doesn't make much sense. Work on that logic first.