External interrupts and SD card operations

Hi all,

I have a setup that does continuous datalogging with timestamps. I'm not using an external clock or any other sort of timekeeping other than millis(). At various stages throughout the experiment, it would be useful to have in the datafile an extra line denoting when an experimental parameter is changed. I thought about having the setup() loop write something into the file at the beginning of every initialization and just hitting reset every time I change the experiment, but that's inelegant.

My question concerns interrupts: has anyone had success setting up a pushbutton or something that triggers a function that writes a new line into the datafile? Possible problems could be interrupting a read/write process and thus corrupting the datafile, and I'm wondering if anyone's found a way around that.

An interrupt signals something that must be handled right now. Not later. Not the next iteration of loop. Right now. I can't see that polling a switch pin to see if the user wants to write an extra row to the file is going to cause any problems.

If you do go with the interrupt, though, all you should do in the interrupt handler is set a flag that loop deals with next time around.

In the end, there will be no difference as to when the write to the file occurs.

PaulS:
An interrupt signals something that must be handled right now. Not later. Not the next iteration of loop. Right now. I can't see that polling a switch pin to see if the user wants to write an extra row to the file is going to cause any problems.

If you do go with the interrupt, though, all you should do in the interrupt handler is set a flag that loop deals with next time around.

In the end, there will be no difference as to when the write to the file occurs.

So if I happen to trigger the interrupt mid-file.write, that won't harm anything?

So if I happen to trigger the interrupt mid-file.write, that won't harm anything?

That depends on what the interrupt handler does. If you don't try to anything with the file or the data being written to the file (which is in a buffer that you shouldn't have access to), then, no, there should be no harm.

I'm still not convinced that an interrupt is needed. Polling the switch after each write to the file should be often enough. After all, you can't add a record to the data in the middle of writing data to the file, anyway. Nor would it make sense.

All that the interrupt would do is record the fact that a new record needs to be added to the file, before another regular record is written. The loop() function still needs to see that the flag is set, and do the actual work.