SD Card and power "outage"

Hi All,

I'm about to write a sketch that writes sensor values to a file on an SD card in every second. I'd like to close/sync the file after every line to avoid dataloss when powering off.
My question is, how to avoid corrupting the file? The arduino will be shut down by pulling out the power chord. What if it happends in the wrong time, and the file on the card get corrupted? Any solution/idea?

Thanks!

Brucez

What if it happends in the wrong time, and the file on the card get corrupted?

Turn a red LED on. Open the file, write to it, close the file. Turn the red LED off. Don't pull the power while the red LED is on.

PaulS:

What if it happends in the wrong time, and the file on the card get corrupted?

Turn a red LED on. Open the file, write to it, close the file. Turn the red LED off. Don't pull the power while the red LED is on.

Thanks, but as it will write in every second and will be powered by a vehicle I won't be able to check the led... Maybe I'll use some kind of conductor for a temporary powersource and will use a digital pin to check if the board is running on main power or on the backup power. The temporary power source should only keep up for 2-3 seconds.

Maybe I'll use some kind of conductor for a temporary powersource and will use a digital pin to check if the board is running on main power or on the backup power. The temporary power source should only keep up for 2-3 seconds.

I'm having a bit of difficulty imagining how that would work. But, if you can, and can figure out how to turn the temporary power source off when the Arduino can safely be powered off, go for it.

PaulS:

Maybe I'll use some kind of conductor for a temporary powersource and will use a digital pin to check if the board is running on main power or on the backup power. The temporary power source should only keep up for 2-3 seconds.

I'm having a bit of difficulty imagining how that would work. But, if you can, and can figure out how to turn the temporary power source off when the Arduino can safely be powered off, go for it.

Well, the temporary power source would be a capacitor. It would discharge in a few seconds, and it would be enough to finish the last write and skip the next write cycles until it shuts down.