teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

内容簡潔

2017/06/03 06:03

投稿

figero44
figero44

スコア6

title CHANGED
@@ -1,1 +1,1 @@
1
- arduino+RFID+waveshield UID読み取り&音声再生
1
+ arduino+waveshield
body CHANGED
@@ -1,296 +1,14 @@
1
1
  ###前提・実現したいこと
2
- arduinoとwaveshieldとRFID(RC-522)を使用してます。
2
+ arduinoとwaveshieldを使用してます。
3
3
 
4
- RFIDからタグを読み取って、タグ別に音声を再生するようにしたい。
5
-
6
4
  ###発生している問題・エラーメッセージ
7
5
 
8
- コンパイル書き込みはできるが、RFIDの読み取りが行えない。SPI通信のせい?
6
+ 読み取りが行えない。
9
7
 
10
- ###該当のソースコード
11
- /*
12
- * This example plays every .WAV file it finds on the SD card in a loop
13
- */
14
- #include <WaveHC.h>
15
- #include <WaveUtil.h>
16
- #include <FatReader.h>
17
- #include <SdReader.h>
18
- #include <avr/pgmspace.h>
19
- #include "WaveUtil.h"
20
- #include "WaveHC.h"
21
- #include <SPI.h>
22
- #include <MFRC522.h>
23
8
 
24
- SdReader card; // カードの情報を保持します
25
- FatVolume vol; // カード上のパーティションの情報を保持します
26
- FatReader root; // カード上のファイルシステムに関する情報を保持します
27
- FatReader f; // 再生しているファイルの情報を保持します
28
-
29
- WaveHC wave; // これは唯一の波(オーディオ)オブジェクトです。これは一度に1つしか再生しないためです
30
-
31
- uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)ファイル/ディレクトリ名のインデントレベル(きれいな印刷用)
32
- dir_t dirBuf; // ディレクトリ読み込み用のバッファ
33
-
34
- /*
35
- * Define macro to put error messages in flash memory フラッシュメモリにエラーメッセージを入れるマクロを定義する
36
- */
37
- #define error(msg) error_P(PSTR(msg))
38
-
39
-
40
- #define RST_PIN 9 // 設定する、ピンレイアウトを指定
41
- #define SS_PIN 8 // 設定する、ピンレイアウトを指定
42
-
43
- #define UID1 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
44
- #define UID2 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
45
- #define UID3 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
46
- #define UID4 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
47
- #define UID5 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
48
- #define UID6 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
49
- #define UID7 "xx xx xx xx xx xx xx" // DumpInfo.inoで読み取ったUIDを記入
50
-
51
- MFRC522 mfrc522(SS_PIN, RST_PIN); //MFRC522インスタンスを作成する
52
-
53
- MFRC522::MIFARE_Key key;
54
-
55
-
56
- // Function definitions (we define them here, but the code is below)
57
- //void play(FatReader &dir);
58
-
59
- //////////////////////////////////// SETUP
60
- void setup()
61
- {
62
- Serial.begin(9600); //PCとのシリアル通信を初期化する
63
- while (!Serial); // シリアルポートが開かれていない場合は何もしない (added for Arduinos based on ATMEGA32U4)
64
- SPI.begin(); // SPIバスの初期化
65
- mfrc522.PCD_Init(); // MFRC522の初期化
66
- mfrc522.PCD_DumpVersionToSerial(); // PCD - MFRC522カードリーダー詳細
67
- Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
68
-
69
- dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
70
-
71
-
72
- putstring_nl("\nWave test!"); // 起動確認表示
73
-
74
- putstring("Free RAM: "); // これはデバッグに役立ちますが、RAMが不足していると推奨しない
75
- Serial.println(FreeRam());
76
-
77
- // if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
78
- if (!card.init())
79
- { //play with 8 MHz spi (default faster!)
80
- error("Card init. failed!"); // Something went wrong, lets print out why
81
-
82
- }
83
-
84
- // 最適化読み取りを有効にする - 一部のカードがタイムアウトすることがあります。 問題がある場合は無効にする
85
- card.partialBlockRead(true);
86
-
87
- // Now we will look for a FAT partition!
88
- uint8_t part;
89
- for (part = 0; part < 5; part++)
90
- {
91
- // we have up to 5 slots to look in
92
- if (vol.init(card, part))
93
- break; // we found one, lets bail
94
- }
95
- if (part == 5)
96
- {
97
- // if we ended up not finding one :(
98
- error("No valid FAT partition!"); // Something went wrong, lets print out why
99
- }
100
-
101
- // ユーザーに見つけたものを伝える
102
- putstring("Using partition ");
103
- Serial.print(part, DEC);
104
- putstring(", type is FAT");
105
- Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?
106
-
107
- // Try to open the root directory
108
- if (!root.openRoot(vol))
109
- {
110
- error("Can't open root dir!"); // Something went wrong,
111
- }
112
-
113
- // Whew! We got past the tough parts.
114
- putstring_nl("Files found (* = fragmented):");
115
-
116
- // Print out all of the files in all the directories.
117
- root.ls(LS_R | LS_FLAG_FRAGMENTED);
118
-
119
- }
120
-
121
- //////////////////////////////////// LOOP
122
- void loop() {
123
-
124
- // 新しいカードを探す
125
- if ( ! mfrc522.PICC_IsNewCardPresent()) {
126
- return;
127
- }
128
- putstring_nl("new card present");
129
- // カードの1つを選択ぶ
130
- if ( ! mfrc522.PICC_ReadCardSerial()) {
131
- return;
132
- }
133
- putstring_nl("read card serial ok");
134
- String strBuf[mfrc522.uid.size];
135
- for (byte i = 0; i < mfrc522.uid.size; i++) {
136
- strBuf[i] = String(mfrc522.uid.uidByte[i], HEX); // (E)一定の整数を使用して
137
- if(strBuf[i].length() == 1){ // 1桁の場合は先頭に0を追加
138
- strBuf[i] = "0" + strBuf[i];
139
- }
140
- }
141
-
142
-
143
- String strUID = strBuf[0] + " " + strBuf[1] + " " + strBuf[2] + " " + strBuf[3];
144
- if ( strUID.equalsIgnoreCase(UID1) ){ // 大文字小文字関係なく比較
145
- Serial.println("1");
146
- playfile("DO.WAV");
147
-
148
- }
149
- else if ( strUID.equalsIgnoreCase(UID2) ){
150
- Serial.println("2");
151
- }
152
- else if ( strUID.equalsIgnoreCase(UID3) ){
153
- Serial.println("3");
154
- }
155
- else if ( strUID.equalsIgnoreCase(UID4) ){
156
- Serial.println("4");
157
- }
158
- else if ( strUID.equalsIgnoreCase(UID5) ){
159
- Serial.println("5");
160
- }
161
- else if ( strUID.equalsIgnoreCase(UID6) ){
162
- Serial.println("6");
163
- }
164
- else if (strUID.equalsIgnoreCase(UID7) ){
165
- Serial.println("7");
166
- }
167
-
168
-
169
- }
170
-
171
- /**
172
- * Helper routine to dump a byte array as hex values to Serial.シリアルに16進値としてバイト配列をダンプするヘルパールーチン。
173
- */
174
- void dump_byte_array(byte *buffer, byte bufferSize) {
175
- for (byte i = 0; i < bufferSize; i++) {
176
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
177
- Serial.print(buffer[i], HEX);
178
- }
179
- }
180
-
181
- byte i;
182
-
183
- // 一時停止することなく最初から最後まで完全なファイルを再生します。
184
- void playcomplete(char *name) {
185
- // ヘルパーを呼び出してこの名前を見つけて再生する
186
- playfile(name);
187
- while (wave.isplaying) {
188
- // 再生中は何もしない
189
- }
190
- // 再生終了
191
- }
192
-
193
- void playfile(char *name) {
194
- // see if the wave object is currently doing something
195
- if (wave.isplaying) {// already playing something, so stop it!
196
- wave.stop(); // stop it
197
- }
198
- // look in the root directory and open the file
199
- if (!f.open(root, name)) {
200
- putstring("Couldn't open file "); Serial.print(name); return;
201
- }
202
- // OK read the file and turn it into a wave object
203
- if (!wave.create(f)) {
204
- putstring_nl("Not a valid WAV"); return;
205
- }
206
-
207
- // ok time to play! start playback
208
- wave.play();
209
- }
210
-
211
- /////////////////////////////////// HELPERS
212
- /*
213
- * print error message and halt
214
- */
215
-
216
- void error_P(const char *str) {
217
- PgmPrint("Error: ");
218
- SerialPrint_P(str);
219
- sdErrorCheck();
220
- while(1);
221
- }
222
-
223
- /*
224
- * print error message and halt if SD I/O error, great for debugging!
225
- */
226
-
227
- void sdErrorCheck(void) {
228
- if (!card.errorCode()) return;
229
- PgmPrint("\r\nSD I/O error: ");
230
- Serial.print(card.errorCode(), HEX);
231
- PgmPrint(", ");
232
- Serial.println(card.errorData(), HEX);
233
- while(1);
234
- }
235
-
236
- /*
237
- * play recursively - possible stack overflow if subdirectories too nested
238
- */
239
- void play(FatReader &dir) {
240
- FatReader file;
241
- while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time
242
-
243
- // Skip it if not a subdirectory and not a .WAV file
244
- if (!DIR_IS_SUBDIR(dirBuf)
245
- && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
246
- continue;
247
- }
248
-
249
- Serial.println(); // clear out a new line
250
-
251
- for (uint8_t i = 0; i < dirLevel; i++) {
252
- Serial.write(' '); // this is for prettyprinting, put spaces in front
253
- }
254
- if (!file.open(vol, dirBuf)) { // open the file in the directory
255
- error("file.open failed"); // something went wrong
256
- }
257
-
258
- if (file.isDir()) { // check if we opened a new directory
259
- putstring("Subdir: ");
260
- printEntryName(dirBuf);
261
- Serial.println();
262
- dirLevel += 2; // add more spaces
263
- // play files in subdirectory
264
- play(file); // recursive!
265
- dirLevel -= 2;
266
- }
267
- else {
268
- // Aha! we found a file that isnt a directory
269
- putstring("Playing ");
270
- printEntryName(dirBuf); // print it out
271
- if (!wave.create(file)) { // Figure out, is it a WAV proper?
272
- putstring(" Not a valid WAV"); // ok skip it
273
- } else {
274
- Serial.println(); // Hooray it IS a WAV proper!
275
- wave.play(); // make some noise!
276
-
277
- uint8_t n = 0;
278
- while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
279
- putstring(".");
280
- if (!(++n % 32))Serial.println();
281
- delay(100);
282
- }
283
- sdErrorCheck(); // everything OK?
284
- // if (wave.errors)Serial.println(wave.errors); // wave decoding errors
285
- }
286
- }
287
- }
288
- }
289
-
290
9
  ###試したこと
291
10
 
292
11
 
293
12
  ###補足情報(言語/FW/ツール等のバージョンなど)
294
13
  arduino uno
295
- Adafruit Waveシールドキット v1.1
14
+ Adafruit Waveシールドキット v1.1
296
- RC-522 RFIDカードリーダー