HC-SR04 Ultrasonic Sensor...Out of Range

Hi,
I am currently trying to get my ultrasonic sensor to work using the NewPIng Library.
The sensor is working fine as I can use a simple sketch to get distance measurement. This works ok but for my application I need more accurate and reliable distance measurements.
My problem arises when I use the NewPing library. It is installed correctly.
I am using a sketch which I copied from the DroneBotWorkshop but I get ‘Out of Range’ whatever I do. I included a couple of extra lines to see if I was getting anything from the sensor which I am not.
Here’s the code:

// This uses Serial Monitor to display Range Finder distance readings

// Include NewPing Library
#include “NewPing.h”

// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm

#define trigger_pin 10
#define echo_pin 13
#define max_distance 400

NewPing sonar(trigger_pin, echo_pin, max_distance);

float duration, distance;

void setup() {
Serial.begin (9600);
}

void loop() {
delay(50);
duration = sonar.ping();

// Determine distance from duration
// Use 343 metres per second as speed of sound

distance = (duration / 2) * 0.0343;

// Send results to Serial Monitor
Serial.print(“Duration = “); //My extra lines to see if I was getting any data from the sensor
Serial.print(duration); //
Serial.print(” Distance = “);
if (distance >= 400 || distance <= 2) {
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(” cm”);
delay(500);
}
delay(500);
}

As I said, I get “Out of Range” and 0.00 for duration.
I have looked all over the www for a solution…Please Help Me!!

Make sure your wiring is correct.
Use code tags when posting code.
Get a known-good sensor, try with that.
And don't forget to use code tags. Much better than green.

Thanks for the quick reply.
As I said, the sensor works with a simple sketch. The wiring is correct as the sensor works with a simple sketch.
It's when using the NewPing library I don't get any results.
Sorry about the code tags, when I copy and paste the code it comes out all black, I just changed the colour to make it easier to distinguish from my ramblings! How do I post code with all the right colours then?

SOLVED:
I changed the pins the Arduino was connected to the sensor and it worked fine.
I have no idea why that should be so as the board seems to be working fine.

Code tags and so: it's all in the sticky.

Looking at your code again it's obvious. Pin 13 has the built-in led connected as well which interferes with the echo signal.