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:
