Problema con mi sonar [Solucionado]

Tengo un LV-MaxSonar-EZ3 y no sé si estoy haciendo algo mal pues no me devuelve ningua lectura coherente.

Si sigo el ejemplo

//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09

//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;
void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
}
void loop() {
pinMode(pwPin, INPUT);
//Used to read in the pulse that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per Inch.

pulse = pulseIn(pwPin, HIGH);
//147uS per inch
inches = pulse/147;
//change inches to centimetres
cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(500);
}

Conecto el +5v del arduino al del maxsonar, el gnd del arduino y el maxsonar y el PW al pin 7 del arduino y las distancias que me devuelve son o cero o cualquiera menos la real. He probado con leer el pin An (de la placa del maxsonar) con con el otro codigo de la misma página (Arduino Playground - MaxSonar) pero con identicos resultados.

Declaro que tengo roto el arduino o el Sonar? Estoy conectando algo mal ?

ese codigo no lo entiendo del todo, prueba este

const int pingPin = 7;

long pulse, inches, cm;


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


void loop() {

    long duration;
    int cm;
    
 pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(20);
  // 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);

  
  cm = duration / 29 / 2;

Serial.println(cm);

igual no compila pero lo basico esta ahi y en mi proyecto funciona, he tenido que quitar todo lo relacionado con mi proyecto que no te afecta

Mil gracias funciono !! con el pin 6. No es que sea una maravilla de la "tesnica" pues no siempre mide bien en un rango [0,70cm] pero en 14,18 cm si lo hace. Ahora seguiré profundizando sobre el tema.

Gracias por tu ayuda!!!!! pues dan ganas de seguir trasteando con este ingenio.

me alegro que haya funcionado, recuerda cambiar el titulo de este post y añadirle SOLUCIONADO

para las lecturas erroneas puedes usar un filtro modal, es decir lees por ejemplo 8 veces la distancia y te quedas con el valor que mas se repite.