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