problem with loop reading of SD Card text

Hi, i have this project which accessing a text from SD card and then use that as a value.
But some error happen and i try to fix and googling for the solution but still failed.

Can you guys help me why this is happen?
Here is my sketch :

#include <Wire.h>
/* Include the Hobby Components RTC library */
#include <HCRTC.h>
/* Include the standard SD card library */
#include <SD.h>

/* The RTC has a fixed addresses of 0x68 */
#define I2CDS1307Add 0x68

/* DIO pin used to control the SD card CS pin */
#define SD_CS_DIO 10

/* Create an instance of HCRTC library */
HCRTC HCRTC;

/* Create an instance of the standard SD card library */
File SDFiles;


void setup()
{
  /* Initialise the serial port */
  Serial.begin(9600);

  /* Set the SD card CS pin to an output */ 
  pinMode(SD_CS_DIO, OUTPUT);
  
  /* Use the RTCWrite library function to set the time and date. 
     Parameters are: I2C address, year, month, date, hour, minute, second, 
     day of week. You would normally only need to do this once */
  HCRTC.RTCWrite(I2CDS1307Add, 15, 2, 24, 14, 21, 0, 2);
  
  /* Initialise the SD card */
  if (!SD.begin(SD_CS_DIO)) 
  {
    /* If there was an error output this to the serial port and go no further */
    Serial.println("ERROR: SD card failed to initialise");
    while(1);
  }else
  {
    Serial.println("SD Card OK");
  }

//  SDFiles = SD.open("hit.txt", FILE_WRITE);
//
//  if (SDFiles) 
//  {
//    SDFiles.println("Tomat");
//    SDFiles.println(60);
//    SDFiles.println(3.14);
//    SDFiles.println(3.17);
//    SDFiles.println(3.20);
//    SDFiles.println(3.16);
//   
//    SDFiles.close();
//  }
}

String nama = "";
int count =0;
int hari = 0;
String dump = "";

double x = 0;
double nilai[] = {};
/* Main Loop */
void loop()
{
  SDFiles = SD.open("hit.txt");

  if (SDFiles) 
  {
    while (SDFiles.available()) {
      if (count == 0)
      {
        nama = SDFiles.readStringUntil('\n');
        Serial.print(count);
        Serial.print("\t");
        Serial.println(nama);
        count++;
      }
      else if (count == 1)
      {
        dump = SDFiles.readStringUntil('\n');
        hari = dump.toInt()-54;
        Serial.print(count);
        Serial.print("\t");
        Serial.println(hari);
      count++;
      }
      else
      {
        dump = SDFiles.readStringUntil('\n');
        x = dump.toFloat();
        Serial.print(count);
        Serial.print("\t");
        nilai[count-2] = x;
        Serial.print(x);
        Serial.print("\t");
        Serial.print(count);
        Serial.print("\t");
        Serial.println(nilai[count-2]);

      count++;
      }
          
    }
    SDFiles.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  count = 0;
  Serial.println();
  /* Wait a second before reading again */
  delay(1000);
}

I comment the SD card writing cause the text is already in the SD card.

And here is pic of serial result

Thank you,
Really appreciate this forum as a former shadow member :slight_smile:

What is the error that you observe?

basically i use count to make an if statement and for array.

The error is happen at the end of last line reading of SD card
And then it's affect the next reading as you can see in the image that the tomato word become a weird word

I don't think that the problem is in the reading; after all the first result in the last line (5 and 3.16 is correct); and the second result is incorrect.

You probably have an overflow of the serial transmit buffer; you're sending the serial data faster than the serial port can handle.

A total block on your code is 72 characters (this includes the line-ends '\r\n' that are added by the Serial.println()). At 9600 baud, one character takes about 1ms; your lines are (with the given data) 14 characters long.

The easiest way is to add short delays after writing a line (you have to experiment, 15ms should always work). Another way is to use Serial.availableForWrite to check the available space. And the last one I'm aware of is to use Serial.flush

double nilai[] = {};

You haven't specified how many elements are in the nilai array. I think this will make it zero length or maybe, at most, one element. So, when you start writing into the nilai array you are stomping over other stuff in ram.
Figure out how large it has to be and declare its correct length.

Pete

Good catch

@sterretje: thanks :slight_smile:

Just an FYI: I wrote a trivial sketch to print out the size of an array which does not have the number of elements specified and, as might be expected, its length is zero. I think this should warrant at least a warning from the compiler but it just silently produces a zero length array.

Pete

hm, it has no error when i make it fixed array.

Oh well, for now gotta settle with this lol
Thanks for the reply, i really should ask in this forum rather than in slump for one day