Erratic behavior when calling pulseIn function inside loop

I am a newbie to Arduino and am stuck trying to figure out why the following code always returns a value of 0 when measure_duration is called a second time in the loop function. This has not been making any sense to me. Any suggestions on how to get this code to work? What am I missing? Any help would be very appreciated.

// defines variables
const int trigPin = 9;
const int echoPin = 10;

const int relayPin = 7;
const int buzzerPin = 13;

unsigned long duration;        // Time taken by sound wave round trip
unsigned long distance;        // Distance from source to destination

void setup() {
 pinMode(trigPin, OUTPUT); // Sets the digital pin 9 as output
 pinMode(buzzerPin, OUTPUT); // Sets the digital pin 11 as output
 pinMode(relayPin, OUTPUT); // sets the digital pin 7 as output
 pinMode(echoPin, INPUT);  // Sets the digital pin 10 as an Input
 Serial.begin(9600);
}

void loop() {
 Serial.print("Entering loop\n");

 digitalWrite(trigPin, LOW);   // Sets the digital pin 9 off
 delayMicroseconds(5);         // waits for 2 micro seconds
 
  digitalWrite(trigPin, HIGH);  // Sets the digital pin 9 on
 delayMicroseconds(10);        // waits for 10 micro seconds
 digitalWrite(trigPin, LOW);   // Sets the digital pin 9 off

 // Reads the echoPin, returns the sound wave travel time in microseconds
 // duration = pulseIn(echoPin, HIGH);
 duration = measure_duration();
 if (duration == 0) {
   Serial.println("Warning 1: We did not get a pulse from the sensor.");
 }
 // Calculating the distance
 distance = duration * 0.034 / 2;

 Serial.print("Distance1: " );
 Serial.println(distance);

 if ((distance > 6) && (distance < 10)) {
   // start of cycle
   digitalWrite(relayPin, HIGH);
   delay(1000);
   digitalWrite(relayPin, LOW);
   // end of cycle
 }

 // check for hand removal
 boolean distanceOk = false;
 // loop till one of the following conditions are met
 // 1. user has removed hand so distance check will fail
 while (distanceOk == false) {
   delay(50);
   duration = measure_duration();
   if (duration == 0) {
     Serial.println("Warning 2: We did not get a pulse from the sensor.");
   }
   // Calculating the distance
   distance = duration * 0.034 / 2;
   Serial.print("Distance 2: " );
   Serial.println(distance);

   // check if state has not changed
   if ((distance > 6) && (distance < 10))  {
     digitalWrite(buzzerPin, HIGH); // Sets the buzzer pin 7 on
   } else {
     distanceOk = true;
     Serial.println("Distance is ok now. Can exit the loop now.");
   }
 }

 Serial.println("Exiting loop");
}

const unsigned long measure_duration() {
 pinMode(echoPin, OUTPUT);
 digitalWrite(echoPin, LOW);
 delayMicroseconds(2);
 digitalWrite(echoPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(echoPin, LOW);
 pinMode(echoPin, INPUT);
 return pulseIn(echoPin, HIGH);
}

(Tags added by Moderator)

Why would the echo pin need to be an output?
Which sensor are you using?

Please remember to use code tags when posting code.

You define a trigger pin and send a trigger pulse to the trigger pin but then send a trigger pulse to the echo pin before looking for the echo pulse on the echo pin? I think the time you are taking to send a pulse to the echo pin is causing you to miss the start of the echo pulse. Try this:

unsigned long measure_duration() {
  return pulseIn(echoPin, HIGH);
}

I am using the HC-SR04 ultrasonic sensor. I will try John's suggestion and see if that helps.

jdukeusa:
I am using the HC-SR04 ultrasonic sensor.

In that case, there is never a need to make the echo pin an output.