#include <Servo.h> // Include the built-in Arduino Servo library
Servo myServo; // Create a servo object to control our motor
int angle = 0; // Variable to store the current servo position
void setup() {
// Attach the servo motor object to digital Pin 9
myServo.attach(9);
// Initialize Serial Monitor for position tracking
Serial.begin(9600);
Serial.println("--- Servo Sweep Project Initialized ---");
}
void loop() {
// Sweep forward from 0 to 180 degrees
Serial.println("Sweeping forward...");
for (angle = 0; angle <= 180; angle += 1) {
myServo.write(angle); // Tell servo to go to this position
delay(15); // Wait 15ms for the motor arm to physical reach it
}
delay(1000); // Wait 1 second at the 180-degree mark
// Sweep backward from 180 down to 0 degrees
Serial.println("Sweeping backward...");
for (angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle); // Tell servo to go to this position
delay(15); // Wait 15ms for the arm to move
}
delay(1000); // Wait 1 second at the 0-degree mark
}