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:
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.
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.
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.
@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.
@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).
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?
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?
//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);
}
}