trying to use two servos at same time

trying to use two servos at same time i want write code for line follower but cant get the two 360 degree
servos to both run at same time here is my code

#include <Servo.h>
Servo myservo2;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position
int pos2 = 0;

void setup() {
Serial.begin(9600);
myservo.attach(9);
myservo.attach(10);

}

void loop() {
for(pos = 0; pos < 180; pos += 1)
{
for(pos2 = 180; pos2 > 0; pos2 -=1)
{
myservo.write(pos);
myservo2.write(pos2);
delay(25);
}
}
}

that is what I have but only one servo runs the other does nothing at all ?

i want to port this to an attiny85 btw and some codewould be great if possible

trying to use two servos at same time i want to write code for a line follower but cant get the two 360 degree
servos to both run at same time here is my code

#include <Servo.h>
Servo myservo2;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position
int pos2 = 0;

void setup() {
Serial.begin(9600);
myservo.attach(9);
myservo.attach(10);

}

void loop() {
for(pos = 0; pos < 180; pos += 1)
{
for(pos2 = 180; pos2 > 0; pos2 -=1)
{
myservo.write(pos);
myservo2.write(pos2);
delay(25);
}
}
}

any code or suggestions would be great

Do you really want nested do-loops here? Every time you step one step on the outside loop, the other one must run the full range of its steps.

no Im trying to stay away from for loops also but you know ill do what ever Ive got to do to make this work Im making this for my boys when they get a little older

I don't think you can attach one servo to two pins.

You should be posting your code between 'code' tags, too. After 71 posts you should know that.

These nested loops are wrong

for(pos = 0; pos < 180; pos += 1)
{
   for(pos2 = 180; pos2 > 0; pos2 -=1)
  {
    myservo.write(pos);
    myservo2.write(pos2);
    delay(25);
  }
}

it should be (much simpler)

for(pos = 0; pos < 180; pos += 1)
{
    pos2 = 180 - pos;
    myservo.write(pos);
    myservo2.write(pos2);
    delay(25);

}

With nested loops one motor has to do all its stuff before the other motor makes its next move.

...R

After 71 posts you should know not to DOUBLE POST. I have already replied in your other Thread

...R

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

sorry people for double posting im not a pro at this

So did you fix the problem?

Where you had:-

myservo.attach(9);
myservo.attach(10);

You should have had:-

myservo.attach(9);
myservo2.attach(10);