Pages: [1]   Go Down
Author Topic: File list SD card runs only 3 times  (Read 670 times)
0 Members and 1 Guest are viewing this topic.
The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use the slightly modified sample routine for the SD card to list the files in the directory.
After running 3 times the routine fails.
It seems there is a  buffer full or something like that????

Data on serial port:
Code:
DATALOG.TXT 33609
TEST.TXT 288
DATALOG1.TXT 33609
DATALOG4.TXT 33609
DATALO~3.TXT 33609
TEST-K~1.TXT 288
DATALOG3.TXT 33609
DATALOG5.TXT 33609
DATALOG6.TXT 33609
DATALOG2.TXT 33609
done!
DATALOG.TXT 33609
TEST.TXT 288
DATALOG1.TXT 33609
DATALOG4.TXT 33609
DATALO~3.TXT 33609
TEST-K~1.TXT 288
DATALOG3.TXT 33609
DATALOG5.TXT 33609
DATALOG6.TXT 33609
DATALOG2.TXT 33609
done!
DATALOG.TXT 33609
TEST.TXT 288
DATALOG1.TXT 33609
DATALOG4.TXT 33609
DATALO~3.TXT 33609
TEST-K~1.TXT 288
DATALOG3.TXT 33609
DATALOG5.TXT 33609
done!
done!
done!
done!


Code:
#include <SD.h>

File root;

void setup()
{
  Serial.begin(9600);
  pinMode(5, OUTPUT);

  SD.begin(5);
  root = SD.open("/");


 
}

void loop()
{
  printDirectory(root, 0);
  Serial.println("done!");
  delay(5000); 
}

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

     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       // return to the first file in the directory
       dir.rewindDirectory();
       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);
     }
   }
}
 
Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12443
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You aren't closing any File objects...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a bug in the SD library for opening root that causes the position of root to not be at the beginning.

Add a dir.seek(0) to your printDirectory function like this:
Code:
void printDirectory(File dir, int numTabs)
{
  int nEntries = 0;
  char buf[32];
  dir.seek(0);
   while(true) {
     
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       entry.close(); // Added
       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); // no recursion
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
       nEntries++;
     }
     entry.close(); // Added
   }
   
   sprintf( buf, "%d", nEntries );
   
   Serial.println( "" );
   Serial.print( buf );
   Serial.println( " entries found." );
   
}
Logged

Pages: [1]   Go Up
Jump to: