Hi there. I'm a relative newbie to both C/C++ programming and the Arduino as well. I am having a problem with reading from the SD card that I hope you all can help me with. I have tried searching for the solution here on the Arduino forum, and on the internet in general, to no avail.
I have one Arduino that is running a program called GRBL that controls a CNC machine. I am using another Arduino with a TFT display and SD card slot to provide an interface for the Arduino running GRBL. The interface program is getting pretty involved, but all is well except that when reading a text file from the SD there are odd characters that are read at the beginning of the file. These odd characters, when sent to the other Arduino result in an error. Since the code is rather involved, I decided to write a short sketch that just reads a text file and echoes it to the serial port. The only other thing the test code does is ignore '\r' characters. I am getting the same results as my main program, so I think my error lies in this code somewhere. For example, in my test text file, the first line is:
G00G21G90X-0.5375Y3.5375Z3.3598F4000
but what is being read is:
G00G21G90X-0.5375Y3.5375Z3.3598F4000
Those first few characters,  are the problem. I have checked the text file with a text editor and those characters are not in there.
I added a hack workaround to my interface that simply ignores the first three characters and then everything works great, but I want to understand what is going on here. Below is the test code I wrote that generated the exact same problem as my main program. I just can't figure out where these characters are coming from.
Any help would be much appreciated.
#include <SPI.h>
#include <SD.h>
File GCodeFile;
//============================================
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
}
//============================================
void loop() {
if (!SD.begin(10,11,12,13)) {
return;
}
GCodeFile = SD.open("1K.NC");
if (GCodeFile) {
// SD opened
while (GCodeFile.available()){
char TMP = GCodeFile.read();
if (TMP != '\r'){
Serial.print(TMP);
}
}
}
}