-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdometer.py
99 lines (80 loc) · 2.33 KB
/
dometer.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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
print("asdlfkjasdflkasdflk")
# TODOS
# - make distance sensor more reliable
# - pictures of dom
# - lights that light up when it's counting
# - calibrate chip appropriately
GPIO_TRIGGER = 23
GPIO_ECHO = 24
booze_pin = 14
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(booze_pin, GPIO.IN)
GPIO.output(GPIO_TRIGGER, False)
time.sleep(2)
print("asdflkjeiqwqq111")
max_time = 1
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()
ActualStartTime = time.time()
print("initialSt: %1f." % StartTime)
print("initialSp: %1f." % StopTime)
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
if StartTime - ActualStartTime > max_time:
print("something weird happened")
return 0
print("StartTime: %1f." % StartTime)
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
if StopTime - StartTime > max_time:
print("something else weird happened")
return 0
print("StopTime: %1f." % StopTime)
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
time_to_smell = 1000
def test_approval():
n = 0
alcohol_absent_count = 0
while n < time_to_smell:
alcohol_absent_count += GPIO.input(booze_pin)
time.sleep(0.01)
n += 1
print("Verdict:")
print(alcohol_absent_count)
if alcohol_absent_count < 1000:
print("boooooze")
return True
else:
print("no booz")
return False
while True:
print("wat is distance")
dist = distance()
print("Distance is: %.1f" % dist)
if dist < 15:
print("cocktail detected!!!")
print("but does Dom approve?")
does_dom_approve = test_approval()
if does_dom_approve:
print("Dom approves!")
else:
print("Dom does not approve :(")
time.sleep(1)