your new code is great, especially the truncate function is awesome. Using multiple buffers is an improvement as well (although i don't see the point in using 12 - if your logger doesn't manage to write fast enough when using 3 buffers, it's propably too slow and won't catch up after going through 12 ... or does the writing latency vary in such an extreme way?)

nonetheless, I encounter the same problem that I did with my previous code, because the CAN-Controller and the SD-Card controller share the mosi and miso line. Im using this shieldhttp://www.skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.htmlhttp://www.sparkfun.com/datasheets/DevTools/Arduino/canbus_shield-v12.pdf

And i have to set ChipSelectLow(SS_PIN) when I want to write to the sd and put it back to high (not selected) afterwards, because otherwise receiving the CAN-Messages also writes nonsense to the SD card

...If i do not allow interrupts while writing, everything works fine, but i "lose" one or 2 messages during the write process, because the CAN controller can only buffer 2 messages.

I then tried to enable interrupts at various positions in the writeData() function, and after encountering many write errors I found a position that produced no errors and a "valid" log file (formatwise):

But if I do so, I get an incomplete Log file! somehow a lot of data is lost and I have milliseconds of no data. Maybe because of a buffer not actually being written?! ... i don't receive any errors and the program moves on without a hint..

I'm giving up hope, after testing it with your impressive logger. Do you have any hint, as to what I could try?

I'm sorry if this seems like I'm asking you to debug my code ... but I've already invested 15+ hours into solving this interference problem and I'm considering giving up my goal of achieving a Real-Time Capable CAN logger...

edit:do you know, whether calling chipSelectLow/High during the write process does stuff other than just turning the connection on/off ? does it flush a buffer or something like that? .... is this something to consider debugging?

First write latency for SD cards varies widely. Occasionally an SD card has many milliseconds of extra latency so extra buffers help.

Many SD cards require chip select to be held low for the entire 512 byte block write. You can't suddenly switch use of the hardware SPI bus during a block write. SD cards only allow CS for go high at certain times.

This is why I use software SPI for the ADC in the interrupt code.

You will need to use software SPI for the SD, which may be too slow or use software SPI for the CAN bus.

Many SD cards require chip select to be held low for the entire 512 byte block write. You can't suddenly switch use of the hardware SPI bus during a block write. SD cards only allow CS for go high at certain times.This is why I use software SPI for the ADC in the interrupt code.You will need to use software SPI for the SD, which may be too slow or use software SPI for the CAN bus.

So my guesswork went into the right direction ... sadly ...Thanks a million for your quick answer!

I'll reconsider my options and use your logging code for now (it's already extremely performant, even when disabling interrupts while writing!).

Keep up the great work fat16, your SD library already works tremendously well! Thank you!

according to what I understand of this post it is not possible to have hardware SPI on sdcard and ADC. I've got a project working with arduinoMega analog inputs (multiplexer with datalogging at 10Hz). I added MCP3208 to the project, which is working nicely using hardware spi and spi.h. But if I start using the sdcard I've got wrong readings on the MCP3208, also sdfat is not working nicely as I don't get all files I should on the sdcard. Analog reading is done in a time interrupt to have proper frequency. Do I need to unsolder the SPI pin of the MCP3208 to use soft spi or is there another option ? Over question is why sdfat doesn't use spi.h , is it for performances ?

Every SPI device has its own select pin and these should differ! You can only have one device selected simultaneously otherwise there will be interference. The most robust way is to wrap every SPI action with a device select/deselect.

But when you have interrupts using adc, you don't know if you are no gone get interrupt call during sd.write(x). And if you use sei() cli() around sd.write(x) you can drop samples in your adc. That was my assumption for my wrong readings. And I wanted to be sure there was no over option than using soft spi and bit banging for adc.I switched to soft spi for adc and the sketch is working nicely. But I'm still interested to know if there is not another way to do it, perhaps by forcing SS pin of other spi device when getting into the adc interrupt.

thank you fat16lib for your time which you are putting to open-source code and helping others! I am a beginner with Arduino and I appreciate it a lot...

I am using your mcpLogger.pde with ADC MCP3201, but for some reason it save only first two lines to the file. First ,,Log Interval usec: 250" and second ,,4095" or ,,0". I was trying to put different voltage at the input or put it to the ground, but it lives by itself live and always save one of those values. On the serial communication it writes only this:

and then it get stacked. Sometime it ends with FAST08. and sometime just with F. Here comes my question. Do you know, please, where can be the problem? I have Arduino Ethernet and saving it to uSD card. Firstly I thought, that it is because small ram, but this doesnt seem to be the problem. Every time I reset the Arduino board it writes one letter less.