#include <Servo.h>
const int irSensorPin = 2;
const int servoPin = 9;
Servo myServo;
void setup() {
pinMode(irSensorPin, INPUT);
myServo.attach(servoPin);
Serial.begin(9600);
}
void loop() {
int irSensorValue = digitalRead(irSensorPin);
if (irSensorValue == HIGH) {
rotateServo(90);
Serial.println("Motion detected!");
}
else {
rotateServo(0);
}
delay(500); // Delay to avoid rapid toggling and to stabilize readings
}
void rotateServo(int angle) {
myServo.write(angle);
delay(1000);
}