Parallax Ping returns sometimes zeros

Hear me out: first, I have the sonar mounted on top of a standard servo, both connected to an Uno. I wrote a program so that if the sonar detects anything within 15 inches, the servo stops. Otherwise, it turns left and right searching for a target.

I’ve put the sonar code found here into a perfectly-functioning library, ping.h, located in my repository. Alone, the sonar code works and I get a steady stream of accurate values; but as soon as I start using the servo library the sonar will lag once in a while, before returning a zero. If the lag time wasn’t so long (1 to 2 seconds) then it would work fine. Another interesting tidbit, compiling this in Visual Studio instead of Arduino makes the sonar return even fewer zeros. I can’t fathom why. If anyone could steer me in the right direction, I would greatly appreciate it. Here’s the code:

#include <Servo.h>
#include <ping.h>

byte pingPin = 2, servoPin = 9, pos = 90;
long duration, inches;

Servo myservo;
Ping ping(pingPin);

void setup()
{
  Serial.begin(57600);  
  myservo.attach(servoPin, 500, 2400);
  myservo.write(90);  //Initially center turret.
}

void loop(){
  searchTarget();
}

byte searchTarget()
{
  do
{ 
    delay(50);//Inserting this delay reduces the number of '0's from the sonar.
    duration = ping.readDistance();
    inches = ping.microsecondsToInches(duration);//Converts pulse duration into inches.
    if (inches < 15)
    {  //If something is detected within 15 inches...
      //Take a picture!
    }
    else {  //Otherwise keep turning right.
      pos++;
      myservo.write(pos);
    }
  }while (pos < 180);  //Turn right while PING doesn't detect anything and while the servo is not fully turned right.
  if (pos >= 180){
    do{ 
      delay(50);//Inserting this delay reduces the number of '0's from the sonar.
      duration = ping.readDistance();
      inches = ping.microsecondsToInches(duration);        
      if (inches < 15)
      {  //If something is detected within 15 inches...
        //Take a picture!      
      }
      else 
      {  //Otherwise keep turning left.
        pos--;
        myservo.write(pos);
      }
    }while (pos > 0);
  }
}

The first thing that I would do is to tidy up your code to make it easier to read. Put each curly bracket on its own line and then use AutoFormat. Secondly, consider using the NewPing library http://playground.arduino.cc/Code/NewPing

Wow, this NewPing library works a heck of a lot better than the old one! I really appreciate your input, it works without a hitch now! Thank you very much!

I think it's very important to note for the record: the '0' reading was actually caused by echoing in my own room. As soon as I took it outdoors it worked like a charm. Physics...