
const int echo = 4;  
const int trigger = 3; 
long start;                   
long elapsed;  
float distance;

void setup() {
  Serial.begin(9600); 
}

void loop() {
  // trigger the rangefinder
  digitalWrite(trigger, HIGH);
  delay(1);   
  digitalWrite(trigger, LOW);
  // wait for the echo to start
  while (!digitalRead(echo));
  // start timer
  start = micros();
  // wait for echo to return
  while (digitalRead(echo));   
  // stop timer  
  elapsed = micros() - start;
  // print the results
  Serial.write(27); 
  Serial.print("[1;8H");
  Serial.print("Ultrasonic Range Finder ");
  Serial.write(27); 
  Serial.print("[3;8H");
  Serial.print("Elapsed time ");
  Serial.print(elapsed);
  Serial.print(" micro seconds        ");
  Serial.write(27); 
  Serial.print("[5;8H");
  Serial.print("Distance ");
  Serial.print(elapsed/58);
  Serial.print(" cm      ");
  
  // wait a bit
  delay(500);                     
}
