controling 4 servos at the same time easily

OK , i have built an arduino biped and im playing around with the programing but i have hit a brick wall, here is my code (below, its just to see if i can get the servos moving , no gait yet) , i was disappointed to find only one servo moved , it turns out you can only control a maximum of 2 servos on pin 9 , 10 .

so my question is how can i control 4 servos with a, "servo 1 goto 110 degrees , servo 2 goto 120degrees, repeat that infinitely " format , cuz that's about all i can handle at the moment , im pretty new to this and it took me a day of sitting down and looking at the language to program just this.

#include <Servo.h> 

Servo a;
Servo b;
Servo c;
Servo d;


void setup() 
{ 
  
   a.attach(5);  
   b.attach(6);
   c.attach(9);
   d.attach(11);
} 

void loop() 
{ 
      
  a.write(110);                  
  delay(150); 
  a.write(10); 
  delay(150);
  b.write(110);
  delay(150);
  b.write(10);
  delay(150);
  c.write(110);                  
  delay(150); 
  c.write(10); 
  delay(150);
  d.write(110);
  delay(150);
  d.write(10);
  delay(150);

}

There is a library [u]here[/u] that will drive up to 8 servos. Note that the argument is microseconds, so you write anyting from around 750 to 2250 microseconds depending on the servo instead of 0 to 180.

here is your code modifed to use ServoTimer2

#include <ServoTimer2.h>  // the servo library

ServoTimer2 a;
ServoTimer2 b;
ServoTimer2 c;
ServoTimer2 d;


void setup()
{

   a.attach(5);
   b.attach(6);
   c.attach(9);
   d.attach(11);
}

void loop()
{

  a.write(2000); 
  delay(500);
  a.write(1000);
  delay(500);
  
  b.write(2000);
  delay(5000);
  b.write(1000);
  delay(500);
  
  c.write(2000);
  delay(500);
  c.write(1000);
  delay(500);
  
  d.write(2000);
  delay(500);
  d.write(1000);
  delay(500);
}

I have increased the delay between movements, not because of the library, but most servos wont move very far in 150 milliseconds. You can experiment with your servos to see how much delay you actually need.

Good Luck!

I really cant thank you enough , but just one question when i previously have tried those different libraries even though i put them in the right place it still cant find them :frowning:

If the libraries go in the right place and you include the library header file it will find them.

If you have Arduino version 0012 installed, find the directory where the other libraries (like servo) are installed. Copy the code for ServoTimer2 in a new directory along side these.

You should have a bunch of directories including:
?hardware\libraries\Ethernet
?hardware\libraries\Servo
?hardware\libraries\ServoTimer2
?hardware\libraries\Wire

The ServoTimer2 directory should have the files:
ServoTimer2.cpp
ServoTimer2.h

After the files are in the directory, start up the arduino development environment and create your test sketch, select 'sketch\Import Library' from the IDE menu and select ServoTimer2. This should place the line #include <ServoTimer2.h> in the beginning of the sketch.

Good luck!

Thanks for all the help mem , heres some early photos of the build so far , made great progress today :slight_smile: if i can get the code working , it is capable of working.

heres the front

heres the back (its a seeedunio by the way )

and here is a close up of the legs

ill start a new thread about it when its done , don't mind the crap in the first picture , and my typos on the tags ::).