Hola a todos,
tengo un problema con el siguiente codigo:
/*
Radar Arduino v1.0
Autor: FjRamirez
Email: email@tuelectronica.es
Fecha: 15/04/2013
*/
#include <Servo.h> // Añade la libreria Servo
Servo leftRightServo; // Crea una variable servo
int leftRightPos = 0; // Variable para la posición del servo (grados)
const int numReadings = 1; // Numero de lecturas en cada posición
int index = 0; // Contador para el número de lectura
long total = 0; // the total of all readings
int average = 0; // the average
int echoPin = 53; // the SRF05's echo pin
int trigPin = 51; // the SRF05's init pin
long duration = 0; // variable for reading the pulse
int distance = 0; // variable for storing distance
/* setup the pins, servo and serial port */
void setup() {
leftRightServo.attach(12,700,2400);
Serial.begin(9600); // Establece la velocidad de datos del puerto serie
pinMode(trigPin, OUTPUT); // Establece pin como salida
pinMode(echoPin, INPUT); // Establece pin como entrada
digitalWrite(trigPin, LOW); // Pone el pin a un estado logico bajo
}
void loop() {
for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // De izquierda a derecha.
leftRightServo.write(leftRightPos);
for (index = 0; index<numReadings; index++) { // take x number of readings from the sensor and average them
// Pulso de 10us para inicial el modulo
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, 17400); //Devuelve la longitud del pulso del pin Echo en us (3metros maximo)
if (!duration){
duration=17400;
}
distance = duration/58; // convert to centimetres
total = total + distance; // update total
delay(100);
}
average = total/numReadings; // create average reading
total = 0;
Serial.print("X"); // print leading X to mark the following value as degrees
Serial.print(leftRightPos); // current servo position
Serial.print("V"); // preceeding character to separate values
Serial.println(average); // average of sensor readings
}
for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // De derechas a izquierda
leftRightServo.write(leftRightPos);
for (index = 0; index<numReadings; index++) { // take x number of readings from the sensor and average them
// Pulso de 10us para inicial el modulo
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, 17400); //Devuelve la longitud del pulso del pin Echo en us (3metros maximo)
if (!duration){
duration=17400;
}
distance = duration/58; // convert to centimetres
total = total + distance; // update total
delay(100);
}
average = total/numReadings; // create average reading
total = 0;
Serial.print("X"); // print leading X to mark the following value as degrees
Serial.print(leftRightPos); // current servo position
Serial.print("V"); // preceeding character to separate values
Serial.println(average); // average of sensor readings
}
}
Es un sensor de ultrasonidos con un servo, y hace un barrido de 180º para medir distancias. El caso que cuando conecto el servo no marca los valores de distancia correctamente. No esta montado el sensor en el servo, lo pongo para medir una distancia fija, y sin conectar el pin de datos del servo, me mide correctamente, pero cuando conecto el pin de datos del servo, hay errores de una manera sistematica:
Sin el servo conectado:
X3V174
X4V174
X5V174
X6V174
X7V174
X8V174
X9V174
X10V174
X11V174
X12V174
X13V174
Con el servo conectado:
X5V175
X6V137
X7V175
X8V135
X9V175
X10V137
X11V174
X12V134
X13V174
X14V134
X15V174
X16V135
X17V174
X es la posición del servo, y V la distancia en cm (175-174), como veis, se va alternando una medida incorrecta con otra correcta.
He probado con varios servos y nada, hace lo mismo. No entiendo el porque... Alguna sugerencia¿?¿?¿?