質問編集履歴
4
状況の説明
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,6 +140,8 @@
|
|
140
140
|
|
141
141
|
**__追記__**
|
142
142
|
|
143
|
+
**ソースコードがある状況なので、こちらのコードを参照ください。全体の動作に関しては上記のものと変わりません**
|
144
|
+
|
143
145
|
threadingを用いてマルチスレッドで動かすことにしました。
|
144
146
|
|
145
147
|
[こちら](https://qiita.com/castaneai/items/9cc33817419896667f34)を参考に上記の機能を関数化して以下のコードで実効しました。
|
3
コードの変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -214,12 +214,8 @@
|
|
214
214
|
|
215
215
|
def noteStart():
|
216
216
|
|
217
|
-
|
218
|
-
|
219
217
|
global a,i
|
220
218
|
|
221
|
-
initAll()
|
222
|
-
|
223
219
|
|
224
220
|
|
225
221
|
while a == True:
|
2
更新したコードを記入
test
CHANGED
File without changes
|
test
CHANGED
@@ -133,3 +133,147 @@
|
|
133
133
|
|
134
134
|
|
135
135
|
実際のコードがなく、分かりづらい説明になってしまいましたがどうか回答お願いいたします。
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
**__追記__**
|
142
|
+
|
143
|
+
threadingを用いてマルチスレッドで動かすことにしました。
|
144
|
+
|
145
|
+
[こちら](https://qiita.com/castaneai/items/9cc33817419896667f34)を参考に上記の機能を関数化して以下のコードで実効しました。
|
146
|
+
|
147
|
+
```python
|
148
|
+
|
149
|
+
import threading
|
150
|
+
|
151
|
+
import elcFunc as ef
|
152
|
+
|
153
|
+
import judgeFunction as jf
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
ef.initAll()
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
if __name__ == "__main__":
|
162
|
+
|
163
|
+
thread1 = threading.Thread(target = jf.judgeFunc())
|
164
|
+
|
165
|
+
thread2 = threading.Thread(target = ef.noteStart())
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
thread1.start()
|
170
|
+
|
171
|
+
thread2.start()
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
jf.judgeEnd()
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
jf.judgeFunc()一定以上の音量が鳴った回数を保存する、ef.noteStart()がLEDを起動して切り替える関数となっています
|
180
|
+
|
181
|
+
このコードの実行結果は、並行に関数を実行せずにただただ一定以上の音を監視する関数が続いてしまいました。原因がわかりません...
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
**judgeFunc()**
|
186
|
+
|
187
|
+
```python
|
188
|
+
|
189
|
+
def judgeFunc():
|
190
|
+
|
191
|
+
while True:
|
192
|
+
|
193
|
+
global score
|
194
|
+
|
195
|
+
data = stream.read(CHUNK)
|
196
|
+
|
197
|
+
x = np.frombuffer(data, dtype="int16") / 32768.0
|
198
|
+
|
199
|
+
if x.max() > threshold:
|
200
|
+
|
201
|
+
score += 1
|
202
|
+
|
203
|
+
print(score)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
**noteStart()**
|
212
|
+
|
213
|
+
```python
|
214
|
+
|
215
|
+
def noteStart():
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
global a,i
|
220
|
+
|
221
|
+
initAll()
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
while a == True:
|
226
|
+
|
227
|
+
print("Start")
|
228
|
+
|
229
|
+
wiringpi.digitalWrite(GPIO26, LOW)
|
230
|
+
|
231
|
+
wiringpi.digitalWrite(GPIO21, LOW)
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
lf.noteLeft(random.randint(0, 15))
|
236
|
+
|
237
|
+
suc = rf.noteRight(random.randint(0, 15))
|
238
|
+
|
239
|
+
time.sleep(2)
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
while i < 28:
|
244
|
+
|
245
|
+
lf.noteLeft(suc)
|
246
|
+
|
247
|
+
suc = rf.noteRight(random.randint(0, 15))
|
248
|
+
|
249
|
+
time.sleep(2)
|
250
|
+
|
251
|
+
i = i+1
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
wiringpi.digitalWrite(GPIO21, HIGH)
|
256
|
+
|
257
|
+
rf.endRight()
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
lf.noteLeft(suc)
|
262
|
+
|
263
|
+
time.sleep(2)
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
wiringpi.digitalWrite(GPIO26, HIGH)
|
268
|
+
|
269
|
+
lf.endLeft()
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
a = False
|
274
|
+
|
275
|
+
```
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
よろしくお願いします。
|
1
動作環境を追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
[こちらのサイト](https://qiita.com/mix_dvd/items/dc53926b83a9529876f7)のソースコードを参考に、複数のLEDを光らせる間に一定以上の音が鳴った場合音が鳴った回数を記録するというプログラムを組もうとしました。
|
2
2
|
|
3
3
|
しかし、複数のLEDを2秒ごとに切り替えるのですが、切り替えのプログラムをwhile構文で書いているため、一定以上の音が鳴った回数の記録とLEDの切り替えが同時に行えません。これを、一定以上の音が鳴った回数の記録を常時行いながらLEDを切り替えたいです。
|
4
|
+
|
5
|
+
動作環境はRaspberry Pi3 TypeBです。
|
4
6
|
|
5
7
|
|
6
8
|
|