import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24

print "Distance Measurement In Progress"
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

try:
    while True:
        GPIO.output(TRIG, False)
        print "Waiting For Sensor To Settle"
        time.sleep(2)

        GPIO.output(TRIG, True)
        # HC-SR04 sensor requires a short 10uS pulse to trigger
        time.sleep(0.00001)
        GPIO.output(TRIG, False)

        while GPIO.input(ECHO) == 0:
            pulse_start = time.time()
        while GPIO.input(ECHO) == 1:
            pulse_end = time.time()
        pulse_duration = pulse_end - pulse_start

        # Speed of sound at sea level is 34300cm/s
        distance = pulse_duration * 17150
        # 2 decimal places
        distance = round(distance, 2)
        print "Distance:", distance, "cm"
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()
