-
Notifications
You must be signed in to change notification settings - Fork 0
/
garage_sensor.py
121 lines (87 loc) · 2.87 KB
/
garage_sensor.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
# DISTANCE VARIABLES IN INCHES
minDistance = 5
maxDistance = 15
# DISTANCE THAT WE START DETECTING OBJECTS
startMeasure = 25
# SET GPIO PINS
# PINS FOR DISTANCE SENSOR
GPIO_TRIGGER = 18
GPIO_ECHO = 24
# PINS FOR LEDS
GPIO_RED = 17
GPIO_GREEN = 22
GPIO_BLUE = 27
# SET GPIO DIRECTION (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
# PINS FOR LEDS
GPIO.setup(GPIO_RED, GPIO.OUT)
GPIO.setup(GPIO_GREEN, GPIO.OUT)
GPIO.setup(GPIO_BLUE, GPIO.OUT)
def distance():
# SET TRIGGER TO HIGH
GPIO.output(GPIO_TRIGGER, True)
# SET TRIGGER AFTER 0.01MS TO LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# SAVE STARTTIME
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# SAVE STOPTIME
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# TIME DIFFERENCE BETWEEN START AND STOP
TimeElapsed = StopTime - StartTime
# MULTIPLY WITH THE SONIC SPEED (34300 cm/s)
# AND DIVIDE BY 2, (FROM SENSOR AND BACK TO SENSOR)
# CONVERT TO INCHES (/ 2.54)
distance = (TimeElapsed * 34300 / 2.54) / 2
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f inches" % dist)
if dist < minDistance:
print ("Too Close!")
# BLINK RED LED RAPIDLY
GPIO.output(GPIO_RED, True)
time.sleep(0.1)
GPIO.output(GPIO_RED, False)
# TURN OFF GREEN LED
GPIO.output(GPIO_GREEN, False)
# TURN ON THE BLUE LED
GPIO.output(GPIO_BLUE, False)
elif dist > minDistance and dist < maxDistance:
print ("In Range")
# TURN OFF RED LED
GPIO.output(GPIO_RED, False)
# TURN ON GREEN LED
GPIO.output(GPIO_GREEN, True)
# TURN OFF BLUE LED
GPIO.output(GPIO_BLUE, False)
elif dist < startMeasure:
print ("Object Detected")
# TURN OFF RED LED
GPIO.output(GPIO_RED, False)
# TURN ON GREEN LED
GPIO.output(GPIO_GREEN, False)
# TURN ON BLUE LED
GPIO.output(GPIO_BLUE, True)
elif dist > startMeasure:
print ("No Objects Detected")
# ALL LEDS OFF
GPIO.output(GPIO_RED, False)
GPIO.output(GPIO_GREEN, False)
GPIO.output(GPIO_BLUE, False)
time.sleep(.25)
# KILL BY PRESSING CNTRL-C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()