#include <Servo.h>
const int ser_pin = 11; // Pin for the servo
Servo servo1; // Create a Servo object
void setup() {
pinMode(12, INPUT); // Set pin 12 as input for motion detection
servo1.attach(ser_pin); // Attach the servo to pin 11
}
void loop() {
if (digitalRead(12) == HIGH) { // Check for motion
for (int pos = 0; pos <= 180; pos++) { // Sweep from 0 to 180
servo1.write(pos);
delay(30); // Wait for the servo to reach the position
}
delay(3000); // Wait for 3 seconds
for (int pos = 180; pos >= 0; pos--) { // Sweep back from 180 to 0
servo1.write(pos);
delay(30); // Wait for the servo to reach the position
}
}
}