I am trying to get the basics of using a servo. I have done the single servo examples and they all work fine. I tried writing some sketches to use two servos but only one would ever work.
This is simple 2 servo sketch I got online. Which ever servo is assigned a pin first works, but not the second servo assigned. Same results on any pin I try.
I also tried the softwareservo library and some examples that use the SoftwareServo::refresh(); statement to keep multiple servo ports refreshed but I keep getting a error.
Here is the sample sketch.
#include <Servo.h>
Servo myservo;
Servo mysecondservo;
void setup()
{
myservo.attach(3); // attaches a servo connected to pin 3
mysecondservo.attach(15); // attaches a servo connected to pin 15
}
void loop()
{
myservo.write(180); // sets the servo position at 180 degrees
mysecondservo.write(65); // sets the servo position at 65 degrees
}
Simple servo test code for two servos. Note that servos need e3xternal power supplies.
//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(8);
myservo2.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
myservo1.write(n);
myservo2.write(180-n); //turns opposide servo in desired direction
readString="";
}
}
I just tried your code and it mostly worked. The two servos moved but had a lot of jitter. They are MKS high voltage servos I took out of a plane because of the jitter. I will try two simple 9 gram servos and see what happens.
I didn't really pay attention in that sketch that it was pin 15. When I actually compiled the sketches I always tried pins 7 thru 10.
I just tried this sketch from you and it now also works with analog servos.
//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together
// Powering a servo from the arduino usually DOES NOT WORK.
String readString; #include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(7);
myservo2.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
myservo1.write(n);
myservo2.write(180-n); //turns opposide servo in desired direction
readString="";
}
}