#include <BluetoothSerial.h>
#define CONFIG_BT_ENABLED
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error "Bluetooth was not enabled! Please, enable it!"
#endif
BluetoothSerial SerialBT;
const char triggerPin = 2; // Pin for sending a signal to the HC-SR04 sensor
const char echoPin = 4; // Pin for receiving the response from the HC-SR04
void setup() {
Serial.begin(115200);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
SerialBT.begin("ESP32");
}
void loop() {
unsigned short int duration, distance;
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // Calculate distance in centimeters
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Delay before the next distance measurement
}