I modded some code to display files & directories from an Sd card, for a mp3 player.
This code works, but every so often it messes up.
Do I need to close a directory once opened or does that only apply to files.
I tried to use the insert code tab but my browser doesnt support it.
#include <SD.h>
File file;
File entry;
File dir;
boolean dirMem = false;
boolean dirMem1 = false;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
SD.begin(9);
dir = SD.open("/");
//printDirectory();
//delay(2000);
//Serial.println();
//Serial.println("Rewinding, and repeating below:" );
//Serial.println();
//delay(2000);
//root.rewindDirectory();
//printDirectory();
//root.close();
}
void nextDir () {
entry = dir.openNextFile();
Serial.println (entry.name());
file = entry;
if (!entry) {dir = SD.open("/"); nextDir();}
}
void nextFile () {
entry = file.openNextFile();
Serial.println (entry.name());
if (!entry) dirMem = true;
entry.close();
if (dirMem) {dirMem = false; nextDir(); nextFile();}
}
void loop()
{
delay (500);
nextFile();
/*
while (1) {
if (Serial.available()>0) {
int a = Serial.read();
if (a=='d') nextDir();
if (a=='f') nextFile();
}
}
*/
}
This code works, but every so often it messes up.
Do I need to close a directory once opened or does that only apply to files.
I tried to use the insert code tab but my browser doesnt support it.
#include <SD.h>
File file;
File entry;
File dir;
boolean dirMem = false;
boolean dirMem1 = false;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
SD.begin(9);
dir = SD.open("/");
//printDirectory();
//delay(2000);
//Serial.println();
//Serial.println("Rewinding, and repeating below:" );
//Serial.println();
//delay(2000);
//root.rewindDirectory();
//printDirectory();
//root.close();
}
void nextDir () {
entry = dir.openNextFile();
Serial.println (entry.name());
file = entry;
if (!entry) {dir = SD.open("/"); nextDir();}
}
void nextFile () {
entry = file.openNextFile();
Serial.println (entry.name());
if (!entry) dirMem = true;
entry.close();
if (dirMem) {dirMem = false; nextDir(); nextFile();}
}
void loop()
{
delay (500);
nextFile();
/*
while (1) {
if (Serial.available()>0) {
int a = Serial.read();
if (a=='d') nextDir();
if (a=='f') nextFile();
}
}
*/
}