Using MaxSonar WR1 rangefinder sensor with Arduino

I am struggling to use the MaxSonar-WR1 ultrasonic rangefinder with the Arduino Duemilanove (328 chip) and wonder if anyone can provide some tips to the uninitiated

Following the datasheet www.maxbotix.com/uploads/LV-MaxSonar-WR1-Datasheet.pdf,
I decided to use pulse width output, pin 2, output....since I was familiar with PW output on the Parallax Ping))) ultrasonic rangefinder. Power was provided for the WR1 from a benchtop power supply at 5.0V and the ground of the WR1 was connected to the Arduino ground.

I borrowed code from the code I had used with the Ping))) sensor, removing the pulse generating code which the WR1 doesnt need and adjusting the pulse width:distance setting to 147uS/inch:

int pingPin = 7;

void setup()
{
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;

pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH);

// convert the time into a distance

inches = microsecondsToInches(duration);

Serial.print(inches);

Serial.print("in, ");

Serial.println();

delay(100);

}

long microsecondsToInches(long microseconds)

{

return microseconds / 147/ 2; // MaxSonar says use 147uS/inch in calculating
distance.
}

When I read the values output on the Arduino serial monitor, its showing 5” regardless of where I point the sensor. When I put my hand close to the face of the sensor (this is an unreadable distance for this sensor) it reads 20+”.

I also tried using serial communication mode, connecting sensor pin 5 to arduino pin 0/RX, but I wasn't sure how to clear the code (listed above) from the Arduino memory ()so that I could hopefully just use the serial monitor.

Any advice in correcting the problem(s) with this gameplan would be greatly appreciated.

Just waiting to get mine back, but I just read in the analog voltage.

Somehow analogRead(uSoncPin); was just easier.

Thanks for the reply Brutus. I did try the analog output on the WR1 and it worked, although the results were disappointing. I was reading on the Trossen forum where others have found the MaxSonar sensors to be very "jumpy" with analog output, and they recommended filtering the output OR using serial output instead of analog OR switching to the Devantech sensors. I dont want to muddle my data by filtering, but the other options look worth pursuing.

Oversampling works fairly well here too.

Read the pin 10x and keeping adding values together.

Then Divide by 10.

We do this on temp sensors to get a more reliable reading.

analogRead has an execution time of 100uS, so even to read it 10 times you only spend 1mS. Acceptable trade-off for most applications.