#!/usr/bin/env python  
  
import mraa  
import time  

# setup
trigPin = 8;
echoPin = 9;
trig = mraa.Gpio(trigPin)  
trig.dir(mraa.DIR_OUT)  
echo = mraa.Gpio(echoPin)  
echo.dir(mraa.DIR_IN)  

# function: delay in microseconds
def delayMicroseconds(delay = 0):
	microsecond = delay/1e6;
	time.sleep(microsecond)

# function: read pulse length (in microsecond)
def pulseIn(GPIOpin):

	while not echo.read():
		continue
	start = time.time()

	while echo.read():
		continue
	end = time.time()

	return (end-start) * 1e6;

# Function Loop
while True:	
	# pulse generation
	trig.write(0) # LOW
	delayMicroseconds(2) # delay 2 microsecond
	trig.write(1) # HIGH;
	delayMicroseconds(10) # delay 10 microsecond
	trig.write(0) # LOW

	# pulse duration
	duration = pulseIn(echo) 
	distance = duration/58.2

	if distance > 80:
		print "Out of Range"
	else:
		print str(distance) + " cm"

	time.sleep(30e-3) # delay 30 millisecond

