Controlling 3 servo motors using push buttons

I have 3 servo motors and 6 push buttons, 2 for each servo to control them. I tried my code in the simulator(123d.circuits.io) and got the correct output. But when tried it practically my 3 motors together behave in a vague manner and are giving lot of jerks.
The code i used was:

#include <Servo.h>

Servo myservo,myservo1,myservo2;
int button7=0;
int button6=0;
int button5=0;
int button4=0;
int button3=0;
int button2=0;
int pos=180;
int pos1=180;
int pos2=0;

void setup()
{
pinMode(7, INPUT);
pinMode(6, INPUT);
  pinMode(5, INPUT);
pinMode(4, INPUT);
   pinMode(3, INPUT);
pinMode(2, INPUT);
myservo.attach(9);
myservo1.attach(10);
  myservo2.attach(11);
  myservo.write(pos);
  myservo1.write(pos1);
  myservo2.write(pos2);
}

void loop()
{
button7=digitalRead(7);
button6=digitalRead(6);
  button5=digitalRead(5);
button4=digitalRead(4);
   button3=digitalRead(3);
button2=digitalRead(2);

delay(5);
if(button7==1 && button6==0)
{
  
  pos=constrain(pos,0,180);
   myservo.write(pos);
 
pos++;
}

if(button7==0 && button6==1)
{
  pos=constrain(pos,0,180);
   myservo.write(pos);
  
pos--;
}
  if(button5==1 && button4==0)
{
    pos1=constrain(pos1,0,180);
    myservo1.write(pos1);
  
pos1++;
}

if(button5==0 && button4==1)
{
  pos1=constrain(pos1,0,180);
  myservo1.write(pos1);
  
pos1--;
}

if(button3==1 && button2==0)
{
  pos2=constrain(pos2,0,180);
   myservo2.write(pos2);
 
pos2++;
}

if(button3==0 && button2==1)
{
  pos2=constrain(pos2,0,180);
   myservo2.write(pos2);
  
pos2--;
}
}

The circuit looked like(resistor=100 ohm):

Iam trying to control these servos to build my manipulator for my robot. Please help ASAP.

How are you powering the servos? The picture looks like you expect the Arduino to provide the power. It can’t.

We are going to use two 9v power supply to power 3 servos

We are going to use two 9v power supply to power 3 servos

No, you aren't. You are going to use proper voltages - to match the servos.

so how much power should we use according to you to control 3 servos

What kind of servos are they? What are they going to be doing? Typically, servos operate at 6V, and require up to 1A each. No wimpy little 9V battery, if that's what you were thinking of, is going to provide the required amperage, and the voltage is wrong. Or maybe not. It depends on your unnamed servos and needs.

Servo specifications: Operating Voltage : 4.8-6.0V

PWM Input Range : Pulse Cycle 20±2ms, Positive Pulse 1~2ms

STD Direction : Counter Clockwise / Pulse Traveling 1500 to 1900µsec

Stall Torque : 6 kg-cm (82.6 oz/in) at 4.8V, 7.1 Kg-cm (97.3 oz/in) at 6V

Operating Speed : 0.18 sec/ 60° at 4.8V, 0.16 sec/ 60° at 6V at no load

Weight : 40g (1.41 oz)

Size : 41.3*20.3*38.7*48.5*10

Plug Available : FUT, JR

Special Feature : Heavy Duty Plastic Gears, Economy Servo

And we have 3 servos like this so how much power supply we will be needing. And is there any issues with the code?

If I ask two questions, you are supposed to supply two answers.

That data tells you that your proposed 9V power supply is wrong.

As for the code, it doesn't matter until you are properly powering the servos.

What you have now is like trying to start your car with 2 D cell batteries. The fact that you have the ignition switch and starter motor wired correctly, and that you have set the parking brake doesn't matter. The car is not going to start.

The pic below shows how to attach external power to servos and link the grounds. As PaulS said, in the absence of any data to the contrary, one should budget 1A per servo so if your are likely to be maxing out at the same time, you’ll need 3A. And the voltage required is 4.8 to 6, so not sure what you were hoping to achieve with 9?

Best way to see what current you need is to hook a servo up with your ammeter in series with the power supply. Once the servo has settled in a position, try to move the servo and you’ll see the current climb as it fights you.

edit… These symptoms are often associated with poor power:

vaab:
my 3 motors together behave in a vague manner and are giving lot of jerks.

servo power.png

vaab: And we have 3 servos like this so how much power supply we will be needing. And is there any issues with the code?

A 4.8 to 6v power supply that can provide about 3 amps - 1 amp for each servo.

Since you have full control over the values of pos there is no need to use the constrain() function.

Your code would be much easier to follow (for you) if you give meaningful names to things - for example servo3buttonUPpin and servo3buttonDOWNpin (or maybe it should be -LEFT_ and -RIGHT- and maybe there are better names for the servos than myServo1, myServo2 etc. Then, for example you could have

servo3UP = digitalRead(servo3buttonUPpin);

Although it has nothing to do with the functionality of the code you could make life easier by using arrays to hold the servo and button data.

Make sure your code works with a single servo before you try 2 and 3.

...R

@PaulS
Sorry for not answering second question.
We want to control direction of servo motor using two push buttons and like that we have 3 servos with 6 push buttons.

@Robin2
So we have to use 6 v power supply with enough current to power 3 servos?
And code is working with one servo.

vaab: @Robin2 So we have to use 6 v power supply with enough current to power 3 servos?

JimboZA: one should budget 1A per servo so if your are likely to be maxing out at the same time, you'll need 3A. And the voltage required is 4.8 to 6

We want to control direction of servo motor using two push buttons and like that we have 3 servos with 6 push buttons.

Why? A servo dancing around doing nothing is fun for 5 seconds. After that, it's boring. A servo DOING something is useful.

What are your servos going to do? What kind of torque do they need to produce? You can't properly size a power supply until you know that.

@PaulS We Want to produce torque of 6kg.cm. When i press one push button, the servo will move in one direction & when i press another push button it will movie in another direction.Like that i have 3 servos with 6 push buttons, 2 for each(i am using this servos for robotic arm).

We Want to produce torque of 6kg.cm.

That is the stall current (the maximum that that servo can manage) at 4.8V. So, you'll need to supply more than 4.8V but not more than 6V to get that torque. There is no information in what you provided to say how much current you need, so you should plan on 1A per servo.

And, really, if you need that much torque, those are the wrong servos to be using. They will be operating at the upper end of their capabilities, and will not last as long as a lazy servo, hardly breaking a sweat, would.

Now i am clear with the power supply. but is there any issue with the code? Lets say i manage to supply enough power to servo, is this code is ok to get what i want?

Now i am clear with the power supply. but is there any issue with the code? Lets say i manage to supply enough power to servo, is this code ok to get what i want?

but is there any issue with the code?

Connect the proper power and then tell us. You have the hardware. We don't.

Well...

A torque of 6 kg cm is 0.6Nm. (T)

The rotation of 60 degrees / 0.18 seconds is near as damn it 6 radians / second. (w)

So the Power required is P=Tw = 3.6 Watts.

And since P=VI, the current I is 3.6/6 = 0.6A on a good day with no losses.

That's a sort of average figure though, and I guess wouldn't include the stall current... so yeah probably 1A as a budget is not far off the mark. Maybe a bit low though? 1.5?

You'll need to measure it.

Some simple two button servo test code.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}