Any idea on closing the file on SD card without physical switches?

Hi. I'm using a micro SD card shield on Arduino Mega. I want to program the arduino so that the file is closed and saved on the SD card when the main power to the arduino mega is shut off. I'm currently making a dehumidifier, and when the toggle switch is turned on, the power supply powers the arduino and it starts recording the time, humidity and temperature and save them on the SD card until the toggle switch is turned off. When I used the SD card shield before, I used a physical push button to open and close the SD card, but this time I don't have such a button (I can add one, but I prefer not to). Do you have any idea how I can do this? Do I need to add a button or need an extra action to close the SD card before turing off the switch?

Thanks.

When I used the SD card shield before, I used a physical push button to open and close the SD card,

What? open/close are software functions!

Mark

I'm not very experienced with using SD cards, can you explain why you need to close the file when the power is turned off?

We see this sort of question asked often by people wanting to save some data to EEPROM when the Arduino loses power. The solution will generally be to provide backup power via a capacitor or battery, sense when the Arduino goes to backup power, then save the data. The other option is just to blindly save the data regularly but this may lead to excessive wear if the data changes frequently so the best solution depends on the specific application.

holmes4:
What? open/close are software functions!

Mark

Yes, I knew that it's a software function, but using the switch was the easiest way for me to manually start and stop the recording whenever I wanted, and also I didn't really know how the recording stops without giving a certain condition that tells the arduino to stop recording...

pert:
I'm not very experienced with using SD cards, can you explain why you need to close the file when the power is turned off?

We see this sort of question asked often by people wanting to save some data to EEPROM when the Arduino loses power. The solution will generally be to provide backup power via a capacitor or battery, sense when the Arduino goes to backup power, then save the data. The other option is just to blindly save the data regularly but this may lead to excessive wear if the data changes frequently so the best solution depends on the specific application.

The reason why I want to close the file when the power is turned off is because I am not the person who will use the dehumidifier (I am building it for one of the research at my college, and the people who don't know anything about Arduino will use it in the future) so I want to simplify the system. Another reason is that adding a push button makes the coding a little bit more complicated. For me, "inert the SD card into the SD card slot, turn on the dehumidifier, turn it off when the experiment finishes, and remove the SD card for further analysis" is the simplest steps for everyone. But as you said, adding a small backup battery or saving the data every a few minutes or so would be another option. Thanks for the suggestion anyway.