Pages: [1]   Go Down
Author Topic: On SD card I can not list files after writing to SD card  (Read 1470 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have searched various sites and cannot find a reason or fix for this issue.

The project is a controller / data logger with a web interface to show the data.

I have found that once I have written to a file I can no longer list what files are on the SD card.

The Shield is the Adafruit Ethernet shield R3.

The following is a simple example of the code that displays the issue

Code:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
  Serial.begin(9600);
   pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    return;
  }
  myFile = SD.open("/");
  printDirectory(myFile, 0);
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
  } else {
  }
  myFile = SD.open("/");
  printDirectory(myFile, 0);
}

void loop() {
}

void printDirectory(File dir, int numTabs) {
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     Serial.print("\t\t");
     Serial.println(entry.size(), DEC);
     entry.close();
   }
}

I suspect that I am missing a step to move a pointer back to the start of the directory although running the list does not exhibit the issue.

I have also tested with the Adafruit Datalogger shield and get the same results.

Please help.

Thank you and have a good day

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5853
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would try closing that open file after calling printDirectory().
Code:
void setup()
{
  Serial.begin(9600);
   pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    return;
  }
  myFile = SD.open("/");
  if(myFile) {
    printDirectory(myFile, 0);
    myFile.close();
  }
 
 myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
  }

  myFile = SD.open("/");
  if(myFile) {
    printDirectory(myFile, 0);
    myFile.close();
  }
}
Just a thought...
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1603
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should close files as suggested by SurferTim to avoid memory leaks.

You must position the root directory by calling seek() before calling printDirectory().  SD.h returns a pointer to an internal file object for root and does not reposition root to the beginning.

The best mod is to add the seek call to printDirectory();

Code:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
  Serial.begin(9600);
   pinMode(10, OUTPUT);
  if (!SD.begin()) {
    return;
  }
  myFile = SD.open("/");
  printDirectory(myFile, 0);
  myFile.close();  //// Add close  /////////////
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
  } else {
  }
  myFile = SD.open("/");

  printDirectory(myFile, 0);
  myFile.close();  ////// Add close  /////
}

void loop() {
}

void printDirectory(File dir, int numTabs) {
   dir.seek(0); /////// Add seek ///////////////////
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     Serial.print("\t\t");
     Serial.println(entry.size(), DEC);
     entry.close();
   }
}
Logged

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

Hi all,

Thank you fat16lib the seek(0) did fix the issue.

Have a good day
Logged

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

How can I add a sequence number, the date and time to the file listing the sketch produces?

I experimented with many variations of the commands, without success. The problem lines are marked as //??

Code:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
  Serial.begin(9600);
   pinMode(10, OUTPUT);
  if (!SD.begin()) {
    return;
  }
  myFile = SD.open("/");
  printDirectory(myFile, 0);
  myFile.close();  //// Add close  /////////////
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
  } else {
  }
  myFile = SD.open("/");

  printDirectory(myFile, 0);
  myFile.close();  ////// Add close  /////
}

void loop() {
}

void printDirectory(File dir, int numTabs) {
  //See: http://forum.arduino.cc/index.php?PHPSESSID=0ft62ocf1i787r0vnaou4teaa6&topic=190889.0
   dir.seek(0); /////// Add seek ///////////////////
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
//??     Serial.print(i);Serial.print("\t");
     Serial.print(entry.name());
//     Serial.print("\t\t");
//??     Serial.println(entry.Date(), DEC);
//     Serial.print("\t\t");
//??     Serial.println(entry.Time(), DEC);   
     Serial.print("\t");
     Serial.println(entry.size(), DEC);   
     entry.close();
   }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//??     Serial.print(i);Serial.print("\t");
i is the index in the for loop that is inserting tabs. Why would you want to print the number of tabs?

Quote
The problem lines are marked as //??
What IS the problem with:
Code:
//??     Serial.println(entry.Date(), DEC);
//??     Serial.println(entry.Time(), DEC); 

?

Aside from the fact that the File class does not have Date() and Time() methods. Since the Arduino does not know what time it is, how can you expect it to date and time stamp files on the SD card?
Logged

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

1) I have used the //?? markings to identify the statements that do not work (when you take out the marking of course).
You are right about printing multiple tabs. Thanks!

2) It is easy to time tag files using an Arduino sketch  and some hardware. See:   https://github.com/adafruit/RTClib

Any suggestions to get the date-time and the file size listed anyway (See my earlier post) as part of the sketch listed?
Logged

Pages: [1]   Go Up
Jump to: