#include <Servo.h>
Servo servo1;
Servo servo2;
void setup() {
Serial.begin(9600);
servo1.attach(9); // Attach servo1 to pin 9
servo2.attach(10); // Attach servo2 to pin 10
}
void loop() {
if (Serial.available() > 0) {
int angle = Serial.parseInt(); // Read the angle value from Serial Monitor
if (angle >= 0 && angle <= 180) {
rotateServosClockwise(angle);
delay(1000); // Wait for 1 second
rotateServosCounterClockwise(angle);
delay(1000); // Wait for 1 second
} else {
Serial.println("Invalid angle. Please enter an angle between 0 and 180.");
}
}
}
void rotateServosClockwise(int angle) {
servo1.write(angle); // Rotate servo1 clockwise
servo2.write(180 - angle); // Rotate servo2 counterclockwise by the same angle
delay(1000); // Wait for the servos to reach the desired positions
}
void rotateServosCounterClockwise(int angle) {
servo1.write(180 - angle); // Rotate servo1 counterclockwise
servo2.write(angle); // Rotate servo2 clockwise by the same angle
delay(1000); // Wait for the servos to reach the desired positions
}