it's a little hard to answer you question with so little information about your code.
Did the example sketch for the servo library work?
Did you try adding print statements to that example servo sketch and were the values as expected?
Did you try a simplified version of your sketch (perhaps removing the smoothing functions to keep the code as simple as possible) to see if that works as expected?
#include <Servo.h>
Servo myservo; // max. 8 servos
#define NUMREADINGS 10
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int lastValue = 0;
int inputPin = 1;
void setup()
{
Serial.begin(9600);
myservo.attach(10);
myservo.write(180);
for (int i = 0; i < NUMREADINGS; i++){
readings[i] = 0;
}
}
void loop()
{
total -= readings[index]; // subtract the last reading
readings[index] = analogRead(inputPin); // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS){ // if we're at the end of the array...
index = 0; // ...wrap around to the beginning
}
average = total / NUMREADINGS; // calculate the average
Serial.println(average); // send it to the computer (as ASCII digits)
myservo.write(average);
}
somehow i think the problem is more in at the electronics. Since when i unplug the power wire of the servo it prints out the clean data. could it be that the servo distracts the sensor?
You don't say which servo library you are using. The comment in the code about 8 servos implies you are using the old software servo library. But your sketch is coded for the new servo library that is released with Arduino version 0012. If using the old library then you are writing to the servo too quickly – see Arduino Playground - Servo
If using the hardware library (Servo - Arduino Reference) then its still possible that your servo is drawing too much power from your supply. Servos can draw anything up to an amp or so depending on the servo and the load.
yeah, there is no problem with servo or the proximity sensor working alone. only when they are togehther hooked up. now i used the very useful link from Grumpy_Mike above and it works much better. even though not perfectly fine.