Uno minima R4
Arduino IDE 2.3.8
Adafruit RTC+ SDcard shield (it employs the PFC8523 RTC chip)
Apologies to the weary: this is not rocket science. But I have been bashing my head on this stone for too many hours now, approaching it from multiple angles etc etc. to no avail.
I am working on a simple strain-gauge datalogger, writing a {RTC timestamp + gauge data} as CSV data on a SD card. The data is collected in groups of a few measurements at ~ 1 second intervals, then written to the SD file.
I want to add "blank" lines at the end of each "run", so that I can grab the data and graph it, without having to squint at the values of the RTC to manually group the data. In the example below, I have tried to add the word "end" on each of two lines. But this does not work. I have also tried myFile.println() and other variants on that them.....none of them are working.
Here is my code, which does collect the data and writes it to the SD card ....the problem is solely the addition of the delineating "blank" lines:
#include "HX711.h"
#include "SD.h"
#include "RTClib.h"
RTC_PCF8523 rtc;
#define SD_FAT_TYPE 3
#define PIN_SPI_CS 4
File myFile;
#define FILE_NAME "UTEST.TXT"
HX711 scale;
const int chipSelect = 10;
uint8_t dataPin = 2;
uint8_t clockPin = 3;
float f;
int i;
int j;
int k;
// code for setting up SD and checking RTC
void setup() {
Serial.begin(57600);
i =j = k = 1;
if (!rtc.begin()) { // SETUP RTC MODULE
Serial.println(F("Couldn't find RTC"));
while (1)
;
}
// Setup SD card
if (!SD.begin(chipSelect)) {
Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
while (1)
; // don't do anything more:
}
Serial.println(F("SD CARD INITIALIZED."));
Serial.println(F("--------------------"));
// setup the HX711 module
scale.begin(dataPin, clockPin);
scale.set_scale(2340); // output from the 1 kg beam is now in Gram units
scale.tare();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
}
void loop() {
// open file for writing
if (i <= 5) {
myFile = SD.open(FILE_NAME, FILE_WRITE);
if (myFile) {
} else {
Serial.print(F("SD Card: error on opening file "));
Serial.println(FILE_NAME);
}
Serial.println(F("Writing log to SD Card"));
DateTime now = rtc.now();
Serial.println("write date stamp");
myFile.print(now.year(), DEC); // write timestamp
myFile.print('-');
myFile.print(now.month(), DEC);
myFile.print('-');
myFile.print(now.day(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(" , "); // CSV delimiter between timestamp and data
Serial.println("read & write scale");
f = scale.get_units(5);
myFile.print(f);
myFile.write("\n"); // new line
delay(1000);
i = (i + 1);
}
else {
myFile.println("end");
myFile.println("end");
}
myFile.close();
}