#include <NewPing.h>
#include <BluetoothSerial.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 14
#define MAX_DISTANCE 10 // Maximum distance we want to ping for (in centimeters).
#define IR_SENSOR_PIN 13

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
BluetoothSerial SerialBT;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("HUAWEI CM51"); // Bluetooth device name
  pinMode(IR_SENSOR_PIN, INPUT);
}

void loop() {
  if (digitalRead(IR_SENSOR_PIN) == HIGH) {
    delay(1000); // Debounce
    int distance = sonar.ping_cm();
    if (distance == 0) {
      SerialBT.println("Out of range");
    } else {
      String speech = "The distance is " + String(distance) + " centimeters.";
      SerialBT.println(speech);
    }
    delay(2000); // Wait before next measurement
  }
}