Pages: [1]   Go Down
Author Topic: analogRead and Servo don't work together  (Read 935 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!

« Last Edit: January 20, 2009, 06:03:55 am by F1_Champion » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 20, 2009, 06:19:31 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is basicially the same as in the example
Code:
#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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need the external circuit decoupling:-

http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
Logged

Sheffield --- UK --
Offline Offline
Sr. Member
****
Karma: 0
Posts: 377
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 20, 2009, 09:46:25 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which library would you recommand?

i got confused and probably mixed them up.
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are only driving one or two servos, use the library that comes with 0012. check the source code for this comment:
Code:
/*
   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.
« Last Edit: January 20, 2009, 10:11:18 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes.

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: