const int trigPin = 13;

const int echoPin = 12;

const int ledGreen = 2;

const int ledBuzz = 4;

//define sound speed in cm/us

#define SOUND_SPEED 0.034

#define CM_TO_INCH 0.393701

long duration;

float distanceCm;

float distanceInch;

void setup() {

Serial.begin(115200);

Serial.println("Hello, ESP321");

Serial.begin(115200); // Starts the serial communication 
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 
pinMode(echoPin, INPUT); // Sets the echopin as an Input
pinMode(ledGreen, OUTPUT);
pinMode(ledBuzz, OUTPUT);

}

void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(10);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echopin, returns the sound wave travel time in mic

duration = pulseIn(echoPin, HIGH);

// Calculate the distance

distanceCm = duration * SOUND_SPEED/2;

// Convert to inches

distanceInch  = distanceCm * CM_TO_INCH;

// Prints the distance in the Serial Monitor

Serial.print("Distance (cm): ");

Serial.println(distanceCm);
Serial.print("Distance (inch): "); 
Serial.println(distanceInch);
 if(distanceCm < 10)
 {

digitalWrite(ledGreen, LOW);

digitalWrite(ledBuzz, LOW);

}else if(distanceCm < 40){

digitalWrite(ledGreen, HIGH);

digitalWrite(ledBuzz, LOW);

}else if(distanceCm < 70){

digitalWrite(ledGreen, HIGH);

digitalWrite(ledBuzz, HIGH);
}else{

digitalWrite(ledGreen, LOW);

digitalWrite(ledBuzz, LOW);
 }

delay(1000);

}