#include <Servo.h> // servo library
Servo s1;
int ir_sensor_pin = 2;
int ir_sensor_val = 0;
void setup()
{
Serial.begin(9600); // sensor buart rate
pinMode(ir_sensor_pin, INPUT);
s1.attach(13); // Servo Connect 13 pin
s1.write(90); // Set the initial position of the servo to 90 degree
}
void loop()
{
ir_sensor_val = digitalRead(ir_sensor_pin);
Serial.println(ir_sensor_val); // see the value in serial monitor in Arduino IDE
delay(10); // Time Delay
if (ir_sensor_val == HIGH)
{
s1.write(0); // Turn the servo to 0 degree if the IR sensor gets activated.
delay(2000); // Wait for 1 second.
//break;
//s1.write(90); // Turn the servo back to 90 degrees.
}
else{
s1.write(90); // Turn the servo back to 90 degrees.
}
}