Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: SD card openNext does not always work

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    2

    SD card openNext does not always work

    I have a micro SD card in a Teensy 3.6. When I use openNext, about half the time it only goes through the first four files but the rest of the time it works. I have not yet tried another SD card.

    In the code below, if it only goes through four files, there is about a half second pause between printing the file names and the final newline. This pause is not there if it goes through all the files.

    I appreciate the help. Thanks.
    Code:
    #include "SdFat.h"
    
    SdFatSdioEX sd;
    File dirFile;
    
    void setup(){
      Serial.begin(9600);
      while (!Serial) {}
      if (!sd.begin()) {
        sd.initErrorHalt();
        Serial.println("sderror");
    	}
    }
    void loop(){
    	if(Serial.available()){
    		Serial.read();
    		dirFile.open("/", O_READ);
    		traverseDirectory(dirFile);
    		dirFile.close();
    	}
    }
    
    void traverseDirectory (File CFile){
    	File pdfile;
    	CFile.rewind();//This is probabably the only rewind that is in any way relevant
    	pdfile.rewind();
    	sd.vwd()->rewind();
    	
    	while (pdfile.openNext(&CFile, O_READ)){
    		if (pdfile.isHidden()){}
    		else {
    			char fileName[64] = {0};
    			pdfile.getName(fileName, 64);
    			Serial.println(fileName);
    		}
    		pdfile.close();
    	}
    	Serial.println();
    }

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,648
    Try to format the card (Not with windows! - Use the official SD-Format tool: https://www.sdcard.org/downloads/formatter/) - sometimes this helps.
    Last edited by Frank B; 07-12-2019 at 05:04 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    2
    Thank you. That seems to have fixed it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •