#Basic Ultrasonic sensor (HC-SR04) code

import RPi.GPIO as GPIO #GPIO RPI library
import time # makes sure Pi waits between steps
GPIO.setmode(GPIO.BCM) #sets GPIO pin numbering
#GPIO.setmode(GPIO.BOARD)

#Remove warnings
GPIO.setwarnings(False)

#Create loop variable
#loop = 1

#BCM
TRIG = 23 #output pin - triggers the sensor
ECHO = 24 #input pin - reads the return signal from the sensor

#BOARD
#TRIG=16
#ECHO=18

#Looping not necessary
#Print a message to let the user know that distance measurement is in progress
print ("Distance Measurement In Progress")

#Set two GPIO ports as inputs/outputs
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

#while loop == 1: #Looping forever
while True: #Looping forever
    #Ensure the trigger pin is set low
    GPIO.output(TRIG, False)

    #Give the sensor a second to settle
    print ("Waiting for Sensor to Settle")
    #time.sleep(2)
    time.sleep(1)

    #Create trigger pulse
    GPIO.output(TRIG,True)

    #Set trigger pin high for 10uS
    time.sleep(0.00001)

    #Set it low again
    GPIO.output(TRIG,False)

    #Record the last low timestamp for ECHO (just before the return signal is received and the pin goes high)
    while GPIO.input(ECHO)==0:
        pulse_start = time.time()
        
    #Once a signal is received, the value changes from low to high, and the signal will remain high for the duration of the echo pulse
    while GPIO.input(ECHO)==1:
        pulse_end = time.time()

    #speed=distance/time
    #speed of sound at sea level = 343m/s
    #34300 = distance/(time/2)
    #17150 = distance/time
    #17150*time = distance

    #Calculating...
    pulse_duration = pulse_end - pulse_start
    distance_cm = pulse_duration*17150
    #distance_cm = pulse_duration*0.034/2;
    distance_cm = round(distance_cm,2)
    distance_inch = distance_cm/2.54 #2.54 cm in 1 inch
    #distance_inch = pulse_duration*0.0133/2
    distance_inch = round(distance_inch,2)
    distance_feet = distance_inch/12
    distance_feet = round(distance_feet,2)
    

    #Print distance
    #print ("Distance:",distance_cm,"cm")
    #print ("Distance:",distance_inch,"in")
    print ("Distance:",distance_feet,"ft")
    
    #Delay
    time.sleep(2)

#Clean GPIO pins to ensure all inputs/outputs are reset
GPIO.cleanup()



[((5, 0, 5, 22), 'RPi.GPIO.setmode', 'GPIO.setmode', ({(5, 13, 5, 21): 'GPIO.BCM'}, {}), '(GPIO.BCM)', True, 'import RPi.GPIO as GPIO\n'), ((9, 0, 9, 23), 'RPi.GPIO.setwarnings', 'GPIO.setwarnings', ({(9, 17, 9, 22): '(False)'}, {}), '(False)', True, 'import RPi.GPIO as GPIO\n'), ((27, 0, 27, 25), 'RPi.GPIO.setup', 'GPIO.setup', ({(27, 11, 27, 15): 'TRIG', (27, 16, 27, 24): 'GPIO.OUT'}, {}), '(TRIG, GPIO.OUT)', True, 'import RPi.GPIO as GPIO\n'), ((28, 0, 28, 24), 'RPi.GPIO.setup', 'GPIO.setup', ({(28, 11, 28, 15): 'ECHO', (28, 16, 28, 23): 'GPIO.IN'}, {}), '(ECHO, GPIO.IN)', True, 'import RPi.GPIO as GPIO\n'), ((84, 0, 84, 14), 'RPi.GPIO.cleanup', 'GPIO.cleanup', ({}, {}), '()', True, 'import RPi.GPIO as GPIO\n'), ((33, 4, 33, 28), 'RPi.GPIO.output', 'GPIO.output', ({(33, 16, 33, 20): 'TRIG', (33, 22, 33, 27): '(False)'}, {}), '(TRIG, False)', True, 'import RPi.GPIO as GPIO\n'), ((38, 4, 38, 17), 'time.sleep', 'time.sleep', ({(38, 15, 38, 16): '(1)'}, {}), '(1)', False, 'import time\n'), ((41, 4, 41, 26), 'RPi.GPIO.output', 'GPIO.output', ({(41, 16, 41, 20): 'TRIG', (41, 21, 41, 25): '(True)'}, {}), '(TRIG, True)', True, 'import RPi.GPIO as GPIO\n'), ((44, 4, 44, 23), 'time.sleep', 'time.sleep', ({(44, 15, 44, 22): '(1e-05)'}, {}), '(1e-05)', False, 'import time\n'), ((47, 4, 47, 27), 'RPi.GPIO.output', 'GPIO.output', ({(47, 16, 47, 20): 'TRIG', (47, 21, 47, 26): '(False)'}, {}), '(TRIG, False)', True, 'import RPi.GPIO as GPIO\n'), ((81, 4, 81, 17), 'time.sleep', 'time.sleep', ({(81, 15, 81, 16): '(2)'}, {}), '(2)', False, 'import time\n'), ((50, 10, 50, 26), 'RPi.GPIO.input', 'GPIO.input', ({(50, 21, 50, 25): 'ECHO'}, {}), '(ECHO)', True, 'import RPi.GPIO as GPIO\n'), ((51, 22, 51, 33), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((54, 10, 54, 26), 'RPi.GPIO.input', 'GPIO.input', ({(54, 21, 54, 25): 'ECHO'}, {}), '(ECHO)', True, 'import RPi.GPIO as GPIO\n'), ((55, 20, 55, 31), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n')]