-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHC-SR04_KY-008_KY_KY-012.py
81 lines (54 loc) · 1.26 KB
/
HC-SR04_KY-008_KY_KY-012.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import _thread
#GPIO Setup
GPIO_14="cable verde"
GPIO_15="cable rojo"
GPIO_18="cable blanco"
VCC="cable amarillo"
GND="cable naranja"
V3="cable azul"
TRIGGER = 15
ECHO = 18
LASER = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(LASER, GPIO.OUT)
GPIO.setup(TRIGGER,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIGGER,False)
GPIO.output(LASER,False)
def get_distance():
GPIO.output(TRIGGER,True)
time.sleep(0.00001)
GPIO.output(TRIGGER,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,3)
return(distance)
def get_continue_distance():
while True:
get_distance()
def show():
global pw
print(pw)
def laser():
while True:
time.sleep(0.1)
distance=get_distance()
if distance < 2.000:
distance = 0
elif distance >30.000:
distance = 30.000
print(distance)
pw=distance/100
GPIO.output(LASER,False)
time.sleep(pw)
GPIO.output(LASER,True)
time.sleep(pw)
laser()
GPIO.cleanup()