質問編集履歴

1

内容簡潔

2017/06/03 06:03

投稿

figero44
figero44

スコア6

test CHANGED
@@ -1 +1 @@
1
- arduino+RFID+waveshield UID読み取り&音声再生
1
+ arduino+waveshield
test CHANGED
@@ -1,10 +1,6 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- arduinoとwaveshieldとRFID(RC-522)を使用してます。
3
+ arduinoとwaveshieldを使用してます。
4
-
5
-
6
-
7
- RFIDからタグを読み取って、タグ別に音声を再生するようにしたい。
8
4
 
9
5
 
10
6
 
@@ -12,567 +8,9 @@
12
8
 
13
9
 
14
10
 
15
- コンパイル書き込みはできるが、RFIDの読み取りが行えない。SPI通信のせい?
11
+ 読み取りが行えない。
16
12
 
17
13
 
18
-
19
- ###該当のソースコード
20
-
21
- /*
22
-
23
- * This example plays every .WAV file it finds on the SD card in a loop
24
-
25
- */
26
-
27
- #include <WaveHC.h>
28
-
29
- #include <WaveUtil.h>
30
-
31
- #include <FatReader.h>
32
-
33
- #include <SdReader.h>
34
-
35
- #include <avr/pgmspace.h>
36
-
37
- #include "WaveUtil.h"
38
-
39
- #include "WaveHC.h"
40
-
41
- #include <SPI.h>
42
-
43
- #include <MFRC522.h>
44
-
45
-
46
-
47
- SdReader card; // カードの情報を保持します
48
-
49
- FatVolume vol; // カード上のパーティションの情報を保持します
50
-
51
- FatReader root; // カード上のファイルシステムに関する情報を保持します
52
-
53
- FatReader f; // 再生しているファイルの情報を保持します
54
-
55
-
56
-
57
- WaveHC wave; // これは唯一の波(オーディオ)オブジェクトです。これは一度に1つしか再生しないためです
58
-
59
-
60
-
61
- uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)ファイル/ディレクトリ名のインデントレベル(きれいな印刷用)
62
-
63
- dir_t dirBuf; // ディレクトリ読み込み用のバッファ
64
-
65
-
66
-
67
- /*
68
-
69
- * Define macro to put error messages in flash memory フラッシュメモリにエラーメッセージを入れるマクロを定義する
70
-
71
- */
72
-
73
- #define error(msg) error_P(PSTR(msg))
74
-
75
-
76
-
77
-
78
-
79
- #define RST_PIN 9 // 設定する、ピンレイアウトを指定
80
-
81
- #define SS_PIN 8 // 設定する、ピンレイアウトを指定
82
-
83
-
84
-
85
- #define UID1 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
86
-
87
- #define UID2 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
88
-
89
- #define UID3 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
90
-
91
- #define UID4 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
92
-
93
- #define UID5 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
94
-
95
- #define UID6 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
96
-
97
- #define UID7 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
98
-
99
-
100
-
101
- MFRC522 mfrc522(SS_PIN, RST_PIN); //MFRC522インスタンスを作成する
102
-
103
-
104
-
105
- MFRC522::MIFARE_Key key;
106
-
107
-
108
-
109
-
110
-
111
- // Function definitions (we define them here, but the code is below)
112
-
113
- //void play(FatReader &dir);
114
-
115
-
116
-
117
- //////////////////////////////////// SETUP
118
-
119
- void setup()
120
-
121
- {
122
-
123
- Serial.begin(9600); //PCとのシリアル通信を初期化する
124
-
125
- while (!Serial); // シリアルポートが開かれていない場合は何もしない (added for Arduinos based on ATMEGA32U4)
126
-
127
- SPI.begin(); // SPIバスの初期化
128
-
129
- mfrc522.PCD_Init(); // MFRC522の初期化
130
-
131
- mfrc522.PCD_DumpVersionToSerial(); // PCD - MFRC522カードリーダー詳細
132
-
133
- Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
134
-
135
-
136
-
137
- dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
138
-
139
-
140
-
141
-
142
-
143
- putstring_nl("\nWave test!"); // 起動確認表示
144
-
145
-
146
-
147
- putstring("Free RAM: "); // これはデバッグに役立ちますが、RAMが不足していると推奨しない
148
-
149
- Serial.println(FreeRam());
150
-
151
-
152
-
153
- // if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
154
-
155
- if (!card.init())
156
-
157
- { //play with 8 MHz spi (default faster!)
158
-
159
- error("Card init. failed!"); // Something went wrong, lets print out why
160
-
161
-
162
-
163
- }
164
-
165
-
166
-
167
- // 最適化読み取りを有効にする - 一部のカードがタイムアウトすることがあります。 問題がある場合は無効にする
168
-
169
- card.partialBlockRead(true);
170
-
171
-
172
-
173
- // Now we will look for a FAT partition!
174
-
175
- uint8_t part;
176
-
177
- for (part = 0; part < 5; part++)
178
-
179
- {
180
-
181
- // we have up to 5 slots to look in
182
-
183
- if (vol.init(card, part))
184
-
185
- break; // we found one, lets bail
186
-
187
- }
188
-
189
- if (part == 5)
190
-
191
- {
192
-
193
- // if we ended up not finding one :(
194
-
195
- error("No valid FAT partition!"); // Something went wrong, lets print out why
196
-
197
- }
198
-
199
-
200
-
201
- // ユーザーに見つけたものを伝える
202
-
203
- putstring("Using partition ");
204
-
205
- Serial.print(part, DEC);
206
-
207
- putstring(", type is FAT");
208
-
209
- Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?
210
-
211
-
212
-
213
- // Try to open the root directory
214
-
215
- if (!root.openRoot(vol))
216
-
217
- {
218
-
219
- error("Can't open root dir!"); // Something went wrong,
220
-
221
- }
222
-
223
-
224
-
225
- // Whew! We got past the tough parts.
226
-
227
- putstring_nl("Files found (* = fragmented):");
228
-
229
-
230
-
231
- // Print out all of the files in all the directories.
232
-
233
- root.ls(LS_R | LS_FLAG_FRAGMENTED);
234
-
235
-
236
-
237
- }
238
-
239
-
240
-
241
- //////////////////////////////////// LOOP
242
-
243
- void loop() {
244
-
245
-
246
-
247
- // 新しいカードを探す
248
-
249
- if ( ! mfrc522.PICC_IsNewCardPresent()) {
250
-
251
- return;
252
-
253
- }
254
-
255
- putstring_nl("new card present");
256
-
257
- // カードの1つを選択ぶ
258
-
259
- if ( ! mfrc522.PICC_ReadCardSerial()) {
260
-
261
- return;
262
-
263
- }
264
-
265
- putstring_nl("read card serial ok");
266
-
267
- String strBuf[mfrc522.uid.size];
268
-
269
- for (byte i = 0; i < mfrc522.uid.size; i++) {
270
-
271
- strBuf[i] = String(mfrc522.uid.uidByte[i], HEX); // (E)一定の整数を使用して
272
-
273
- if(strBuf[i].length() == 1){ // 1桁の場合は先頭に0を追加
274
-
275
- strBuf[i] = "0" + strBuf[i];
276
-
277
- }
278
-
279
- }
280
-
281
-
282
-
283
-
284
-
285
- String strUID = strBuf[0] + " " + strBuf[1] + " " + strBuf[2] + " " + strBuf[3];
286
-
287
- if ( strUID.equalsIgnoreCase(UID1) ){ // 大文字小文字関係なく比較
288
-
289
- Serial.println("1");
290
-
291
- playfile("DO.WAV");
292
-
293
-
294
-
295
- }
296
-
297
- else if ( strUID.equalsIgnoreCase(UID2) ){
298
-
299
- Serial.println("2");
300
-
301
- }
302
-
303
- else if ( strUID.equalsIgnoreCase(UID3) ){
304
-
305
- Serial.println("3");
306
-
307
- }
308
-
309
- else if ( strUID.equalsIgnoreCase(UID4) ){
310
-
311
- Serial.println("4");
312
-
313
- }
314
-
315
- else if ( strUID.equalsIgnoreCase(UID5) ){
316
-
317
- Serial.println("5");
318
-
319
- }
320
-
321
- else if ( strUID.equalsIgnoreCase(UID6) ){
322
-
323
- Serial.println("6");
324
-
325
- }
326
-
327
- else if (strUID.equalsIgnoreCase(UID7) ){
328
-
329
- Serial.println("7");
330
-
331
- }
332
-
333
-
334
-
335
-
336
-
337
- }
338
-
339
-
340
-
341
- /**
342
-
343
- * Helper routine to dump a byte array as hex values to Serial.シリアルに16進値としてバイト配列をダンプするヘルパールーチン。
344
-
345
- */
346
-
347
- void dump_byte_array(byte *buffer, byte bufferSize) {
348
-
349
- for (byte i = 0; i < bufferSize; i++) {
350
-
351
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
352
-
353
- Serial.print(buffer[i], HEX);
354
-
355
- }
356
-
357
- }
358
-
359
-
360
-
361
- byte i;
362
-
363
-
364
-
365
- // 一時停止することなく最初から最後まで完全なファイルを再生します。
366
-
367
- void playcomplete(char *name) {
368
-
369
- // ヘルパーを呼び出してこの名前を見つけて再生する
370
-
371
- playfile(name);
372
-
373
- while (wave.isplaying) {
374
-
375
- // 再生中は何もしない
376
-
377
- }
378
-
379
- // 再生終了
380
-
381
- }
382
-
383
-
384
-
385
- void playfile(char *name) {
386
-
387
- // see if the wave object is currently doing something
388
-
389
- if (wave.isplaying) {// already playing something, so stop it!
390
-
391
- wave.stop(); // stop it
392
-
393
- }
394
-
395
- // look in the root directory and open the file
396
-
397
- if (!f.open(root, name)) {
398
-
399
- putstring("Couldn't open file "); Serial.print(name); return;
400
-
401
- }
402
-
403
- // OK read the file and turn it into a wave object
404
-
405
- if (!wave.create(f)) {
406
-
407
- putstring_nl("Not a valid WAV"); return;
408
-
409
- }
410
-
411
-
412
-
413
- // ok time to play! start playback
414
-
415
- wave.play();
416
-
417
- }
418
-
419
-
420
-
421
- /////////////////////////////////// HELPERS
422
-
423
- /*
424
-
425
- * print error message and halt
426
-
427
- */
428
-
429
-
430
-
431
- void error_P(const char *str) {
432
-
433
- PgmPrint("Error: ");
434
-
435
- SerialPrint_P(str);
436
-
437
- sdErrorCheck();
438
-
439
- while(1);
440
-
441
- }
442
-
443
-
444
-
445
- /*
446
-
447
- * print error message and halt if SD I/O error, great for debugging!
448
-
449
- */
450
-
451
-
452
-
453
- void sdErrorCheck(void) {
454
-
455
- if (!card.errorCode()) return;
456
-
457
- PgmPrint("\r\nSD I/O error: ");
458
-
459
- Serial.print(card.errorCode(), HEX);
460
-
461
- PgmPrint(", ");
462
-
463
- Serial.println(card.errorData(), HEX);
464
-
465
- while(1);
466
-
467
- }
468
-
469
-
470
-
471
- /*
472
-
473
- * play recursively - possible stack overflow if subdirectories too nested
474
-
475
- */
476
-
477
- void play(FatReader &dir) {
478
-
479
- FatReader file;
480
-
481
- while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time
482
-
483
-
484
-
485
- // Skip it if not a subdirectory and not a .WAV file
486
-
487
- if (!DIR_IS_SUBDIR(dirBuf)
488
-
489
- && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
490
-
491
- continue;
492
-
493
- }
494
-
495
-
496
-
497
- Serial.println(); // clear out a new line
498
-
499
-
500
-
501
- for (uint8_t i = 0; i < dirLevel; i++) {
502
-
503
- Serial.write(' '); // this is for prettyprinting, put spaces in front
504
-
505
- }
506
-
507
- if (!file.open(vol, dirBuf)) { // open the file in the directory
508
-
509
- error("file.open failed"); // something went wrong
510
-
511
- }
512
-
513
-
514
-
515
- if (file.isDir()) { // check if we opened a new directory
516
-
517
- putstring("Subdir: ");
518
-
519
- printEntryName(dirBuf);
520
-
521
- Serial.println();
522
-
523
- dirLevel += 2; // add more spaces
524
-
525
- // play files in subdirectory
526
-
527
- play(file); // recursive!
528
-
529
- dirLevel -= 2;
530
-
531
- }
532
-
533
- else {
534
-
535
- // Aha! we found a file that isnt a directory
536
-
537
- putstring("Playing ");
538
-
539
- printEntryName(dirBuf); // print it out
540
-
541
- if (!wave.create(file)) { // Figure out, is it a WAV proper?
542
-
543
- putstring(" Not a valid WAV"); // ok skip it
544
-
545
- } else {
546
-
547
- Serial.println(); // Hooray it IS a WAV proper!
548
-
549
- wave.play(); // make some noise!
550
-
551
-
552
-
553
- uint8_t n = 0;
554
-
555
- while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
556
-
557
- putstring(".");
558
-
559
- if (!(++n % 32))Serial.println();
560
-
561
- delay(100);
562
-
563
- }
564
-
565
- sdErrorCheck(); // everything OK?
566
-
567
- // if (wave.errors)Serial.println(wave.errors); // wave decoding errors
568
-
569
- }
570
-
571
- }
572
-
573
- }
574
-
575
- }
576
14
 
577
15
 
578
16
 
@@ -587,5 +25,3 @@
587
25
  arduino uno
588
26
 
589
27
  Adafruit Waveシールドキット v1.1
590
-
591
- RC-522 RFIDカードリーダー