/*
1. If an object is detected, turn the
servo motor 90 degrees and turn on the LED
2. If there is no object detected, turn the
servo motor 90 degrees back and turn off the LED
*/
#include <ESP32Servo.h>
Servo Serv;
int pinIR=5; //IR sensor digital pin
int servoPin=18; //servo motor pin
int pinLED=2;
int val=0;
void setup(){
Serv.attach(servoPin, 500, 2400);
Serv.write(0);//back to 0 degrees
pinMode(pinLED,OUTPUT);
}
int pos = 90;
void loop() {
val = digitalRead(pinIR);
if (val ==0){
digitalWrite(pinLED,LOW);
Serv.write(pos);
delay(4060);
}
else {
digitalWrite(pinLED,HIGH);
Serv.write(90);
delay(3650);
Serv.write(40);
delay(1320);
Serv.write(120);
delay(800);
Serv.write(40);
delay(1420);
Serv.write(90);
delay(650);
Serv.write(40);
delay(1320);
Serv.write(120);
delay(800);
Serv.write(40);
delay(1420);
}
}