I have a text file on an SD card, and I am trying to print the lines to the serial monitor.
This Code Works:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("c.txt");
if (myFile) {
Serial.println("c.txt:");
while (myFile.available()) {
Serial.write(myFile.read()); //THIS LINE RIGHT HERE
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening c.txt");
}
}
void loop()
{
}
However, if I replace the line Serial.write(myFile.read());
with Serial.println(myFile.read());
, then the code doesn't return the correct lines.
It should return numbers similar to this (albeit 3000 of them):
564
564
564
564
564
564
564
464
564
664
564
If I use Serial.println
it returns numbers like this (and it doesn't stop at 3000 lines):
13
10
54
54
52
13
10
53
54
52
And if I try to do int(myFile.read())
then the integer becomes the wrong numbers (returned by println
), not the right ones (returned by write
).\
How do I get a number returned by Serial.write(myFile.read());
to be an integer?