-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHC-SR04_test.py
49 lines (38 loc) · 1.12 KB
/
HC-SR04_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#####################################
# HC-SR04_test.py
# Written By Ethan Reker 2017
#####################################
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
print "Using BOARD numbering system for GPIO pins."
TRIG = raw_input("Enter Pin Number that is connected to TRIG: ")
ECHO = raw_input("Enter Pin Number that is connected to ECHO: ")
TRIG = int(TRIG)
ECHO = int(ECHO)
GPIO.cleanup(TRIG)
GPIO.cleanup(ECHO)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
cont = ""
while cont!="n":
GPIO.output(TRIG, GPIO.LOW)
time.sleep(1)
GPIO.output(TRIG, GPIO.HIGH)
time.sleep(0.0001)
GPIO.output(TRIG, GPIO.LOW)
#print "Waiting for edge."
GPIO.wait_for_edge(ECHO, GPIO.BOTH, timeout=1000)
start_time = time.time()
GPIO.wait_for_edge(ECHO, GPIO.BOTH, timeout=1000)
end_time = time.time()
total_time = end_time - start_time
distance = total_time * 17150
distance = round(distance, 2)
if distance > 3 and distance < 400:
print "Distance: ", distance, "cm"
else:
print "Out of Range"
cont = raw_input("Continue?(y/n): ")
GPIO.cleanup()
print "Test Complete."