I'm running simple loops using the Teensyduino iterating a variable (x) from 1 to, say, 10,000. It's a scaled down problem in an attempt to find a solution for a bug in a much larger code. The problem is that after writing a certain number of lines to the SD, it will stop writing for a certain quantity of values, then start back up again, e.g. In the most recent trial it recorded from 1 - 4293, then stopped, and began on the next line (Excel line 4294) with 14581, then continued until I cut it off around 15350. Also, the odd time it will print two values on the same line instead of skipping to the next one.

I've been running the trials alongside Serial port recordings using the PuTTY program (very useful program) which creates a .txt or .csv file with the serial port output.

It's confusing, as in both the larger code and this simple one, the serial outputs are always exactly what is desired, and it's not a fault with the SD card as I've tried using a few different ones and the same problem emerges.

I'm thinking it might be a problem with opening and closing the datafile so often and quickly (would anyone have a better way of doing this?), or else maybe with the SD library.Has anyone else had a similar problem?

I'd still like to know where the data is coming from, and how much of it there is.

What is the risk if data is lost? How much data can you afford to lose? You can write more than one byte at a time to the file. Buffer data (as much as you can afford to lose), and open the file, write the buffered data, and close the file. If the Arduino has problems, like loss of power while the file is not open, then the only data lost will be what was in the buffer, not yet written to the file.

You need to, of course, balance the buffer size with the time needed to write that data to the file.

The idea is to minimize both the amount of time the file is open (and corruptible) and the number of times the file needs to be opened.

After running a series of programs using buffers, it still didn't solve the problem. I ran dozens of trials over the past few days, making small edits to the above code each time, and finally found a solution.

When I removed the SD.begin(chipSelect) line from the loop, the code runs perfectly every time. I will still most likely use a buffer in order to make to code more efficient.

Still, I don't understand why the SD.begin in the loop would cause the program to skip over 10,000 values at the same position every time, after working for more than 4000. For now I'm just satisfied the program runs properly.

Calling it in loop discards a lot of data and may cause file system corruption. SD.h was not designed to call SD.begin() multiple times. SD.h is a wrapper for an old version of SdFat. The wrapper has bugs that prevent multiple calls from always working.

SdFat allows multiple calls to begin/init if all files are closed first. This allows one SD card to be removed and another SD card to be inserted.