Serial bus replacement by RN41 -I/RM and audio streaming

Hi all,

I am trying to stream some mp3 files over bluetooth between two Arduinos Uno.

The first Arduino (Transmitter) is “wearing” the Arduino wireless SD shield on which I connected a RN41 module in master mode. This module runs this code :

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
boolean receiverReady = false;
File dataFile;
int bytesSent = 0;
const uint8_t pinPREV =5;
const uint8_t pinNEXT =6;
const uint8_t pinPLAY =9;
char trackName[] = "track001.mp3";
uint8_t trackNo = 1;
boolean play=true;

void setup()
{

 // Open serial communications and wait for port to open:
  Serial.begin(230400);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  

  //Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(4, OUTPUT);
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  //Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  sprintf(trackName, "track%03d.mp3", trackNo);
  dataFile = SD.open(trackName);
  
     //on met les bouton en entrée
   pinMode(pinNEXT, INPUT);
   pinMode(pinPREV, INPUT);
   pinMode(pinPLAY, INPUT);
   //on active les résistance de pull-up en mettant les broche à l'état haut (mais cela reste toujours une entrée)
   digitalWrite(pinNEXT, HIGH);
   digitalWrite(pinPREV, HIGH);
   digitalWrite(pinPLAY, HIGH);
 
   /*Serial.write("$$");
   delay(50);
   Serial.write("CFR\n");*/
}

int etat_pinNEXT = 0;
int etat_pinPREV = 0;
int etat_pinPLAY = 0;
int dernierEtat_pinNEXT = 1;
int dernierEtat_pinPREV = 1;
int dernierEtat_pinPLAY = 1;

void loop()
{
   etat_pinNEXT = digitalRead(pinNEXT); //
   etat_pinPREV = digitalRead(pinPREV); //
   etat_pinPLAY = digitalRead(pinPLAY);
   
   if (etat_pinPLAY != dernierEtat_pinPLAY) 
      {
        if (etat_pinPLAY == LOW)
        {
          play = !play;
        } 
      }
      
      //Si on appuye sur le bouton volume DOWN, le volume diminue  
   if (etat_pinNEXT != dernierEtat_pinNEXT) 
      {
        if (etat_pinNEXT == LOW)
        {
          nextSong();
        } 
      }
      
 //Si on appuye sur le bouton volume DOWN, le volume diminue  
   if (etat_pinPREV != dernierEtat_pinPREV) 
      {
        if (etat_pinPREV == LOW)
        {
          prevSong();
        } 
      }
      
      
   
   if(play==true)
   {
     fonction(); 
   }
   

   
  // save the current state as the last state, 
  //for next time through the loop
  dernierEtat_pinNEXT = etat_pinNEXT;
  dernierEtat_pinPREV = etat_pinPREV;
  dernierEtat_pinPLAY = etat_pinPLAY;
}

void serialEvent()
{
    
    Serial.read();
    receiverReady=true;
    
}

int fonction(){
  
  if(bytesSent>=32){
      if(receiverReady){
        bytesSent=0;
        receiverReady=false;
        return 2;
      }//if
      else{
        return 1;
      }//else
   }//if
   else{
     
        // if the file is available, read from it:
        if(dataFile.available()){
          Serial.write(dataFile.read());
          bytesSent++;
          return 0;
        }//if
    }//else
}//fonction()



void nextSong()
{
   dataFile.close();
   if (trackNo<9)
   { 
     trackNo = trackNo+1;
   }
  else
   { 
     trackNo=1;
   }
   //tack the number onto the rest of the filename
     sprintf(trackName, "track%03d.mp3", trackNo);
     dataFile = SD.open(trackName);      
}


void prevSong()
{
   dataFile.close();
   if (trackNo>1)
   { 
     trackNo = trackNo-1;
   }
  else
   { 
     trackNo=9;
   }
   //tack the number onto the rest of the filename
     sprintf(trackName, "track%03d.mp3", trackNo);
     dataFile = SD.open(trackName);
       
}

It sends packets of 32bytes to the Hardware Uart when it receives a character from the receiver.
The second Arduino is wearing two shields :

The wireless SD shield and the Sparkfun MP3 player shield. I edited the refill() function from Bill Porter’s Library to read directly 32 bytes from the Serial UART and then send a character to the transmitter to continue the mp3 file transfer.

void SFEMP3Shield::refill() {

  //Serial.println(F("filling"));
#if PERF_MON_PIN != -1
  digitalWrite(PERF_MON_PIN,LOW);
#endif

  // no need to keep interrupts blocked, allow other ISR such as timer0 to continue
#if !defined(USE_MP3_REFILL_MEANS) || USE_MP3_REFILL_MEANS == USE_MP3_INTx
  sei();
#endif
	
  while(digitalRead(MP3_DREQ)) {
	Serial.write('s');
    if(!(Serial.readBytes(((char *)(mp3DataBuffer)), sizeof(mp3DataBuffer)))) { //Go out to SD card and try reading 32 new bytes of the song
      //track.close(); //Close out this track
      playing_state = ready;

      //cancel external interrupt
      disableRefill();

      flush_cancel(post); //possible mode of "none" for faster response.

      //Oh no! There is no data left to read!
      //Time to exit
      break;
    }


    //Once DREQ is released (high) we now feed 32 bytes of data to the VS1053 from our SD read buffer
#if !defined(USE_MP3_REFILL_MEANS) || USE_MP3_REFILL_MEANS == USE_MP3_INTx
    cli(); // allow transfer to occur with out interruption.
#endif
    dcs_low(); //Select Data
    for(uint8_t y = 0 ; y < sizeof(mp3DataBuffer) ; y++) {
      //while(!digitalRead(MP3_DREQ)); // wait until DREQ is or goes high // turns out it is not needed.
      SPI.transfer(mp3DataBuffer[y]); // Send SPI byte
    }

    dcs_high(); //Deselect Data
    //We've just dumped 32 bytes into VS1053 so our SD read buffer is empty. go get more data
#if !defined(USE_MP3_REFILL_MEANS) || USE_MP3_REFILL_MEANS == USE_MP3_INTx
    sei();
#endif
  }

#if PERF_MON_PIN != -1
  digitalWrite(PERF_MON_PIN,HIGH);
#endif
}

My problem occurs when I use the bluetooth modules. I have a custom BaudRate set at 256000 Bauds, with a wired connection everything works fine. But when I use the RN41 modules in SPP - even in Fast data mode, I can’t manage to transfer the file quickly enough and the music is not played correctly.

Do you have any Idea of why the bluetooth trnasfer is that slow ?

Thanf you for your help,
Brian

PS : Sorry about my english, I’m French XD