-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltrasonic.py
More file actions
54 lines (46 loc) · 1.19 KB
/
Copy pathUltrasonic.py
File metadata and controls
54 lines (46 loc) · 1.19 KB
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
50
51
52
53
54
import RPi.GPIO as GPIO
import time
import threading
import sys
TRIG = 23
ECHO = 24
LIGHT1 = 17
def definition():
GPIO.setmode(GPIO.BCM)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(LIGHT1, GPIO.OUT)
def fun1():
while True:
GPIO.output(TRIG, False)
print("Waiting For Sensor To Settle")
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
if 2 < distance < 50:
print
"Distance:", distance - 0.5, "cm"
GPIO.output(LIGHT1, 1)
time.sleep(1)
GPIO.output(LIGHT1, 0)
else:
print("Out Of Range")
def main():
try:
definition()
threads = []
t = threading.Thread(target=fun1)
threads.append(t)
t.start()
except KeyboardInterrupt:
GPIO.cleanup()
if __name__ == "__main__":
main()