• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sd card opening files & directories

Status
Not open for further replies.

dr pepper

Well-Known Member
Most Helpful Member
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();
}
}
*/
}
 

dr pepper

Well-Known Member
Most Helpful Member
Ok so not many people have done this.

I managed to get the thing to work, fairly well so far, when you put a sd card in the slot the player will play the first file in the first folder, then the next & the next & then open the next folder & play that, the whole Sd is played, mp3, wma and wav files all work.
You can skip forward a track, or a folder, but not back yet I'm working on that., the sd library has opennextfile, but not openlastfile, you have rewind directory but that takes you to the start of a folder.
Sound quality from the VS1053 is very good.
 

dr pepper

Well-Known Member
Most Helpful Member
Any ideas how I can return to root directory on the SD card?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I suppose there are not many with this kind of knowlege... I will download the library... Most FAT systems need "." or "\" to return upwards... But I didn't write the library...

Link https://www.arduino.cc/en/reference/SD
Have you tried rewindDirectory();
 

dr pepper

Well-Known Member
Most Helpful Member
I'm not really a dos man, that said I've nearly got everything to work in this project.
Yes I tried rewindDirectory(), that takes you back to the first file in the currently open folder, it doesnt take you to root.
I use File dir to keep track of open folders, and on startup to select root I go dir = SD.open ("/"), if I do that again in the code nowt happens, I tried dir = ("/") that doesnt do owt either.
On a plus side I found a way to play next file and play last file, play next uses opnNextFile(), and play last uses a counter that counts the No. of tracks played, pressing back one uses rewindDir() to go back to the start of the directory, and then I just call openNextFile() 1 less times than the number of files opened in the current directory to get to the last track.
Just thinking, dir.open ("/") will probably get me back to root after closing everything, if that woks I'm a twonk, but at least a happy one.
 
Last edited:

dr pepper

Well-Known Member
Most Helpful Member
I've sat and stared at the print directory function in that link, thats pretty much all I could find example iwse and I used that to build my player code on.
Documentation on SD or SDfat isnt great, I didnt know there was a file.chdir();, its not mentioned here:
https://www.arduino.cc/en/Reference/SD
But I'll try it and see if it works.

I've got some odd thing where the player corrupts make a racket for a split second then re-starts the track, I was thinking the uno didnt have enough datmem, thats why I went to the mega but it does it too, I think I've got some interrupt latency issue, maybe this codec isnt the best.
The vs1053 codec doesnt read the sd card, the 'duino does then transfers the data to the codec to play it, fresh data is demanded via an interrupt from the codec.
I went this way so's I could read and display album and tracks names, I dont just want cd1, track6 etc.
I might have a look at cheap players that dont read file names, if I can find a way to read the sd before the player plays anything I'll have a directory of dir and file names to display. only thing is I dont think the cheapo's allow access to the Sd card.
 
Last edited:

dr pepper

Well-Known Member
Most Helpful Member
I did a bit of researching with google.
Turns out a few folks have had the same trouble with the player freezing
There are obviously data registers & junk in the codec, one forum post mentioned using musicPlayer.softReset() after every musicPlayer.stop() command which I use every time I do anything other than pause, so i have added the soft reset command after every stop command and I'll see if it stops the freezing.
Sounds like a work around to me.
 

dr pepper

Well-Known Member
Most Helpful Member
Yes that sounds intuitive, but to me it int!
Root is a 'file' variable, and so is 'file', confusing innit.
I gorrit to work using direntry = SD.open("/")
So when I refer to the file direntry which is where the current open folder is kept I go to the root.
SD doesnt have or no longer supports chDir()
Makes as much sense as a 4 pound note, but I spose this is dos.
Sussed the odd thing with the player freezing, a sprinkling of musicPlayer.softReset() did the trick.
The code now plays, pauses, goes forward & back a track, and forward & back a folder, now to glue my fm tuner code to it and build a car radio type thing, in disguise so the bosses dont know we have the latest, or oldest tunes on board during the late shift.
Enjoy the cognac.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top