Can't write to SD card while in loop

I’m trying to run a program that tracks a circuit and marks every time the current is above the baseline, but for some reason the SD card isn’t saving any text in the loop. I have a test write in the setup and it works fine, but nothing after writes to the card.

// This #include statement was automatically added by the Particle IDE.
#include <SdFat.h>

// This #include statement was automatically added by the Particle IDE.
#include <adafruit-ina219.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_mfGFX.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_ILI9341.h>


double blip=0;
Adafruit_INA219 ina219;
const int LED = D7;
double seconds;
double secondsi=Time.second();
String current;
bool shocked;
File myFilex;
float current_mA = 0;
#define TFT_DC   D5
#define TFT_CS   D4
#define STMPE_CS D3
#define SD_CS    D2
const int SD_CS_PIN = SS;
SdFat SD;
File myFile;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int initial=0;
void setup(void)
{ SPI.begin(SD_CS_PIN);
  digitalWrite(SD_CS_PIN,HIGH);
  tft.begin();
  delay(1000);
  tft.fillScreen(ILI9341_BLACK);
  if (!SD.begin(SD_CS_PIN,SD_SCK_MHZ(50))) {
    tft.println("initialization failed!");
    return;}
  uint32_t currentFrequency;
  pinMode(LED,OUTPUT);
  Serial.begin(115200);
  Serial.println("Hello!");
  //Mesh.connect();
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  digitalWrite(LED,HIGH);
  Wire.begin();
  ina219.begin();
  digitalWrite(LED,LOW);
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  ina219.setCalibration_16V_400mA();
  
  Serial.println("Measuring voltage and current with INA219 ...");
  shocked=false;
  myFile = SD.open("Test1.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    tft.fillScreen(ILI9341_GREEN);
    tft.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
    tft.fillScreen(ILI9341_RED);
   return;
  }
  
}

void loop(void) 
{myFilex=SD.open("Test2.txt", FILE_WRITE);
    if (initial<10){blip+=ina219.getCurrent_mA(); initial+=1;}
else{
if (initial==10){blip=blip/10; initial+=1;}
else{
    bool zero=false;
  
  current_mA = ina219.getCurrent_mA()-blip;
  //Serial.println(current_mA);
  //delay(1000);}
  current=String(current_mA)+" mA";
 // Mesh.publish("Current",current);
  
  if (current_mA>.3&&current_mA<1){
     digitalWrite(LED,HIGH);
      seconds =Time.second();
      myFilex.println("String(seconds-secondsi)");
      digitalWrite(LED,HIGH);
        }
  if (current_mA>1){
      
      seconds =Time.second();
      myFilex.println(seconds-secondsi);}
  }
  
  tft.setCursor(0, 0);
tft.setTextColor( ILI9341_RED, ILI9341_YELLOW);
tft.setTextSize(8);
tft.println(current_mA);
Serial.println(current_mA);
myFilex.close();
digitalWrite(LED,LOW);
}
/*
else if (zero==false&&current_mA<0.3){
zero=true;
tft.setCursor(0, 0);
tft.setTextColor( ILI9341_RED, ILI9341_YELLOW);
tft.setTextSize(8);
tft.println(0.0);
Serial.println(current_mA);
shocked=false;}
  */
}

I would initially suggest a delay in the loop of 1 to 2 seconds to allow the buffers to flush to the SD card. alternatively look at a library such as EDB which offers a clean way to create a set of records on the sd in a simple fashion.

Thank you for your response. I tried to add a delay but it didn’t solve the problem. I also looked into EDB, but it doesn’t look implementable as a particle library. Is there a special way to include it?

I just copied to EDB.h and EDB .cpp files into files in my src model of my project and included the EDB.h one into the .ino

Copy the SD example from the website and it should all work

Here is my test app for this on the web IDE

https://go.particle.io/shared_apps/5e4131d6033ee5002330a753