motion sensor using parallax PING

Hi all

I'm trying to write a very simple program to activate a relay when a parallax PING sensor detects something within a certain distance of it

I cut and paste the PING program written to return distance by David A. Mellis (http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor) and lopped the end off (the part that converts time into distance) I wrote a simple if/then statment that states if the time returned by the sensor is greater than a certain value it turns the relay on, if it gets no value it turns the relay off.

The program works in that when you put your hand in front of the sensor it turns the relay on, but it also turns the relay on EVERY time the sensor 'pings', no matter if your hand is in front of it or not. The result is a cycling of the relay about once a second, and it will stay on when your hand is in front of it.

Any idea why it is cycling every time the sensor pings?

Thanks!

const int pingPin = 7; //pin used to signal sensor
const int relayPin = 13; // pin connected to relay

void setup()
{
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{

// establish variables for duration of the ping,

long duration;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): 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.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

if (duration >= 1000) //triggers relay from 8in - 2ftish
{
digitalWrite(relayPin, LOW);

}

else
{
digitalWrite(relayPin, HIGH);
}
}

I can't see anything wrong with it; put some serial.prints in there to see what values you're getting for duration - may give some clues.