#include <Servo.h>
Servo myServo;
int servoPin = 9;
int irSensorPin = 2; // Digital pin for the IR sensor
int servoPosition = 0;
void setup() {
pinMode(irSensorPin, INPUT); // Set IR sensor pin as input
myServo.attach(servoPin); // Attach servo to the servo pin
myServo.write(servoPosition); // Initialize servo to 0 degrees
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(irSensorPin); // Read the IR sensor value
if (sensorValue == LOW) { // Object detected
servoPosition = 90; // Set servo position to 90 degrees
myServo.write(servoPosition);
Serial.println("Object detected, servo moved to 90 degrees");
} else { // No object detected
servoPosition = 0; // Set servo position to 0 degrees
myServo.write(servoPosition);
Serial.println("No object detected, servo at 0 degrees");
}
delay(500); // Delay for stability
}