Hello. I'm doing a project including servos, DC motors, and sensors. Almost everything else works, except the servos. Only one spins.
this is the code I was using before I modified part of it.
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
turnRight();
delay(2000);
turnLeft();
delay(2000);
stopRobot();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft() {
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
since the servos I'm using aren't continuous rotation servos, I modified the code so it would be like this:
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
int pos = 0;
void setup() {
servoLeft.attach(1); // Set left servo to digital pin 10
servoRight.attach(2); // Set right servo to digital pin 9
}
void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
stopRobot();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
the second code compiled and uploaded, but only one servo spun.
the schematic for the two servos is at this link: http://www.robotoid.com/appnotes/arduino-operating-two-servos.html
thank you for all your help.