#include <Wire.h>
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#define TRIG 18
#define ECHO 5
#define SERVO 19
int pos = 0;
LiquidCrystal_I2C lcd(0x27, 16, 4);
Servo servo; // create servo object to control a servo
void setup () {
lcd.init();
lcd.backlight();
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
servo.attach(SERVO); //motor.attach(SERVO)
}
void loop() {
for (pos = 0; pos <= 180; pos += 1)
servo.write(pos);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int distance = pulseIn(ECHO, HIGH);
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance / 58);
lcd.print(" cm");
lcd.setCursor(0, 2);
lcd.print("Angle: ");
lcd.print(pos);
lcd.print(" deg");
}