How to fix some loss of data while write SDC ?

I’m running simple program in my arduino mega2560. this is my little project. i am using it for log my data. but i’ve problem that unsolved within weeks. my problem is lossing some data while write to sd card. Until the present, i’m still seeking the problem (hardware or software).

first, here is my listing program: (on attachment → .ino file)

and the result is become: (on attachment -->.txt file)

the result sometimes more bad than attached result. Sometimes it only a half. But it never have perfect result.

FYI i’ve tried some way to resolve my problem, but its can’t:

  1. i’ve triedwith standard lib SD card but it have same problem;
  2. now i used sd card typr HC class 6 with capacity 4 GB; and i’ve tried other type of sd card, but it got same problem, and sometime the other type is can’t detected by arduino.
  3. i’ve edited file SdFatConfig.h at line #define ARDUINO_FILE_USES_STREAM and #define SD_SPI_CONFIGURATION
  4. i’ve tried add some delay while write prosess to sd card
  5. i’ve tried add some .flush() while write some char into sd card
  6. Sd card formated by windows (fat32)

need some help for my problem please? :’(
(i’m realy sorry for my very bad english. hope can understand my problem, but thanks before)

trip03.txt (2.71 KB)

just_run_01.ino (8.46 KB)

riunizio: 6. Sd card formated by windows (fat32)

Please read the top post of the Storage forum:

http://forum.arduino.cc/index.php?topic=228201.0

Gdunge: Please read the top post of the Storage forum:

http://forum.arduino.cc/index.php?topic=228201.0

Thanks for the suggestion Gdunge, but i've been tried to format with sdcard formater sw. And it's have same problem.

You should not have these objects.

SdFile root;
Sd2Card card;
//SdVolume volume;

You should not have any of these calls.

  card.init();            //Initialize the SD card and configure the I/O pins.
  //volume.init(card);      //Initialize a volume on the SD card.
  //root.openRoot(volume);

You should check the return status of open() like this.

  if (!dataFile.open(filename, O_CREAT | O_APPEND | O_WRITE)) {
     Serial.println("open failed");

  }

close() also returns status so check it. You don't need flush() here and flush() does not return status. sync() has the same functionality and does return status.

    dataFile.flush();
    dataFile.close();

Just use close() like this without a flush call:

  if (!dataFile.close()) {
    Serial.println("close failed");
  }

You can also check status for print to a file but this may only be helpful for the first in a series of print statements since up to 512 bytes will be cached if the first print is successful.

You should verify that a simple example works. Try the ReadWriteSdFat example.

Does the SdFat QuickStart example work? Does the SdInfo example work?

Do not use SdInfo or QuickStart as examples of how to use SdFat. These are diagnostic programs.

Look at the ReadWriteSdFat example for a typical app.

The dataLogger example is also a typical app.

Use the latest SdFat or try SdFat-beta for the latest features and examples.

@fat16lib:

i was tried example works before i write my own program, this examples is work very successful.

And i was applied all your suggestions on my program. But the result is get same problem. Some of my data is missing. Any other suggestions?
(i am sorry for my dullness)

All sdcards have got Write Latency. It means you cannot write to an sdcard during WL. WL could be from a few ms up to 250ms long. You cannot avoid WL. WL is random. http://forum.arduino.cc/index.php?topic=383040.new#new