Write and play audio from SD card

Hello,

I am trying to write sensor data to sd card and while the data is being written, if the sensor data is above a particular value, I want to play an audio file which is on the sd card.
Is it possible to play the audio file while simultaneously writing data to the sd card?

Thank you!

What hardware are you using to play the audio file? The Arduino doesn't know squat about audio files.

Proper hardware would read enough data from a file, so that you could also write to a different file, with only minimal chances of both the Arduino and the other hardware needing to read from and write to the SD card at the same time. Mostly the Arduino would write to, and the other hardware read from, buffers. Writing to the card, or reading from the card, happens only when the buffer becomes full (a write happens) or empty (a read happens).

What hardware are you using to play the audio file? The Arduino doesn't know squat about audio files.

I have the audio file in the sd card and using the library GitHub - TMRh20/TMRpcm: Arduino library for asynchronous playback of PCM/WAV files direct from SD card. Arduino Uno,Nano,Mega etc supported , I am able to play the audio file on a speaker which is connected to the pin 9 of arduino promini.

I was wondering if its even possible to play the audio file from the sd card ( whenever the data point exceeds a certain value) while continuously writing the sensor data to the card.

I was wondering if its even possible to play the audio file from the sd card ( whenever the data point exceeds a certain value) while continuously writing the sensor data to the card.

So, that library has a file open on the SD card, using 512+ bytes of SRAM. Do you have enough SRAM to open a second file, using another 512+ bytes of SRAM?

How quickly does that library use up the 512 bytes that it reads each time? How often will you fill up the 512 byte buffer that you will be using to write to the file?

How important is it that the audio play glitch-free (that is, with no delays caused by you writing to the card when it needs to read from the card?

The only way to really know is to try it.

I tried it :

I have an audio file (31 KB) (around 2 sec) saved on the sd card. When I collect sensor data, the microcontroller creates a file "DATA00" and writes the sensor data to this file.
The audio plays just once (when the datapoint is above a value) and after playing once, the audio file gets corrupted and the data is not readable anymore.
The microcontroller writes data to the file "DATA00" every 30 ms.

I had an other question too. Can the sd card keep writing data to the DATA file while the audio is being played?
From what I noticed, its not able to do that.

Any suggestions to fix the issue?

Any suggestions to fix the issue?

Yes, but I'll refrain unless you continue to fail to POST YOUR CODE!

THAT is why I asked you to post your code. It seems that you forgot to mention that you have other devices on the I2C bus.

When I finally found the TMRpcm library, it has a readme file that points to a Wiki that says:

May interfere with other libraries that rely on interrupts

, like the Wire class.

This library can be very processor intensive

The misspelled is.

Oh! Looks like I have to figure out another way to do this!
Thank you for pointing it out.