How to read file continuously?

Hello everyone.

I am reading wav file from SD card and I would like to read it continuously (to play via speaker all the time). How to do it better way?

Here's what I already tried, but after about 25 times it stops

void open_me(){
    dataFile = SD.open("test2.wav");
    if (dataFile){
        while (dataFile.available()) {
            Serial.write(dataFile.read());
        }
        dataFile.close();
        open_me();
    }
}

Thanks for your help.

Here's what I already tried,

That code doesn't compile. Sorry.

Strange.

Here’s full code, just tried to compile it. It works, but as I mentioned, it stops after some time

#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
File dataFile;

void setup(){
  Serial.begin(115200);
  Serial.print("Initializing SD card...");
  opensd();
}
void opensd(){
  if (!SD.begin(chipSelect)) {
    return;
  }else{
    Serial.println("Card loaded");
    openme();
  }
}
void openme(){
   dataFile = SD.open("test2.wav");
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    delay(1000);
    dataFile.close();
    openme();
  }else {
    Serial.println("error opening file");
  }
}
void loop(){

}

Move the openme() function in the main loop. No need to re invoke the openme(), it might cause stack overflow.

On top of not calling openMe from inside the openMe function, If you keep opening and closing the file, you will never progress much in the bytes you read...

Your code should do

Open the file Repeat until end of file Read next byte from source (and move to next byte) Write that byte out End Close the file

J-M-L, my code shouldn't just open the file, but it need to start reading file over, once it reaches the end of it.

well, then just start again from the beginning when you are at the end of the file.

there is a seek() function that lets you set where you read in the file. so once you arrive at the end (read will return -1), just dataFile.seek(0); and you will start again at the beginning

It's working! Huge Thanks!

void loop(){ if (dataFile.available()){ Serial.write(dataFile.read()); }else{ dataFile.seek(0); } }