-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_distance.py
More file actions
executable file
·38 lines (33 loc) · 828 Bytes
/
14_distance.py
File metadata and controls
executable file
·38 lines (33 loc) · 828 Bytes
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
#! /usr/bin/python
#-----------------------------------------------------------
# File name : 14_distance.py
# Description : measuring the distance.
# Author : Jason
# E-mail : jason@adeept.com
# Website : www.adeept.com
# Date : 2015/06/12
#-----------------------------------------------------------
import RPi.GPIO as GPIO
import time
def checkdist():
GPIO.output(16, GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(16, GPIO.LOW)
while not GPIO.input(18):
pass
t1 = time.time()
while GPIO.input(18):
pass
t2 = time.time()
return (t2-t1)*340/2
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(18,GPIO.IN)
time.sleep(2)
try:
while True:
print 'Distance: %0.2f m' %checkdist()
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()