#include <ESP32Servo.h>
#define TRIG_PIN 18
#define ECHO_PIN 5
#define SERVO_PIN 25
#define SERVO_PIN3 27
#define DISTANCE_ultrasonservo 150 //centimetres
#define DISTANCE1 150 //centimetres
#define distance_cm2 350 //centimetres
Servo servo;
Servo servo1;
float distance_cm,duration_us,distance_cm1,duration_us1;
int a1=0;
int a2=0;
int i=0;
int servoPos;
int xValue;
void setup() {
Serial.begin (9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(SERVO_PIN);
servo.write(0);
servo1.attach(SERVO_PIN3);
servo1.write(0);
}
void loop() {
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017 * duration_us;
if (distance_cm < DISTANCE_ultrasonservo) {
servo.write(90); }
else {
servo.write(0);}
xValue = analogRead(25);
servoPos = map (xValue, 0, 1023, 0, 90);
if (servoPos == 0) {
for(a1=0;a1<90;a1++){
servo1.write(a1);
delay(20);
}
for(a2=90;a2>=0;a2--){
servo1.write(a2);
delay(20);}
}
}