Skip to content

SD card example fail #3696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maxcpr opened this issue Aug 18, 2015 · 9 comments
Closed

SD card example fail #3696

maxcpr opened this issue Aug 18, 2015 · 9 comments
Assignees
Labels
Library: SD The SD Arduino library Type: Bug

Comments

@maxcpr
Copy link

maxcpr commented Aug 18, 2015

a little bit changed example from IDE (SD listfiles), file listing moved to "loop".
Code stops listing microSD card after 17-20 loops (arduino nano).
(there is similar code, also listing files on microsd, that works fine on the same hardware)

/*
  Listfiles

 This example shows how print out the files in a 
 directory on a SD card 

 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
 modified 2 Feb 2014
 by Scott Fitzgerald

 This example code is in the public domain.

 */
#include <SPI.h>
#include <SD.h>

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...");
  digitalWrite(10, 1);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(4, 0);   // turn the LED on (HIGH is the voltage level)
  delay(2000); 
  while (!SD.begin(4)) {
    Serial.println("initialization failed!");
    delay(2000); 
//    return;
  }
  Serial.println("initialization done.");
}

void loop(){
  Serial.println("starting!");
  root = SD.open("/");
  printDirectory(root, 0);
  Serial.println("done!");
  delay(2000);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(10, 1);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(4, 0);   // turn the LED on (HIGH is the voltage level)


}

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

     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       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();
   }
}
@facchinm facchinm added Type: Bug Library: SD The SD Arduino library labels Aug 31, 2015
@shiftleftplusone
Copy link

I made it similar, providing a dedicated SDinit() function and aborting after 20 sec timeout:

#define fileIO_OK            +1
#define fileIO_NO_ERR         0
#define fileIO_ERR_CREATE    -1
#define fileIO_ERR_OPEN      -2
#define fileIO_ERR_REMOVE    -3
#define fileIO_ERR_WRITE     -4
#define fileIO_ERR_READ      -5
#define fileIO_ERR_IMPLAUS   -6
#define fileIO_ERR_NAME      -8
#define fileIO_ERR_SDCARD   -16

#define sd_cs    4


//=====================================================================================
// SD init
//=====================================================================================

int16_t SDinit() {
   char sbuf[50];
   uint32_t  tstamp;
   int16_t   ior=0;

   pinMode(13, OUTPUT);
   digitalWrite(13, LOW);
   tstamp = millis();
   ior=SD.begin(sd_cs);  // true on sucess; else false
   while( !ior) {      
      sprintf(sbuf, "#: ...SD not found... ");  Serial.println(sbuf);
      delay(1000);   
      ior=SD.begin(sd_cs);
      if (millis()-tstamp>20000) {Serial.println("#: ...break!"); break; }
   } 
  if(!ior) return fileIO_ERR_SDCARD;     // SD ioresult == 0
  digitalWrite(13, HIGH);
  return fileIO_OK ;            // SD init OK  == 1
} 

@jpk-0320

This comment was marked as off-topic.

@facchinm

This comment was marked as off-topic.

@jpk-0320

This comment was marked as off-topic.

@jpk-0320

This comment was marked as off-topic.

@jpk-0320

This comment was marked as off-topic.

@jpk-0320

This comment was marked as off-topic.

@jpk-0320

This comment was marked as off-topic.

@agdl
Copy link
Member

agdl commented Jul 12, 2016

This issue was moved to arduino-libraries/SD#4

@agdl agdl closed this as completed Jul 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library: SD The SD Arduino library Type: Bug
Projects
None yet
Development

No branches or pull requests

6 participants