#include <Servo.h>
Servo myServo; // Create a servo object to control a servo motor
int servoPin = 9; // Define the pin for the servo motor
void setup() {
myServo.attach(servoPin); // Attach the servo to the specified pin
}
void loop() {
// Rotate servo motor forward
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle); // Rotate the servo to the specified angle
delay(15); // Delay for smoother motion, adjust as needed
}
delay(1000); // Delay at the end of forward motion
// Rotate servo motor in reverse
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle); // Rotate the servo to the specified angle
delay(15); // Delay for smoother motion, adjust as needed
}
delay(1000); // Delay at the end of reverse motion
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}