Help: Arduino and Ultrasonic ranger...

I commented, my problem is the following, found in a kind of sensor network ping))) (parallax) but for home, I did everything right, the issue is that now I have the remotest idea how to read well data because the data read but not one close to what it should be. What's more, is a source code with this finding, but is aimed at PIC, and try to adapt it manually, to run on the arduino but nothing happens ...

I would highly appreciate if someone can help ... I leave the link to the page where I got this invention and the code I'm using to make it work ...

#define PULSELEN 250
#define time 25
const int pinLed = 13;
const int pwm = 6;
const int pulsePin = 5;
int pulse = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pwm, OUTPUT);
  pinMode(pinLed, OUTPUT);
}

void loop()
{

  long inches, cm;
  int prom;
  func_pwm(); //LLamo al funcion PWM.
  
  prom = func_lectura(); // Llamo a la funcion LECTURA.
  
  prom= (prom *34 * 2)/250;
  Serial.println("");
  Serial.print("promedio bien");
  Serial.print(prom);
  Serial.println("");

  Serial.println();
  
  delay(100);
}
void func_pwm(){ //PWM que se encarga de generar un tren de pulsos de 250us con 40Khz de frecuencia.
  int av=0;
  do{
  digitalWrite(pwm, LOW);
  delayMicroseconds(time);
  digitalWrite(pwm, HIGH);
  delayMicroseconds(time);
  digitalWrite(pwm, LOW);
  delayMicroseconds(1);
  av++;
  }while(av!=PULSELEN);
  
}
long func_lectura(){
  int i, aux=0, buffer[10];
  for(i=0;i<10;i++){
    buffer[i]= analogRead(pulsePin);
    aux = aux + buffer[i];
  }
  Serial.print("Valor almacenado en el Buffer5: ");
  Serial.print(buffer[5]);
  Serial.println("");
  aux = (aux / 10);
  delay(1000);
  Serial.print (" Promedio ");
  Serial.print(aux);
  Serial.println("");
  return aux * 5;
}

Sorry my English

I think func_pwm outputs 20 KHz instead of 40 KHz. There is a total of 50 us from one peak to the next which gives 20 KHz.

I think the terminating condition in func_pwm is wrong. 250 pulses are generated instead of 250 us of pulses.

I suggest something like this for generating the pulse...

void func_pwm(){ //PWM que se encarga de generar un tren de pulsos de 250us con 40Khz de frecuencia.
  int av=0;
  do{
  digitalWrite(pwm, HIGH);
  delayMicroseconds(12);
  digitalWrite(pwm, LOW);
  delayMicroseconds(13);
  av++;
  }while(av!=10);

}

Do you have an oscilloscope you can use to ensure the generated pulse is correct?

Thx for the answer, if I succeed in getting this working. I need not spend much money on a PING))).

With regard to the oscilloscope:
Now i haven't got one, but in this week i will tray to get one to probe this...

Sorry my bad English.

Thx