#include <ESP32Servo.h>
#define trig 25
#define echo 33
const int servoPin = 18;
int pos = 0;
void irsensor();
void ultrasonic();
void espservomotor();
float distance, duration,cm;
Servo servo;
void setup()
{
pinMode(14,OUTPUT);
pinMode(26,OUTPUT);
pinMode(27,INPUT);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
servo.attach(servoPin, 500, 2400);
Serial.begin(9600);
}
void loop()
{
irsensor();
ultrasonic();
}
void irsensor()
{
int irvalue=digitalRead(27);
if(irvalue==1)
{
digitalWrite(14,HIGH);
Serial.println("LED ON");
Serial.println("Person detected");
delay(1000);
espservomotor();
}
else
{
digitalWrite(14,LOW);
Serial.println("LED OFF");
Serial.println("not detected");
delay(1000);
}
}
void ultrasonic()
{
digitalWrite(trig,LOW);
delay(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration = pulseIn(echo,HIGH);
distance = ((duration*0.034)/2);
Serial.print("Distance:");
Serial.print(distance);
Serial.println("cm");
if(distance>=150)
{
digitalWrite(26,HIGH);
Serial.println("LED ON");
Serial.println("Person detected");
delay(1000);
espservomotor();
}
}
void espservomotor() {
for (pos = 0; pos <= 90; pos += 1) {
servo.write(pos);
delay(50);
}
// for (pos = 90; pos >= 0; pos -= 1) {
// servo.write(pos);
// delay(15);
// }
}