Skip to content

SD card example fail #4

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

Open
agdl opened this issue Jul 12, 2016 · 8 comments
Open

SD card example fail #4

agdl opened this issue Jul 12, 2016 · 8 comments
Assignees
Labels
type: imperfection Perceived defect in any part of project

Comments

@agdl
Copy link
Member

agdl commented Jul 12, 2016

From @maxcpr on August 18, 2015 18:2

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();
   }
}

Copied from original issue: arduino/Arduino#3696

@agdl
Copy link
Member Author

agdl commented Jul 12, 2016

From @shiftleftplusone on September 12, 2015 15:34

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
} 

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@agdl

This comment was marked as off-topic.

@per1234 per1234 added type: imperfection Perceived defect in any part of project and removed Library: SD labels Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

3 participants