Arduino IDE program stops running after 66-67 seconds

Running a flight computer program on Arduino IDE, which prints readings from an altimeter and an accelerometer to both Serial and an SD card. I've attempted to run the program for as long as possible, stationary, just to see if it will last the duration of a flight; but the program always stops running after ~66-67 seconds. I thought it might have been an issue with the sensors, so I made a demo code that just prints "hello" to the SD card, but had the same issue—always stops around 66 seconds. There aren't any error messages—the Serial print lines just stop. Has anyone experienced this/does anyone know why this might be happening?

Hardware: Arduino MKR WAN 1310, Adafruit LSM6DSO32, Adafruit BMP390, and Adafruit SPI Flash SD Card (4GB).

Demo code:

#include <SD.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SensorLab.h>

const int sdCS = 6;

File sdFile;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }
  Serial.println("Serial begin");

  Serial.print("Initializing SD card...");
  if (!SD.begin(sdCS)) {
    Serial.println("SD initialization failed");
  } else {
    Serial.println("SD initialization successful");
  }

  sdFile = SD.open("test01.txt", FILE_WRITE);
  if (sdFile) {
    Serial.print("Writing to file...");
    sdFile.println("Initial write.");
    sdFile.close();
    Serial.println("Writing done.");
  } else {
    Serial.println("Error opening file for writing.");
    while(1);
  }

}

void loop() {
  delay(100);
  
  sdFile = SD.open("test05.txt", FILE_WRITE);
  if (sdFile) {
    sdFile.println("hello");
    sdFile.flush();
    Serial.println("Wrote to file 5.");
  } else {
    Serial.println("Error writing to file.");
  }
  Serial.println("hello");
  // put your main code here, to run repeatedly:

}

Thanks in advance

I suspect the SD code gets confused after you open a file a few hundred times without closing it.

I was under the impression that sdFile.flush() closed and saved the file. Is there something else I need to be including? Thanks

I believe you have it backwards.

close automatically flushes (i.e. ensures that any bytes written to the file are physically saved to the SD card) before closing.

flush does not close.

This is a common beginner mistake and a very bad idea. Opening a file, writing a line or two of data, then closing it again vastly increases the SD card error rate, vastly increases the SD card current draw and shortens the life of the card.

Fix it by opening a file once in setup(), writing data, then closing it only when you are done collecting data. flush() can be used every so often to update the file pointers, so if the power is lost before the file is closed, most of the data will be saved.

The Arduino SD library is very error prone and inherently unreliable, so you need take precautions for it to be useful.

:man_facepalming: Yep, that fixed it. Thank you!

I wasn't aware of this—that's very helpful to know. I'll modify the program to address this.