Write to SD after repower

I'm making something like a datalogger that will write data to a CSV file on a SD card.
I'd like each row of the CSV to be consecutively indexed even after power down/up.

Eg the Arduino logs data in row #1 to #4.
The whole device is powered down. (No back up battery.)
At some time later, it is powered up again.
The arduino starts logging data to the same file and picks up with index #5.

I can't find any suggestions on how to do this.

You can store the row number to be written after next power-up in EEPROM, but there is no need to do so. Close the file before power down and start over on power up, opening and appending more data to the file.

As i understand it, closing and reopening the file allows me to continue writing to it in sequence, but surely the indexing for each row will be reset at every reboot?

I'll look into the eprom.

After some research, I'm trying to use size() and seek() to determine the last number in the .csv file and have the index at the end of the row, rather than at the start.

My test .csv contains the following 3 rows:

7,4,1
8,5,2
9,6,3

This is my code:

hLog = SD.open("h.csv");
int  csvSize = hLog.size();
int  csvLast = hLog.seek(csvSize);
Serial.print ("csvSize = ");
Serial.println (csvSize);
Serial.print ("csvLast = ");
Serial.println (csvLast);
hLog.close();

csvSize is reported as 21 and as far as I can tell, that equates to the 3 numerical digits, +2 commas and (I assume) +2 '/n' for a new line delimiter.

csvLast, however is reported as 1, not as 3, which is what need.

I've tried changing the seek() argument to both various csvLast = hLog.seek(csvSize-n) and just csvLast = hLog.seek(n) and csvLast is always reported as 1.
I've tried changing the variable type too, in case that was an issue, but for int, unsigned long, and byte, csvLast is still always 1 and for char, the serial monitor outputs it's value as a square. (Sorry - don't know what that's called.)

Obviously I'm not understanding how to use seek()
Please can someone enlighten me?

Function File.seek() doesn't return the char at the specified position, its result is boolean flag, indicating whether this position is available for reading or not. Any position in the file with indexes from 0 to filesize is available, so the seek() returns true, which represented as 1 if readied as integer type.

To get the char under position, in addition to seek(), you have to read it

Thanks. It'll be a few days or so before I've got time to get my head roudn that, but thanks for the pointer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.