-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.py
More file actions
41 lines (25 loc) · 721 Bytes
/
Copy pathtest3.py
File metadata and controls
41 lines (25 loc) · 721 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
38
39
40
41
import pineworkslabs.RPi as GPIO
import time
from time import sleep
TRIG = 6
ECHO = 5
GPIO.setmode(GPIO.LE_POTATO_LOOKUP)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
while True:
GPIO.output(TRIG, GPIO.LOW)
sleep(2)
GPIO.output(TRIG, GPIO.HIGH)
sleep(0.00001)
GPIO.output(TRIG, GPIO.LOW)
#note the start pule of echo
while GPIO.input(ECHO)==0:
pulse_start = time.time()
#note the end of the pule of echo
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = (pulse_end - pulse_start)
distance = pulse_duration * 17150
#round to two decimal
distance = round(distance, 2)
print(f"Distance: {distance} cm")