Go Down

Topic: analogRead and Servo don't work together (Read 970 times) previous topic - next topic

Jan 20, 2009, 12:03 pm Last Edit: Jan 20, 2009, 12:03 pm by F1_Champion Reason: 1
Hi,

i am using this code ( http://arduino.cc/en/Tutorial/Smoothing ) to read out a proximity sensor, then i want to pass that values on to a servo.
Code: [Select]

myservo.write((int)average);


When i print the values without the servo attached, they are pretty clean, but as soon the servo is plugged they are beeing totaly messed up:

With Servo:
102
94
88
77
80
86
84
89
75
82
71
83
89


Without Servo:
150
150
150
150
150
150
150
150
150
150
150
151


What might be the cause for that?

Thanks!


mem

#1
Jan 20, 2009, 12:18 pm Last Edit: Jan 20, 2009, 12:19 pm by mem Reason: 1
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?

The code is basicially the same as in the example
Code: [Select]

#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?


peter247

how are you powering the servo direct from the board or via buffering ?

http://peter224722.blogspot.com

The Servo as the proximity sensor are both powered over the 5V pin of the board, which is powered by a external power supply.

mem

#6
Jan 20, 2009, 03:45 pm Last Edit: Jan 20, 2009, 03:46 pm by mem Reason: 1
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 http://www.arduino.cc/playground/ComponentLib/Servo

If using the hardware library (http://arduino.cc/en/Reference/Servo) 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.

Good luck.

which library would you recommand?

i got confused and probably mixed them up.

mem

#8
Jan 20, 2009, 04:10 pm Last Edit: Jan 20, 2009, 04:11 pm by mem Reason: 1
If you are only driving one or two servos, use the library that comes with 0012. check the source code for this comment:
Code: [Select]
/*
  Servo.h - Hardware Servo Timer Library

The sketch code you posted should work with that library if you can get the electrics sorted.

Good luck.

ok, thanks i guess i was using that library as well, it only works on pin 9, 10 right?

mem

Yes.

Did you try the example sketches for that library and did they work?

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.

Go Up