-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonic_distance.py
46 lines (32 loc) · 1.03 KB
/
sonic_distance.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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(29, GPIO.OUT) #set PIN 29 as SR04 tirgger
GPIO.setup(31, GPIO.IN) #set PIN 31 as SR04 echo
def sonic_detect():
GPIO.output(29,0)
GPIO.output(29,1) # init I/O
time.sleep(0.000015) # sensor enable signal trigger 15uS
GPIO.output(29,0) # enable signal send end
t0=time.time()
while not GPIO.input(31):
t3=time.time() # waitting echo signal time 't3'
if (t3-t0)>=20:
print("SR04 cable connection maybe NG,or U set sample frequecy too high!")
time.sleep(0.5)
else:
pass
t1 = time.time()
while GPIO.input(31):
pass
t2 = time.time()
sonf = int((t2 - t1) * 17000)
#print(sonf)
return sonf
if __name__=='__main__':
print("Sonic wave distanse start!")
while 1:
a=sonic_detect()
print(a,"cm")
time.sleep(0.1)