Handling and parsing large CSV files

I assume the timestamps are in order. That means you only have to read one line ahead.

struct recordType {float timestamp, float angleX, float angleY, float angle} Record;

void ReadRecord()
{
  Record.timestamp = file.parseFloat();
  Record.angleX = file.parseFloat();
  Record.angleY = file.parseFloat();
  Record.angleZ = file.parseFloat();
}

void setup()
{
  // Open the file

  ReadRecord();
  StartTime = millis();
}

void loop()
{
  // Compare the timestamp of the record to the 
  // current elapsed time. 
  if (millis() - StartTime >= Record.timestamp)
  {
    // send the angles to the servos.
    servoX.write(Record.angleX);
    servoY.write(Record.angleY);
    servoZ.write(Record.angleZ);

    // Read the next record
    ReadRecord();

    // Check for end of file
    if (!file.available())
    {
      // Hit the end of the file
      while (1) {} // Hang here
    }
  }
}
1 Like