[追記]
この記事を書いたのが、ほぼ、1年前になるけど、どうやら、Seeedのライブラリがアップデートされているようで、新しい、ライブラリでやると、なんとか、問題なく使えるようになっているみたいだ。
新しいライブラリは、以下のリンクで。
github.com
このライブラリをインクルードして、以前の
#include <SPI.h> #include <Seeed_FS.h> #include <SD/Seeed_SD.h>
のところを
"#include <Seeed_Arduino_FS.h>"
に、変更してやれば、思うように使えるようになった。
変更前のライブラリでも、公開されている利用例もあるので、この問題が、自分だけだったのか、他の方にも生じていたのかよくわからないけど、一応、自分が、忘れないための記録として残しておくことにします。
なんだか、すっきりしないけど、まぁ、いいか…。
一応、1年前の記事も、以下に置いとくことにします。
*********************************************************
SDカードのファイルのRead / Write がうまくいかない。
Wiki のサンプルをまんまコピーしてもハングしてしまう。
これって、僕だけ?
皆さん、問題なく動いてるのかなぁ…??
コンパイルもアップロードも問題なくて、エラーにも遭遇しないのだけど、ファイルはできてるけど、ファイルにデータは、かけてなくて、読めない状態。
いろいろ試してみると、どうも、File 変数の宣言と実際のファイルオープンを分けるとうまくいかなくて、宣言と同時にオープンすると動くみたい。
でも、そうなると、グローバルでできないので、いろいろ、不便だなぁ~、なんとかならんかなぁ~…。
ちなみに、読み込みだけで見た場合、これは、できる。
#include <SPI.h> #include <Seeed_FS.h> #include <SD/Seeed_SD.h> //File myFile; void setup() { Serial.begin(115200); while (!Serial) { } Serial.print("Initializing SD card..."); if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) { Serial.println("initialization failed!"); while (1); } Serial.println("initialization done."); // re-open the file for reading: File myFile = SD.open("test.txt", FILE_READ); // <-------ここのとこ if (myFile) { Serial.println("test.txt:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.write(myFile.read()); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } } void loop() { // nothing happens after setup }
でも、これはできない。
#include <SPI.h> #include <Seeed_FS.h> #include <SD/Seeed_SD.h> File myFile; // <-------ここのとこ void setup() { Serial.begin(115200); while (!Serial) { } Serial.print("Initializing SD card..."); if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) { Serial.println("initialization failed!"); while (1); } Serial.println("initialization done."); // re-open the file for reading: myFile = SD.open("test.txt", FILE_READ); // <-------ここのとこ if (myFile) { Serial.println("test.txt:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.write(myFile.read()); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } } void loop() { // nothing happens after setup }
で、これもできなかった。
#include <SPI.h> #include <Seeed_FS.h> #include <SD/Seeed_SD.h> void setup() { Serial.begin(115200); while (!Serial) { } Serial.print("Initializing SD card..."); if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) { Serial.println("initialization failed!"); while (1); } Serial.println("initialization done."); // re-open the file for reading: File myFile; // <-------ここのとこ myFile = SD.open("test.txt", FILE_READ); // <-------ここのとこ if (myFile) { Serial.println("test.txt:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.write(myFile.read()); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } } void loop() { // nothing happens after setup }
なんでじゃろ????