Loop interfering with setup

I’m currently making some code for some sensors and want to store the values to an SD card. Problem is that putting the SD card code in the loop causes timing issues so instead I’m trying to save them to an array and then write the array when the program is done. The problem I’m having is that it can’t open the file due to something happening in the loop. I don’t know the exact issue, but here’s my code.

//******** Libraries ***********
#include <SparkFun_Qwiic_Relay.h>
#include <U8glib.h>
#include "SparkFun_Qwiic_Relay.h"
#include "U8glib.h"
#include <Wire.h>
#include <SoftwareSerial.h>
#define rx 2
#define tx 3
#include "TSYS01.h"
#include "MS5837.h"
#define RELAY_ADDR 0x18
#include <SPI.h>
#include <SD.h>
//*******************************

TSYS01 sensor1;
MS5837 sensor;

SoftwareSerial myserial(rx, tx);
Qwiic_Relay relay(RELAY_ADDR);
File myFile;

int currentpos = 0;
int sensordata [] = {};
float relaytime = 0;
int sensortiming = 0;
String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;  

void setup() {

  SD.remove("Output.txt");
  Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(9600);                               //set baud rate for the software serial port to 9600
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);  
  Wire.begin();

  sensor1.init();
  while (!sensor.init()) {
    Serial.println("Init failed!");
    Serial.println("Are SDA/SCL connected correctly?");
    Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL");
    Serial.println("\n\n\n");
    delay(5000);
  }

  if(!relay.begin()) {

    Serial.println("Check connections to Qwiic Relay.");

  } else {

    Serial.println("Ready to flip some switches.");

  float version = relay.singleRelayVersion();

  Serial.print("Firmware Version: ");

  Serial.println(version);

  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("Output.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to Output.txt...");
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening Output.txt");
  }

  myFile = SD.open("Output.txt");
  if (myFile) {
    Serial.println("Output.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("error opening Output.txt");
  }

  sensor.setModel(MS5837::MS5837_30BA);
  sensor.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)
}

//void loop() {
  
//}


void loop() {

  if (relaytime >= 10) { 

    relay.turnRelayOn(); //turn the relay on

    delay(1000); // stay on for 1 second

    relay.turnRelayOff(); //turns relay off

    delay(1000); // delay before second (failsafe) run

    relay.turnRelayOn(); // turns relay on a second time 

    delay(1000); //leave on for one second

    relay.turnRelayOff();

    relaytime = -10; //The time it takes for the relay to execute again (in seconds)

  }
  
  if (sensortiming == 1000) {
    sensor.read();
    sensor1.read();

    Serial.print("Pressure: "); 
    Serial.print(sensor.pressure()); 
    Serial.println(" mbar");
  
    Serial.print("Temperature: "); 
    Serial.print(sensor1.temperature()); 
    Serial.println(" deg C");
  
    Serial.print("Depth: "); 
    Serial.print(sensor.depth()); 
    Serial.println(" m");
  
    Serial.print("Altitude: "); 
    Serial.print(sensor.altitude()); 
    Serial.println(" m above mean sea level");
    Serial.println("");
    sensortiming = 0;
  }

  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    myserial.print('\r');                             //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }
  
  if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;                           //add the char to the var called sensorstring
    if (inchar == '\r') {                             //if the incoming character is a <CR>
      sensor_string_complete = true;                  //set the flag
    }
  }

  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.print("pH: ");
    Serial.println(sensorstring);                     //send that string to the PC's serial monitor
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
  delay(1);
  sensortiming = sensortiming+1;
  relaytime = relaytime+0.001;
}

What messages do you see on the Serial monitor ?

int sensordata [] = {};

How much data were you planning on putting in there?

What does the serial monitor show when you run your code?

You’ve got a lot of literal strings needlessly taking up RAM - your prints could use the F() macro more.

TheMemberFormerlyKnownAsAWOL: int sensordata [] = {};

How much data were you planning on putting in there?

What does the serial monitor show when you run your code?

You've got a lot of literal strings needlessly taking up RAM - your prints could use the F() macro more.

That was the array I was talking about, I took it out of the code example because it didn't interfere with the error. Also, I use every variable except for the array.

UKHeliBob:
What messages do you see on the Serial monitor ?

Capture.PNG

  if (input_string_complete == true)

Where in your program is input_string_complete set to true ?