SD card not reading several lines of files

Hi all. First post on this forum.

I’ve got a project that includes a component where, after receiving a command, my program goes through the attached SD card, opens each file, and prints their contents back to serial. Eventually they’ll go elsewhere, but I’m just trying to get the read part working.

I’ve got it working … except that for some reason, when files are read, a lot of the file does not seem to be getting “read”.

Example: When I send the code du jour, I get:

Setup Complete
Trying to print out data
data1.txt
;4.10;
;19-12-25 4:0:44;0;28;-81;1.30;4.00;
;19-12-25 4:0:50;0;28;-81;1.30;3.90;

But the actual contents of the file are something like:

Header1;Header2;Header3;Header4;Header5;Header6;
;19-12-25 4:0:14;5;28;-81;1.50;5.60;
;19-12-25 4:0:20;0;28;-81;1.50;5.10;
;19-12-25 4:0:26;0;28;-81;1.50;4.50;
;19-12-25 4:0:32;0;28;-81;1.50;4.60;
;19-12-25 4:0:38;0;28;-81;1.30;4.10;
;19-12-25 4:0:44;0;28;-81;1.30;4.00;
;19-12-25 4:0:50;0;28;-81;1.30;3.90;

There really doesn’t seem to be much of a pattern to what gets “read” and what doesn’t, other than that the stuff that’s read is always the end of the file. Some files print more lines than others, some print less. It changes every time I run it. I’m guessing it has something to do with I/O times or memory, but no matter where I seem to drop in a delay the program doesn’t seem to be able to catch up and read the full file. The problem definitely seems to be in the file reading, I’ve tried outputting the data to two different serials (generic Serial back via USB to my PC, and NeoSWSerial to an HC-06 to my Android), and they see the same output.

I could really use some help trying to figure this one out, I’ve Googled a whole bunch and I’m totally stuck. If it matters, I’m using a Nano. Is the memory on the Nano too small for this sort of thing?

I’ve attached the .ino, but the critical section is probably just this part below:

void printData()
{ 
  //Run through the SD card and print out any data in each file
  Serial.println("Trying to print out data");
  btss.println("Trying to print out data");
  int j = 1;
  while ( SD.exists("data"+String(j)+".txt") ) {
    myFile = SD.open("data"+String(j)+".txt");
    btss.println("data"+String(j)+".txt");
    Serial.println("data"+String(j)+".txt");
    String inputStr = "";
    char charIndv;
    while (!myFile){
      Serial.println("Waiting for file to open");
      btss.println("Waiting for file to open");
      delay(1000);
    }
    delay(1000);
    while (myFile.available()) 
    {
      charIndv = myFile.read();
      inputStr = inputStr + String(charIndv);
      //delay(10);
    }
    Serial.println(inputStr);
    btss.println(inputStr);
    j = j + 1;
    myFile.close();
    delay(1000);
  }
  Serial.println("Output Complete");
  btss.println("Output Complete");
}

Let me know if I need to provide additional info or something, again, first time on the Arduino forums. Any help appreciated. Thanks in advance!

BT_TEST_2.ino (1.71 KB)

Update: Resolved and can be closed.

It was a memory issue. Noob mistake, some of these files are definitely too big for the Nano to hold in memory completely. I threw in a 10-character buffer to print and that fixed the problem.

while (myFile.available()) 
    {
      charIndv = myFile.read();
      inputStr = inputStr + String(charIndv);
      //delay(10);
      if (inputStr.length() = 10) { //10-char buffer
        Serial.print(inputStr);
        btss.print(inputStr);
        inputStr = "";
      }
    }
    //Serial.println(inputStr);
    //btss.println(inputStr);