Hello Community,
I am struggling with the following problem:
I'm using an Audio Shield http://www.elv.de/audio-shield-fuer-arduino-asa1-bausatz.html.
Basically the shield is reading mp3 Audio from an SD cart and sends it to an mp3 decoder (VS1011).
WHILE this is done I want to switch on/off LED's matching to the streamed Audio,
Here is the important part of the code:
loop()
{
//Create Buffer for the mp3 decoder
unsigned char buffer[32];
//open File
if( File SoundFile = SD.open( "001.mp3", FILE_READ ) )
{
//play file as long as it is available.
while( SoundFile.available())
{
//Read File chunk
SoundFile.read( buffer, sizeof(buffer));
//send File chunk to the mp3 decoder
VS1011.Send32( buffer );
}
}
}
When Audio is on everything has to be done inside the WHILE loop, right?
I'm switching the LEDs i'm using the millis() function (known from the blink without delay example http://arduino.cc/en/Tutorial/BlinkWithoutDelay):
loop()
{
//Create Buffer for the mp3 decoder
unsigned char buffer[32];
//open File
if( File SoundFile = SD.open( "001.mp3", FILE_READ ) )
{
//play file as long as it is available.
while( SoundFile.available())
{
//Read File chunk
SoundFile.read( buffer, sizeof(buffer));
//send File chunk to the mp3 decoder
VS1011.Send32( buffer );
current_1 = millis();
if( (current_1 - previous_1) >= on_1_1) digitalWrite(blink_1, HIGH);
if( (current_1 - previous_1) >= off_1_1)digitalWrite(blink_1, LOW);
if( (current_1 - previous_1) >= on_2_1) digitalWrite(blink_1, HIGH);
if( (current_1 - previous_1) >= off_2_1) digitalWrite(blink_1, LOW);
if( (current_1 - previous_1) >= on_3_1) digitalWrite(blink_1, HIGH);
if( (current_1 - previous_1) >= off_3_1) digitalWrite(blink_1, LOW);
}
}
}
the problem is that if too many if() and millis() functions are used the time consumption is too big, so the mp3 decoder buffer can't be filled fast enough and the sound starts to stutter. I already lowered the mp3 bit rate, but i want to do many more LED switchings.
Do you have any idea how to optimize this? I think using hardware timer will also not work, because using Interrupts would also "interrupt" the data streaming?
Thanks so far. I'm using an Arduino Mega, so program size doesn't matte. But maybe it's simply too slow for this task.