-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
106 lines (82 loc) · 1.9 KB
/
run.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
import RPi.GPIO as gp
import time
from multiprocessing import Process
from relay import Relay
gp.setmode(gp.BCM)
# Ultrasonic pins
# 0 = C piano key
pins = {
# echo pin, trigger pin, piano key
0: [17, 18, "c4"],
1: [27, 23, "d4"],
2: [22, 24, "e4"],
3: [5, 25, "f4"],
4: [6, 12, "g4"],
5: [13, 16, "a4"],
6: [19, 20, "b4"],
7: [26, 21, "c4"],
}
playStatus = {}
gp.setup(trig,gp.OUT)
for k, v in pins.items():
playStatus[k] = False
avgDistance[k] = [0, [], 1]
gp.setup(v[0], gp.IN)
gp.setup(v[1], gp.OUT)
r = Relay(pins)
def play(k):
if (playStatus[k]):
return
playStatus[k] = True
print(k)
r.press(k)
def get_change(current, previous):
if current == previous:
return 0
try:
return ((current - previous) / previous) * 100.0
except ZeroDivisionError:
return float('inf')
def sonarSensor(k):
global trig, avgDistance, playStatus, mutex
echo = pins[k][0]
'''
if (mutex):
return;
mutex = True
'''
trig = pins[k][1]
# time.sleep(0.2)
gp.output(trig, True)
time.sleep(0.1)
gp.output(trig, False)
start = time.time()
stop = time.time()
while(gp.input(echo) == 0):
start = time.time()
while(gp.input(echo) == 1):
stop = time.time()
t = stop-start
distance = 17150*t
maxDistance = 100
# print(k, '-', distance)
# print(get_change(120, 100))
if (distance > 5 and distance < maxDistance and get_change(distance, 180) < -70):
play(k)
else:
playStatus[k] = False
def looplisten(k):
while True:
sonarSensor(k)
try:
started = False
while True:
if (started):
continue
for k, v in pins.items():
t = Process(target=looplisten, args=[k])
t.start()
started = True
except KeyboardInterrupt as e:
print(e)
gp.cleanup()