我尝试在Arduino SD卡读卡器中读取文本文件,并将其文本复制到一个字符串变量中,但是函数.read始终返回-1。我该如何解决这个问题?
以下是代码:
#include <SPI.h>
#include <SD.h>
File mappa;
String text;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
mappa = SD.open("map.txt");
// if the file opened okay, write to it:
if (mappa) {
Serial.println("File aperto");
} else {
// if the file didn't open, print an error:
Serial.println("error opening map.txt");
}
Serial.println("map.txt:");
// read from the file until there's nothing else in it:
while (mappa.available()) {
Serial.write(mappa.read());
// text = parseInt(mappa.read());
}
Serial.println(text);
// close the file:
mappa.close();
}
void loop() {
// nothing happens after setup
}
我知道
.read()
返回一个整数数组,但我不知道如何单独访问它们。