Help controlling 5 servos independtly with pots

Hello everyone.

I made an arduino sketch to control 5 servos independently via potentiometers, actually im using 2 servos to test it, but when i move the potentiometer both servos spin, the same with the other pot.

I proved with one servo, ex. im using the digital output 9 for the servo signal, and the analog input 0 for the pot and works fine, then i move the servo signal wire to the digital pin 8 with the same analog input (0) and the servo control with the pot still works, but what i want is that the dig. output 8 only works with the analog input 1, and the same with the other servos. Please help me with this problem. Thanks.

I also have the problem that the servo motors move sligthly erratically, im using an external suply 5v 1a. for the 2 testing servos.

kkk.ino (939 Bytes)

Post your code per #7 below so more people will look at it. Make sure the arduino and servo power supply grounds are connected together. Bottom is some servo/pot test code that might be of use.

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing, adjust as needed
}

zoomkat:
Post your code per #7 below so more people will look at it. Make sure the arduino and servo power supply grounds are connected together. Bottom is some servo/pot test code that might be of use.

Here's the code im using. Both grounds are connected.

#include <Servo.h>

Servo ServoBase;
Servo ServoHombro;
Servo ServoCodo;
Servo ServoMano;
Servo ServoPinza;

int pinPot = 0;
int val0;

int pinPot2 = 1;
int val1;

int pinPot3 = 2;
int val2;

int pinPot4 = 3;
int val3;

int pinPot5 = 4;
int val4;

void setup() {

ServoBase.attach(9);
ServoHombro.attach(8);
ServoCodo.attach (7);
ServoMano.attach(6);
ServoPinza.attach(5);

}

void loop(){
val0 = analogRead(pinPot);
val0 = map(val0, 0, 1023, 0, 180);
ServoBase.write(val0);
delay(15);

val1 = analogRead(pinPot2);
val1 = map(val1, 0, 1023, 0, 180);
ServoHombro.write(val1);
delay(15);

val2 = analogRead(pinPot3);
val2 = map(val2, 0, 1023, 0, 180);
ServoCodo.write(val2);
delay(15);

val3 = analogRead(pinPot4);
val3 = map(val3, 0, 1023, 0, 180);
ServoMano.write(val3);
delay(15);

val4 = analogRead(pinPot5);
val4 = map(val4, 0, 1023, 0, 180);
ServoPinza.write(val4);
delay(15);

}

zoomkat:
Post your code per #7 below so more people will look at it.

r4mmz:
Here's the code im using.

If you follow the link zoomkat provided and read section #7 you can learn to post code correctly (as zoomkat posted his).

This makes it easier for us to both read the code and to try it ourselves. You can edit your post and add the code tags.

Edit: Another advantage to using code tags is you don't end up with a smiley wearing sunglasses "8)" whenever you type "8``)".

Thnks :disappointed_relieved: :smiling_imp:

#include <Servo.h>

Servo ServoBase;
Servo ServoHombro;
Servo ServoCodo;
Servo ServoMano;
Servo ServoPinza;

int pinPot = 0;
int val0;

int pinPot2 = 1;
int val1;

int pinPot3 = 2;
int val2;

int pinPot4 = 3;
int val3;

int pinPot5 = 4;
int val4;


void setup() {
 
ServoBase.attach(9);
ServoHombro.attach(8);
ServoCodo.attach (7);
ServoMano.attach(6);
ServoPinza.attach(5);

}

void loop(){
 val0 = analogRead(pinPot);
 val0 = map(val0, 0, 1023, 0, 180);
 ServoBase.write(val0);
 delay(15);
 
 val1 = analogRead(pinPot2);
 val1 = map(val1, 0, 1023, 0, 180);
 ServoHombro.write(val1);
 delay(15);
 
 val2 = analogRead(pinPot3);
 val2 = map(val2, 0, 1023, 0, 180);
 ServoCodo.write(val2);
 delay(15);
 
 val3 = analogRead(pinPot4);
 val3 = map(val3, 0, 1023, 0, 180);
 ServoMano.write(val3);
 delay(15);
 
 val4 = analogRead(pinPot5);
 val4 = map(val4, 0, 1023, 0, 180);
 ServoPinza.write(val4);
 delay(15);


}

Thanks for using code tags.

r4mmz:
I also have the problem that the servo motors move sligthly erratically, im using an external suply 5v 1a. for the 2 testing servos.

What kind of servos are these two servos?

Your 1A supply might not be enough to keep one servo happy and free from jitter.

I've found servos and motors in general tend to behave a lot better when powered from a good battery pack. I often have trouble with my motor projects when I attempt to power then from an AC to DC power supply. A good AC to DC supply should be enough to power a couple servos but keep their limitation in mind if you attempt to use them with a bunch of servos.

Servos often surprise people with how much current they can draw as they start to move. This current spike can be very short and often can't be detected with a multimeter. Servo jitter is very frequently caused by power supply problems.

The pots in servos wear out as the pot's wiper moves back and forth. A bad pot is another common cause of servo jitter. If a servo stops jittering when moved to a different position, then the chances are good the problem is a bad pot. The jitter often starts in the "home" position. The home position is often the center position.

DuaneDegn:
Thanks for using code tags.

What kind of servos are these two servos?

im using the towerpro's mg995

zoomkat:
Post your code per #7 below so more people will look at it. Make sure the arduino and servo power supply grounds are connected together. Bottom is some servo/pot test code that might be of use.

i already used that code but still the same problem, the pot that is supposed to control only the first servo also move the second servo

I do not understand that you say that you can not get analog input 1 to only control servo on pin 8 ... which it clearly does in the code. That code is too simple to fail. So you are looking at a hardware problem.

Post your wiring. If nothing else, just draw it on paper, take a picture and upload that, but drawing it in some (simple) drawing program would be nice. If you have not yet mounted the servos in the robot arm (servo names suggest that is what you are building), then take a picture of the Arduino/servo wiring that you have made so far.

By the Way, I suggest you edit code so instead of "pinpot2" you call it "pinHombre" and so on, then it becomes clearer like

valHombro = analogRead(pinHombro);
 valHombre = map(valHombro, 0, 1023, 0, 180);
 ServoHombro.write(valHombro);
 delay(15);

so you never need to remember if "3" was the Mano or Pinza

What happens if you tidy up your code like this

void loop(){
 val0 = analogRead(pinPot);
 val0 = map(val0, 0, 1023, 0, 180);
 
 val1 = analogRead(pinPot2);
 val1 = map(val1, 0, 1023, 0, 180);
 
 val2 = analogRead(pinPot3);
 val2 = map(val2, 0, 1023, 0, 180);
 
 val3 = analogRead(pinPot4);
 val3 = map(val3, 0, 1023, 0, 180);
 
 val4 = analogRead(pinPot5);
 val4 = map(val4, 0, 1023, 0, 180);
 
 
 ServoBase.write(val0);
 ServoHombro.write(val1);
 ServoCodo.write(val2);
 ServoMano.write(val3);
 ServoPinza.write(val4);
 delay(15);
}

Also, for testing, I suggest you increase the delay() to 200 (or more) to make sure the servos have plenty of time to move. Then you can try shortening the time gradually.

...R

zoomkat:
Post your code per #7 below so more people will look at it. Make sure the arduino and servo power supply grounds are connected together. Bottom is some servo/pot test code that might be of use.

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat multi pot/servo test 3-23-13

//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h>
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup()
{
  Serial.begin(9600); 
  myservo1.attach(2); 
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo"); 
}

void loop()
{
  newval1 = analogRead(potpin1);         
  newval1 = map(newval1, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){ 
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

newval3 = analogRead(potpin3);         
  newval3 = map(newval3, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){ 
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

newval4 = analogRead(potpin4);         
  newval4 = map(newval4, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){ 
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

newval5 = analogRead(potpin5);         
  newval5 = map(newval5, 0, 1023, 0, 179);
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){ 
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  }
  delay(50);  //to slow loop for testing, adjust as needed
}

Way too big, and the use of arrays would eliminate the cut-and-paste bugs :wink:

Give a man a fish . . . and food-poisoning. :smiley:

Groove:
Way too big, and the use of arrays would eliminate the cut-and-paste bugs :wink:

Give a man a fish . . . and food-poisoning. :smiley:

If I have copy paste errors, let me know so I can correct. A lot of people suggest making this type of code using looping code and iterations because it looks tighter. It has been my experience that in line coding allows for easier tweaking for the individual servos/pots being used. Small servos like the 9g ones may need a different control band to prevent stripping the gears as opposed to the requirements for a large metal gear servo. YMMV

newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);

Etc, etc.
Short code gives the bugs fewer places to hide in.

It has been my experience that in line coding allows for easier tweaking for the individual servos/pots being used.

That's odd - it's been my experience that putting all the tunings in one place facilitates simple code modifications, and reduces the incidence of easily avoidable bugs.

That's odd - it's been my experience that putting all the tunings in one place facilitates simple code modifications, and reduces the incidence of easily avoidable bugs.

Thanks for the copy/paste error feedback! I find tracking variables in looping iterations might not be friendly for trouble shooting initial code if one is not familiar with these things. If one is so inclined, one can modify the final working development code into a different format if one desires.