How long is my PulseIn() taking to complete?

Is there a way to calculate how long it takes for the pulseIn() to complete? I am trying to get the MaxBotix Sonar sensor to be on as little as possible and was wondering if there was a way to calculate the time it takes to accomplish the pulseIn() command so I can shut off the sensor.

Thanks!

/* 
Test code for the Arduino Uno 
Written by Tom Bonar for testing 
Sensors being used for this code are the MB10X0 from MaxBotix 
All PW inputs are coded in this for simplicity. 
Remove the comments to use the additional sensor inputs 
*/ 
const int pwPin1 = 3; 
long pulse1, sensor1; 
unsigned long duration;

void setup () { 
Serial.begin(9600); 
pinMode(pwPin1, INPUT); 
digitalWrite(7, LOW);
} 

void read_sensor(){ 
pulse1 = pulseIn(pwPin1, HIGH); 
sensor1 = pulse1/147; 
} 

//This section of code is if you want to print the range readings to your computer too remove this from the code put /* before the code section and */ after the code 
void printall(){ 
Serial.print("S1"); 
Serial.print(" "); 
Serial.print(pulse1); 
Serial.println(" "); 
} 

void loop () { 
delay(5);
 digitalWrite(7, HIGH);
read_sensor(); 
printall();
digitalWrite(7, LOW);
//delay(2000);
}

Is there a way to calculate how long it takes for the pulseIn() to complete?

27 minutes, or the time returned by pulseIn().

You may be able to use this:

Thanks, I was able to figure out that is was taking 280ms. I have another quick question… .Anyone know why this sensor has so much false positives and wrong values?

/*
 Sharp GP2Y0A21YK0F infrared proximity sensor (#28995)
 Collects an average of five readings from the sensor and displays the
 resulting value about every half second.
 
 See the SharpGP2Y0A21_Simple demonstration for additional usage notes,
 including connection diagram.
 
 This example code is for the Arduino Uno and direct compatible boards, using the
 Arduino 1.0 or later IDE software. It has not been tested, nor designed for, other 
 Arduino boards, including the Arduino Due.

*/

const int irSense = A0;          // Connect sensor to analog pin A0
int distance = 0;

void setup() {
  Serial.begin(9600);            // Use Serial Monitor window
}

void loop() {
  Serial.println(irRead(), DEC); // Call irRead function to read sensor
                                 // Print value in Serial Monitor
  delay(250);                    // Wait another 1/4 second for the next read
                                 // (Note: Because of delays built into the
                                 //   irRead function the display of values will
                                 //   be slower than in the SharpGP2Y0A21_Simple
                                 //   sketch
}

// Take multiple readings, and average them out to reduce false readings
int irRead() {
  int averaging = 0;             //  Holds value to average readings

  // Get a sampling of 5 readings from sensor
  for (int i=0; i<5; i++) {
    distance = analogRead(irSense);
    averaging = averaging + distance;
    delay(55);      // Wait 55 ms between each read
                    // According to datasheet time between each read
                    //  is -38ms +/- 10ms. Waiting 55 ms assures each
                    //  read is from a different sample
  }
  distance = averaging / 5;      // Average out readings
  return(distance);              // Return value
}

How did you connect the sensor? Bad or loose wiring (not soldered) can give false signals.

GND to GND, VCC to 3v, A0 using a breadboard and jumper wires. It feels like the sensor has 1-2 bad readings every 10 polls.

What do you consider a bad reading?
Is it way off or just slightly off?
What is the sensor doing to the output when updating?
Dwight

+-100 is the readings I get when its off.