#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define TRIGGER_PIN 18
#define ECHO_PIN 5
#define SERVO_PIN 17
NewPing sonar(TRIGGER_PIN, ECHO_PIN, 200); // 200 cm maximum distance
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address and dimensions
Servo servo;
void setup() {
lcd.init();
lcd.backlight();
servo.attach(SERVO_PIN);
}
void loop() {
int distance = sonar.ping_cm();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance: " + String(distance) + " cm");
if (distance < 100) { // Change this threshold as needed
lcd.setCursor(0, 1);
lcd.print("Object detected!");
servo.write(90); // Point the servo to the object
} else {
lcd.setCursor(0, 1);
lcd.print("No object detected");
servo.write(0); // Reset servo position
}
delay(500);
}