SD card writing only works half the time

I have a basic circuit powered via USB by a battery bank ( Infinitive Power Bank 20000 mAh | Walgreens) connected to a standard HiLetgo micro SD read/write module (Amazon.com: HiLetgo 5pcs Micro SD TF Card Adater Reader Module 6Pin SPI Interface Driver Module with chip Level Conversion for Arduino UNO R3 MEGA 2560 Due : Electronics) and a load cell w/ HX711 (https://www.amazon.com/dp/B09WVNJJVC). It's set up to take readings every 5 seconds and assign it into an array, to keep the memory cost down it creates a .txt file and appends the array to the file every 20 readings.

However, occasionally and especially over longer duration runs the card will have a file written to it but with no content at all. It's puzzling as the file is created but completely empty, even missing the pre-made arrays.

Code:

unsigned long minutes = 60000;
#include <SPI.h>
#include <SD.h>
#include "HX711.h"

// HX711.DOUT  - pin #A1
// HX711.PD_SCK - pin #A0

const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_SCK_PIN = 2;
HX711 scale;

String dataString;
float flowRate;
const int total_time = 60*12;
float datlist[20] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 100, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 100};
File myFile;
int iteration = 0;
int sub_iteration = 0;

int pulse = 0;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(394);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0
 
  pinMode(0, OUTPUT);
  pinMode(4, OUTPUT);

}

void loop() {
  //Serial.print(iteration);
  if (iteration == 0) {
    StartTimer();
  }

  if (iteration <= total_time) {

    //Serial.print("reading:\t");
    //long reading = scale.read();
    long reading = scale.get_units();
    Serial.println(reading);
    datlist[sub_iteration] = reading;
    delay(5000);

    iteration++;
    sub_iteration++;
    if (sub_iteration == 20) {
      for (int i = 0; i < sizeof(datlist) / sizeof(*datlist); i++) {
        Serial.println(datlist[i]);
      }
      Write();
      sub_iteration = 0;
    }
  }

  if (iteration > total_time) {
    Write();
    digitalWrite(4, HIGH);
    //Read();
    myFile = SD.open("chamber4.txt");
    myFile.close();
    delay(1000000);
  }
}

void StartTimer() {
  //digitalWrite(0, 50);
  digitalWrite(4, 50);
  delay(500);
  //digitalWrite(0, LOW);
  digitalWrite(4, LOW);
  delay(500);
  //digitalWrite(0, 50);
  digitalWrite(4, 50);
  delay(500);
  //digitalWrite(0, LOW);
  digitalWrite(4, LOW);
  delay(500);
  //digitalWrite(0, 50);
  digitalWrite(4, 50);
  delay(500);
  //digitalWrite(0, LOW);
  digitalWrite(4, LOW);
  delay(500);
  //digitalWrite(0, 50);
  //delay(500);
}

void Write() {
  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
  }
  //Serial.println("initialization done.");
  //if (SD.exists("chamber4.txt")) {
  //  SD.remove("chamber4.txt");
  //}
  myFile = SD.open("chamber4.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    for(int i = 0; i < sizeof(datlist)/sizeof(*datlist); i++)
    {
      myFile.println(datlist[i]);
    }
    // close the file:
    myFile.close();
    //Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    //Serial.println("error opening test.txt");
  }
}

//void Read() {
//   myFile = SD.open("chamber4.txt");
//  if (myFile) {
//    Serial.println("chamber4.txt");
//
//    // read from the file until there's nothing else in it:
//    while (myFile.available()) {
//      Serial.write(myFile.read());
//    }
//    // close the file:
//    myFile.close();
//    digitalWrite(4, 50);
//  } else {
//    // if the file didn't open, print an error:
//    Serial.println("error opening test.txt");
//  }
//  delay(1000000);
//}

bad schematic:

where is SD.begin?

#include <SPI.h>
#include <SD.h>
#include "HX711.h"

const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_SCK_PIN = 2;
HX711 scale;

void setup() {
  Serial.begin(115200);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(394);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0
  pinMode(4, OUTPUT);
  Serial.print("Initializing SD card...");

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

void loop() {
  long reading = scale.get_units();
  Serial.println(reading);
  File myFile = SD.open("chamber4.txt", FILE_WRITE);
  if (myFile) {
    myFile.println(reading);
    myFile.close();
  } else Serial.println(Writing failed);
  StartTimer();
}

void StartTimer() {
  for (int i = 0; i < 6; i++) {
    digitalWrite(4, !digitalRead(4));
    delay(500);
  }
}

how does the SD library know which pin is its CS?
use SD.begin(<cs pin>)

On an Uno with that call, it will use D10 (SS).

why weight sensor adapter powered from Vin? does it need 12V ?

The sensor is 5V, I'm pulling it out (technically running in parallel to) the Arduino power from the bank through the Vin.

I'll check my code but I may have deleted it when pasting the code over, since I do have an SD.begin() in my code, as @SurferTim it defaults to D10.

This looks like it's opening and writing every reading each time right? I'd ideally like to minimize the read/write actions since I'm collecting a ton of points every day and don't trust this to reliably work without corrupting the card unless it's definitely better than my batch write method.

The weird part is that even with my premade array a file is written to the SD card but contains nothing randomly. I'm thinking this may be due to bad connections on the hx711 causing a file to be created but no data to be collected and then it hangs there.

Your power bank will shut down if the current draw drops below a certain level. Could that be happening? Have you tried using another type of power source to see if the problem still happens?

Initially used a power plug through the barrel jack