Hi all,
I have an SD card project that that logs data, and can lose power at any time. The only way I seem to be able to keep the files intact is open them, append, and close them--but it seems take so long (relatively speaking).
I'm using an Adafruit Feather M0 board, with their SD card & RTC shield. Their tutorial has me using the SD.h library. Here's a code-snippet of how I'm writing to the SD card:
dataFile = SD.open(fileName, FILE_WRITE); // Seems to take less than 1-ms
dataFile.println(dataString); // Seems to take 3-ms
dataFile.close(); // Seems to take 11-ms.
This is clunky, and not elegant. I know there has to be a better way. I also know there's a small chance, if I lose power before that last line runs, I'm probably still gonna corrupt the file...
Please, can I get some advice?
Thank you,
-Jeff
Gnerffed:
Hi all,
I have an SD card project that that logs data, and can lose power at any time. The only way I seem to be able to keep the files intact is open them, append, and close them--but it seems take so long (relatively speaking).
I'm using an Adafruit Feather M0 board, with their SD card & RTC shield. Their tutorial has me using the SD.h library. Here's a code-snippet of how I'm writing to the SD card:
dataFile = SD.open(fileName, FILE_WRITE); // Seems to take less than 1-ms
dataFile.println(dataString); // Seems to take 3-ms
dataFile.close(); // Seems to take 11-ms.
This is clunky, and not elegant. I know there has to be a better way. I also know there's a small chance, if I lose power before that last line runs, I'm probably still gonna corrupt the file...
Please, can I get some advice?
Thank you,
-Jeff
- Build a circuit that gives you 50ms of advanced warning of power failure.
- Connect the output of this circuit to an Arduino Interrupt pin.
- After receiving the interrupt, close the file.
- Monitor the power failure input, If the input was a false alarm (only a momentary glitch), enter a timing loop that allows the power supply to stabilize.
If the power supply recovers, return to the logging mode.
- If the power failure input stays true, hang in a loop until the processor's brown out circuit activates.
Chuck.
While you say your code is clunky and not elegant, it is also normal and reliable. The 15ms to do the job sounds quite fast, and you haven't explained if and why you have a problem with that. Further, this is not usually seen as a problem by those who voluntarily turn their power off before checking that the SD operation is complete.
I think you should re-assess the risks. If your paranoia is getting the better of you or, indeed the power supply really is so erratic that it is hampering your work, you might consider running the show on batteries and essentially just use the mains to charge them.