Problem with SD library

Hello everybody,

After having lasted many hours on my application, I think that the problem comes from the SD library. That is :

if I first want to list files then read/write files, that’s OK. But, if I first read/write files and after I want to list files, the second listing is wrong (missing files).

To show you the problem, I have modified the example “listfile” in which I have added a write file between two listing. The first listing is correct, the write is correct but the second listing is wrong (missing files).

For the test, I have a Mega2560 board with TouchScreen 320 x 240 that supports the SD card reader. I use a
2 Mb SD card on which I have some files at the root…

Did you meet this problem ? Can you test the included sketch and tell me what are the results you obtain.

NOTE, be carefull to the right value to set in : “SD.begin(xx)” ( xx = 53 is for my case)

Thank you for your help.

Pierre

/*
  SD card basic file example
 
 This example shows how to create and destroy an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
#include <SPI.h>
#include <SD.h>

File myFile;
File root;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
  Serial.println();


  ecritFichier();


  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
  while(true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i=0; i<numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs+1);
    } 
    else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

void ecritFichier() {
  Serial.println();

  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    Serial.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
  }
  Serial.println("done!");
  Serial.println();
}

You should post a link to the Touchscreen hardware and a description on how it is connected to the Mega.

You should have posted this in the "Storage" section of the forum.

SurferTim,

my set is as follow : http://www.ebay.com/itm/SainSmart-Mega2560-3-2-TFT-LCD-Shield-Touch-Screen-SD-Reader-for-Arduino-2560-/271019848784

Sorry, I thought that troubleshooting was the right section. How to move to the right sction ?

Sincerely.

Pierre

I'll see if I can get a moderator to move your thread to the Storage section. You will probably get more help there than here.

I don't know how the SD library handles more than one file open at a time. You seem to have two open at once.

Thank you SurferTim,

In the SD.h, it said that “only one file can be open at a time.” It’s what I pay attention to do. But about Handle, nothing is said. So how to release a Handle ?

Pierre

root in your setup is a file handle.

root.close()

You also try to open a file in ecritFichier(); It's file handle is myFile.

You are right.

In the sketch, I have deleted "myFile" and replaced it by "root", so I have just one handle.

I also have added "root.close();" after the first call to printDirectory(root, 0);"

the problem is always the same ???

Pierre