Arduino hanging when writing to SD. Not a memory leak?!

Hi, first time poster, long time peruser...

My problem is this. I have a adafruit 3v Trinket Pro with LipO backpack for power that has as MPU6050 accelerometer & Gyro and an DS1307 Real Time Clock on the I2C bus. I also have an SD card on the SPI bus.

At power on I get the time/date from the realtime clock and put it in a file on the SD (data00.csv).
Then I am taking readings from the MPU6050 and writing them to the SD card along with the millis since power on.

My problem is that writing to the SD stops after a variable amount of time around the 45minute to 2 hour mark. I can see this as the SCK of the SPI is pin 13 on the Trinket Pro, which also shares the inbuilt led. so this goes out...

I have this problem when powered from USB and from the LiPo.

I have added in and used the freemem library to track the memory usage, and there appears to be no memory leak (788bytes free constantly...) (library removed now)

The cycle time for the sketch appears to increase slowly (by 10-20 milliseconds total over an hour). I put this down to writing larger and larger values of millis() to the SD...

I'm completely out of ideas now! Can anyone help? Thanks in advance!

Code attached....

ParkinsonsArduino7_6.ino (10.3 KB)

Sorry, I should also add that the files generated have a hundreds of thousands of lines of output, hence why I have broken the files into 30minute chunks, to see if this was the cause. That and my laptop groaned when excel tried to plot a graph on 350,000 x 6 data points....

Solved!

Hi, this random hang was caused by the loop cycle times being right on the very edge of the SD cards speed rate. This would run fine until the data being written got larger (extra digits on millis()) and then the timings became very, very close, to the point where a small change in motion would add another char into the text file being written and cause the whole thing to halt with SD write errors.

So the fix was to put a small delay into the loop (20ms) to prevent this happening.

Hope this helps someone else....

wow.
cheers!