Introduction: Arduino + 2 Servos + Mouse

About: Evo 8, Honda K, B Engines Mods
Here is a tutorial on how to make 2 servos move with a mouse using the Arduino board.
There is not a simple tutorial than this one and I have search the web looking for one with no result.
This one is simple and easy for everybody to follow. Enjoy! ;)
Dont forget to vote for me on the Arduino Challenge!!

Note: This tutorial is for those who are using the Arduino already. But If you haven't just yet with this tutorial you can.

Step 1: Materials

You will need the following: (not in a particular order)

1. Arduino Board (my version is UNO).
2. 2 servos Parallax (Radio Shack).
3. Web Cam (optional) to see the movement.
4. 2 rubber bands to hold the Webcam in position.
5. Bread Board to make any connections easy.
6. 1 cable tie
7. Something to hold the servos in place.
8. A computer.                  

1. Arduino Software (to upload the code to move the 2 servos).
2. Process Software (to move the servos using the mouse)

Step 2: Connections

Before you upload the code or use the Process sofware. You must connect
the servos using the picture.
Be advice that I label  the servos so I can hook them the right way.
Also this works when connecting the webcam  to them, you can see witch one
goes up & down and witch one goes right & left.
Follow the diagram for more help.
I have use a bread board to make the connections easy.
Connections: (each servo)
Black to GND
Red to 5V
Yellow or White to Analogs 0 and Analog 1
(Use the bread board to provide 5v to each servo)

Step 3: Arduino Software

After all the connections were made please double check them always.
Connect the Arduino Board to the computer. (USB cable)
Open the arduino Software
a) copy and paste the code in the arduino sketch
b) Always verify any code you upload, this saves you time.
c) After you verify the code, proceed to upload it to the Arduino board.
d) Close the Arduino Software.

Here is the code: ( I have tested this code several times before I pasted it here

//Arduino code:
#include <Servo.h>

Servo yservo;  Servo xservo; // servos for x and y
//set initial values for x and y
int ypos = 0;
int xpos= 0;

void setup(){
  xservo.attach(14); //(analog pin 0) for the x servo
  yservo.attach(15);  //(analog pin 1) for the y server
  Serial.begin(19200); // 19200 is the rate of communication
  Serial.println("Rolling"); // some output for debug purposes.

void loop() {
  static int v = 0; // value to be sent to the servo (0-180)
  if ( Serial.available()) {
    char ch =; // read in a character from the serial port and assign to ch
    switch(ch) { // switch based on the value of ch
      case '0'...'9': // if it's numeric
        v = v * 10 + ch - '0';
           so if the chars sent are 45x (turn x servo to 45 degs)..
           v is the value we want to send to the servo and it is currently 0
           The first char (ch) is 4 so
           0*10 = 0 + 4 - 0 = 4;
           Second char is 4;
           4*10 = 40 + 5 = 45 - 0 = 45;
           Third char is not a number(0-9) so we  drop through...
      case 'x': // if it's x
       ....and land here
       where we send the value of v which is now 45 to the x servo
       and then reset v to 0
        v = 0;
      case 'y':
        v = 0;

Step 4: Processing Software

Leave the Arduino Board connected to the computer.
Open Processing Software.

a) Copy and paste the code.(there is a value that you can paly with is in line 17 :port = new Serial(this, Serial.list()[0], 19200); The [0] in bold is the value you can change and it is for the size of the box to control the servos you can put 0,1,2,3 but I prefer 0 because is the right size for me.
b) Click Run.
c) If you follow everything correct you will see a gray box
    next to the Processing Software. Move the mouse pointer
    to that area to start moving the servos.(green is for up & down, red is for right & left).
Note: The box is very sensitive so it means that when you finish paying with the servos
          you should put the two marks in the middle and disconnect the board.
          If not then the servos will keep running and it would damage them unless you have a 
          fully 360 rotating servo.  


//Processing code:
import processing.serial.*;       

int xpos=90; // set x servo's value to mid point (0-180);
int ypos=90; // and the same here
Serial port; // The serial port we will be using

void setup()
  size(360, 360);
  println(Serial.list()); // List COM-ports
  //select second com-port from the list (COM3 for my device)
  // You will want to change the [1] to select the correct device
  // Remember the list starts at [0] for the first option.
  port = new Serial(this, Serial.list()[0], 19200);

void draw()
  fill(255,0,0); //rgb value so RED
  rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
  fill(0,255,0); // and GREEN
  rect(175, 180, 10, mouseY-180);
  update(mouseX, mouseY);

void update(int x, int y)
  //Calculate servo postion from mouseX
  xpos= x/2;
  ypos = y/2;
  //Output the servo position ( from 0 to 180)

Step 5: Troubleshooting

Common problems of this project:
1. The servos do not move after follow all the directions. 
A. Double check the connections, If not then the servos don't work.
2. The code do not compile on Arduino Software.
A. Double check that at the end were the last key } is there is no space.
3. When copy code to Processing Software  and click Run there is an error.
A. This happens because you have the arduino Software open. After upload the code to the board close it the click run
     or start the Processing Software part again.
4. I change the :port = new Serial(this, Serial.list()[0], 19200);  value to 1,2 or 3 and there is an error.
A. Then leave the value to [0] and it will work.
5. How to provide 5V to both servos at the same time.
A. Use the bread Board for this. (look at the picture).

Any other queations  please leave a comment and I will be glad to help you. ;)
Arduino Challenge

Participated in the
Arduino Challenge