1

I've got this piece of code for reading a list of files from root directory on SD card, which I run on ESP8266 chip (maybe it makes a difference):

  File dir = SD.open("/", O_RDONLY);

  int n = 0;
  while(true)
  {
     File entry =  dir.openNextFile();
     if (!entry) break;
     if (!entry.isDirectory())
     {
       Serial.println(entry.name()); 
       Serial.flush();
       n++;
     }
     entry.close();
  }
  dir.close();
  Serial.println(n);

The code works fine, but can someone please tell me why, when I comment out printing file names to the Serial in this loop, the program crashes? How is this possible? Are the files opened too rapidly or what? And how can I get the list, let's say into a vector<String> without printing it to the Serial?

When I comment out the printing the chip crashes with this in output:

ctx: cont 
sp: 3ffeb540 end: 3ffeb950 offset: 01b0

>>>stack>>>
3ffeb6f0:  000000ff 00007c52 0000012c 3ffea30c  
3ffeb700:  000000c0 3ffea4dc 00000000 40203a70  
3ffeb710:  00000040 000000ff 3ffea318 402042f7  
(...)
3ffeb940:  00000000 00000000 3ffea930 40100378  
<<<stack<<<
ň"_Çc_ÇĎ
4
  • Show the actual code, exactly as commented, which is crashing. – Chris Stratton Apr 11 '16 at 0:26
  • Could it be program optimisation? try assigning name to a global variable. – Code Gorilla Apr 11 '16 at 12:18
  • @ChrisStratton: It's the same code but without these 2 lines: Serial.println(entry.name()); Serial.flush(); I've put the n++; there just to make sure the condition won't get skipped by the compiler. – P.W. Apr 11 '16 at 17:56
  • @Matt: It can't be optimisation - the result is getting out through the Serial, so it can't be skipped. – P.W. Apr 11 '16 at 17:59
0

Try to use "delay(0);" in the loop (or yield() maybe).

1
  • try to elaborate more your answer and why it should help to solve the problem. Perhaps adding a more extensive reference like this – brtiberio Aug 5 '16 at 12:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.