Arduino Multi Servo Control

Program Download:

Arduino Sketch
multi_servo.ino

The Software
           There are two main portions of code that you'll see explained and in detail below:
    -Initialization
    -Servo Control


           The first part of the code shows the initializations for Servo #0 and Servo #1. After they have be setup, we can write values to them and make the do our bidding! Note that I will name them accordingly, in the schematic we called them Servo0 and Servo1. In the software we will do exactly the same so we don't create any confusion.

Initialization

------------« Begin Code »------------
#include <Servo.h>
 
Servo servo_0;
Servo servo_1;
 
unsigned int pos[] = {10,90,170,90};    // variable to store the servo position 
 
void setup() 
{ 
  servo_0.attach(0);  // attaches the servo on pin 0 to the servo object 
  servo_1.attach(1);  // attaches the servo on pin 1 to the servo object 
} 
..
...
------------« End Code »------------

           After the intializations are complete, we can move onto performing synchronous and asynchronous control of the servo motors. The software below shows examples of the two types of control of the servo motors. Nothing complex, just moving servos to specific angles and holding (the synchronous control is commented out for this example).

Main Loop

------------« Begin Code »------------
...
..
void loop() 
{
  
//Individual Servo Control
for(int i=0;i<=170;i++){
  servo_0.write(i);
  delay(25);  
  
  servo_1.write(170-i);
  delay(25);  
}

for(int i=170;i>=0;i--){
  servo_0.write(i);
  delay(25);  
  
  servo_1.write(170-i);
  delay(25);  
}

/*
//Synchronous Servo Control
for(int i=0;i<4;i++){
  servo_0.write(pos[i]);
  servo_1.write(pos[i]);
  delay(2000);  
}
*/

}
------------« End Code »------------

           If you're an experience Arduino-er you're probably chuckling at how simple the code is, but truth be told, most microcontrollers don't have such nice sets of libraries allowing for short programs and easy servo control, so we should relish the shortness of our program and the accuracy that it provides. Now, let's test this software out!