#http://www.raspberrypicookbook.com

# 5/(470+270) = 0.00649350649351
# 470 3.05194805195
# 
# 270 1.7532467523

import RPi.GPIO as GPIO
import time
trigger_pin = 18
echo_pin = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)

def send_trigger_pulse():
	GPIO.output(trigger_pin, True)
	time.sleep(0.0001)
	GPIO.output(trigger_pin, False)

def wait_for_echo(value, timeout):
	count = timeout
	while GPIO.input(echo_pin) != value and count > 0:
		count = count - 1
		
def get_distance():
	
	send_trigger_pulse()
	wait_for_echo(True, 10000)
	start = time.time()
	wait_for_echo(False, 10000)
	finish = time.time()
	pulse_len = finish - start
	distance_cm = pulse_len / 0.000058
	distance_in = distance_cm / 2.5
	return (distance_cm, distance_in)

while True:
    print("cm=%f\tinches=%f" % get_distance())
    time.sleep(1)
	
	
	
