-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrason.py
48 lines (42 loc) · 1.05 KB
/
ultrason.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
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
Tr = 11
Ec = 8
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Tr, GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(Ec, GPIO.IN)
def checkdist(): #Reading distance
GPIO.setmode(GPIO.BCM)
GPIO.setup(Tr, GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(Ec, GPIO.IN)
GPIO.output(Tr, GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(Tr, GPIO.LOW)
while not GPIO.input(Ec):
pass
t1 = time.time()
while GPIO.input(Ec):
pass
t2 = time.time()
return round((t2-t1)*340/2,2)
#return (t2-t1)*340/2
# def checkdist(): #Reading distance
# GPIO.output(Tr, GPIO.HIGH)
# time.sleep(0.000015)
# GPIO.output(Tr, GPIO.LOW)
# while not GPIO.input(Ec):
# pass
# t1 = time.time()
# while GPIO.input(Ec):
# t3 = time.time()
# if ((t3-t1)*340/2)>=2:
# break
# pass
# t2 = time.time()
# return round((t2-t1)*340/2,2)
if __name__ == '__main__':
while 1:
print(checkdist())
time.sleep(1)