How to reliable measurement by ULTRASONIC SENSOR (e.g. HC-SR04)?

Hello,

what is the best way to measure sharp edges and railings with ultrasonic sensor?
I’ve wrotte the following simple code for my rover, based on the code from Tautvidas Sipavicius adapted for HC-SR04:

/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* VCC connection of the sensor attached to +5V
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin 2
	* ECHO connection of the sensor attached to digital pin 4
 */
 
 
const int trigPin = 2;
const int echoPin = 4;
 
void setup() {
  Serial.begin(9600);
  // initialize serial communication:
}
 
void loop()
{
  int duration, cm, distance = 10000;
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
 
 for (short i = 0; i < 5; i++)  // Take 5 samples
{ 
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
 
  cm = duration / 29 / 2;
  // convert the time into a distance
  // 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.
  
  if (cm < distance)
  {
    distance = cm; 
  }
  //  Choose the lowest value of samples
  delay(30);  // ---> How low can it be?
}
Serial.print(distance);
Serial.println(" cm");

}

While experimenting with delay values I’ve notice that less than 10ms makes the ultrasonic send e.g. 220cm in 4 cm long distance sometimes or makes it a bit confused from time ti time.
I’ve also notice the shorter delay, the longer intervals between printed values on more distanced obstacles (hard to notice within 50-100ms).
May it be so due to echos in a room?
How would you do that better?

Any suggestions are welcomed!

I’ve modify the program a bit to this:

/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* VCC connection of the sensor attached to +5V
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin 2
	* ECHO connection of the sensor attached to digital pin 4
 */
 
 
const int trigPin = 2;
const int echoPin = 4;
 
void setup() {
  Serial.begin(9600);
  // initialize serial communication:
}
 
void loop()
{
  int duration, cm, distance = 10000;
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
 
 for (short i = 0; i < 7; i++)  // Take 7 samples
{ 
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
 
  cm = duration / 29 / 2;
  // convert the time into a distance
  // 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.
  
  if (cm < distance)
  {
    distance = cm; 
  }
  //  Choose the lowest value of samples
  delay(30);  // ---> How low can it be?
}

if (distance < 230)
{
  Serial.print(round(distance));
  Serial.println(" cm");
} else {
  distance = -1;  // OoR flag
  Serial.println("Out of Range");
}

}

What is you opinion? Have you any comment to that?