In response to a request, I have created a program which allows for multi servo control through Processing. Connect two servo’s to pins 9 and 10 of your Arduino, as shown in my previous blog post, wich has got much more information on using potentiometers: Servo_control_via_potentiometer. You will also need to ensure you have the Servo library for Arduino (again more is detailed in my last blog post).
To control servo’s using your computer, you will need to use Processing. This is an incredibly useful java based programming language, wich makes it really easy to communicate with the Arduino. You can find out more at processing.org .
Connecting your Arduino to a computer is as simple as plugging it in to a USB port. Then, when you load the processing file below, look in the black box at the bottom of the screen to see a list of ports available on your computer, and which one the Arduino is using, then change the number in Serial.list()[0] to match whatever port is being used.
import processing.serial.*;
Serial myPort; // Create object from Serial class
PFont f;
void setup() {
size(640, 130);
println(Serial.list());
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
f = createFont("Times New Roman",16,true);
}
void draw() {
fill(95);
textFont(f, 16);
text("Control Two Servo, switch between then using the number 1 and 2 keys on your keyboard", 10, 30);
text("Use the L key to rotate the servo 90' left, and the R key to rotate it 90' Right", 10, 60);
text("Use the S key to return to the center", 10, 90);
}
void keyPressed() {
switch (key) {
case 'a':
println("A pressed");
myPort.write('l'); //turns left
break;
case 's':
println("S pressed");
myPort.write('s'); //stops turning
break;
case 'd':
println("D pressed");
myPort.write('r'); //turns right
break;
case '1':
println("Using servo 1");
myPort.write('1'); //controlling servo 1
break;
case '2':
println("Using servo 2");
myPort.write('2'); //controlling servo 2
break;
default:
break;
}
}
It is then time to upload the Arduino code. Ensure that you are not running the Processing file whilst uploading the code, or the upload may fail, as the Arduino can only communicate with one srial device at a time. After uploading, quit the Arduino IDE before starting the Processing file.
// Controlling two servo positions using a processing file
//Created by Jacob Unwin - http://www.jacob-unwin.com
#include <Servo.h>
Servo myservo_one; // create servo object to control a servo
Servo myservo_two;
char val;
void setup()
{
myservo_one.attach(9); // attaches the servo on pin 9 to the servo object
myservo_two.attach(10); //attaches the 2nd servo on pin 10
myservo_one.write(90); //sets both servo's at 90 degrees
myservo_two.write(90);
Serial.begin(9600); //start serial output at 9600
}
void loop()
{
if(Serial.available()){
val = Serial.read();
if (val == '1'){ //if 1 has been pressed, current servo is 1
currentServo == 1;
} else if (val == '2'){ //else if 2 has been pressed, current servo is 2
currentServo == 2;
}
if (val == 'l') { //if the last key press was l, rotates to 0 degrees
if (currentServo == 1){
myservo_one.write(0);
} else {
myservo_two.write(0);
}
}
if (val == 'r') { //if the last key press was r, rotates to 180'
if (currentServo == 1){
myservo_one.write(180);
} else {
myservo_two.write(180);
}
}
if (val == 's') { //if the last key press was s, returns to the center
if (currentServo == 1){
myservo_one.write(180);
} else {
myservo_two.write(180);
}
}
}
To control the servo, first select wether you want to control servo one or two by pressing the 1 or 2 keys on your keyboard. Then you can rotate the servo left (to 0 degrees) using the L key, to the center (90 degreed) using the S key, and to the right (180 degrees) using the D key. This code could be further modified to add more servo’s, and create a more accurate rotation, i.e. with each key press moving the servo by only 1 degree.


