I2C Master requesting data from Slave

Hi,

I am hoping that someone can help me as I have been trying to work this out for day's and its driving me nuts!!

Ok, I have made a full scale R2D2 robot and now wanted to added voice recognition to him. I got an EasyVr Module and wrote some code to see if I could get some sound working via my MP3 player as I spoke and this worked out great.

I now want to set up an Arduino with the EasyVR module connected to it, place that into R2's head and send the voice data over I2C to my master controller that is in R2's body. The master is already sending data to other controllers via I2C so this has to stay as the master so that means the EasyVR controller connected to another Arduino needs to be a slave device that sends its data to the master.

I have some code and have some print in's so I can see what is happening and although I can see the correct data been sent its just that it is been sent over and over and I can't hear the sound I want the MP3 player to play.

What I need is when I say a word, the EasyVR gets it and send out that bit of data to the master which sends that to my MP3 player to play the sound.

Code for the slave:

#include "SoftwareSerial.h"
#include "EasyVR.h"
#include <Wire.h>
  
SoftwareSerial port(8, 9);

EasyVR easyvr(port);

#define I2C_SLAVE_ADDRESS 9  //define slave i2c address

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
  GROUP_2  = 2,
  GROUP_3  = 3,
  GROUP_4  = 4,
};

enum Group0 
{
  G0_HAY_R2 = 0,
};

enum Group1 
{
  G1_SAY_HELLO = 0,
  G1_SAY_GOODBYE = 1,
  G1_SHAKE_HANDS = 2,
  G1_THATS_FUNNY = 3,
  G1_BE_QUITE = 4,
  G1_SPEAK_UP = 5,
  G1_SLEEP_TIME = 6,
  G1_GUARD_ROOM = 7,
  G1_LETS_PLAY = 8,
};

enum Group2 
{
  G2_DO_YOU_REMEMBER = 0,
  G2_MUSIC = 1,
  G2_DANCE_FOR_ME = 2,
  G2_LIGHTS = 3,
  G2_SPIN_AROUND = 4,
  G2_END_PLAY = 5,
};

enum Group3 
{
  G3_LUKE_SKYWALKER = 0,
  G3_ANAKIN = 1,
  G3_C_3PO = 2,
  G3_DARTH_VADER = 3,
  G3_PRINCESS_LEIA = 4,
  G3_OBI_WAN = 5,
  G3_HAN_SOLO = 6,
  G3_CHEWBACCA = 7,
};

enum Group4 
{
  G4_HAPPY_BIRTHDAY = 0,
  G4_STARWARS = 1,
  G4_IMPERIAL_MARCH = 2,
  G4_MAHNA_MAHNA = 3,
  G4_CANTINA = 4,
};

byte voiceData;

int8_t group, idx;
//byte vol = 50; // 0 = full volume, 255 off

void setup()
{
  port.begin(9600);                     // setup PC serial port

  Wire.begin(I2C_SLAVE_ADDRESS);
  Wire.onRequest(requestVoiceData);
  
  
  while (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    delay(1000);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(0);
  
  group = EasyVR::TRIGGER; //<-- start group (customize)
}



void loop(){
  
  if (easyvr.getID() < EasyVR::EASYVR3)
    easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
    
    Serial.print("Say a command in Group ");
    Serial.println(group);
    easyvr.recognizeCommand(group);

  //do
  //{
    // can do some processing while waiting for a spoken command
  //}
  while (!easyvr.hasFinished());
  
  if (easyvr.getID() < EasyVR::EASYVR3)
  easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off

  idx = easyvr.getWord();
  if (idx >= 0)
  {
    // built-in trigger (ROBOT)
    //group = GROUP_X; <-- jump to another group X
    return;
  }
  
  idx = easyvr.getCommand();
  if (idx >= 0)
  {
    uint8_t train = 0;
    char name[32];
    
    Serial.print("Command: ");
    Serial.print(idx);
    
  
 if (easyvr.dumpCommand(group, idx, name, train))
   {
    Serial.print(" = ");
    Serial.println(name);
    
    
   }
 else
   Serial.println();
   //easyvr.playSound(0, EasyVR::VOL_FULL);
   action(); // perform some action
  }
  else // errors or timeout
  {
    if (easyvr.isTimeout())
      Serial.println("Timed out, try again...");
     int16_t err = easyvr.getError();
    if (err >= 0)
    {
        Serial.print("Error ");
        Serial.println(err, HEX);
      
      voiceData = 99; 
            
      }
    }
  }




void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_HAY_R2:
        voiceData = 0; 
        group = GROUP_1; 
        break;
      }
      break;
    case GROUP_1:  
      switch (idx)
      {
      case G1_SAY_HELLO:
         voiceData = 1;  
         group = GROUP_0; 
        break;
      case G1_SAY_GOODBYE:
         voiceData = 2;  
         group = GROUP_0; 
        break;
      case G1_SHAKE_HANDS:
        voiceData = 3; 
        group = GROUP_0; 
        break;
      case G1_THATS_FUNNY:
        voiceData = 4; 
        group = GROUP_0; 
      case G1_BE_QUITE:
        voiceData = 5; 
        group = GROUP_0; 
        break;
      case G1_SPEAK_UP:
        voiceData = 6; 
        group = GROUP_0; 
        break;
      case G1_SLEEP_TIME:
        voiceData = 7; 
        group = GROUP_0; 
        break;
      case G1_GUARD_ROOM:
        voiceData = 8; 
        group = GROUP_0; 
        break;
      case G1_LETS_PLAY:
        voiceData = 9; 
        group = GROUP_2; 
        break;
      }
      break;
    case GROUP_2:
      switch (idx)
      {
      case G2_DO_YOU_REMEMBER:
        voiceData = 10; 
        group = GROUP_3; 
        break;
      case G2_MUSIC:
        voiceData = 11; 
        group = GROUP_4; 
        break;
      case G2_DANCE_FOR_ME:
        voiceData = 12; 
        group = GROUP_2; 
        break;
      case G2_LIGHTS:
        voiceData = 13;
        group = GROUP_2; 
        break;
      case G2_SPIN_AROUND:
        voiceData = 14; 
        group = GROUP_2; 
        break;
      case G2_END_PLAY:
        voiceData = 15; 
        group = GROUP_0; 
        break;
      }
      break;
    case GROUP_3:
      switch (idx)
      {
      case G3_LUKE_SKYWALKER:
        voiceData = 16; 
        group = GROUP_2; 
        break;
      case G3_ANAKIN:
        voiceData = 17; 
        group = GROUP_2; 
        break;
      case G3_C_3PO:
        voiceData = 18; 
        group = GROUP_2; 
        break;
      case G3_DARTH_VADER:  
        voiceData = 19; 
        group = GROUP_2; 
        break;
      case G3_PRINCESS_LEIA:
        voiceData = 20; 
        group = GROUP_2; 
        break;
      case G3_OBI_WAN:
        voiceData = 21;  
        group = GROUP_2; 
        break;
      case G3_HAN_SOLO:
        voiceData = 22; 
        group = GROUP_2; 
        break;
      case G3_CHEWBACCA:
        voiceData = 23;  
        group = GROUP_2; 
        break;
      }
      break;
    case GROUP_4:  
      switch (idx)
      {
      case G4_HAPPY_BIRTHDAY:
        voiceData = 24; 
        group = GROUP_2; 
        break;
      case G4_STARWARS:
        voiceData = 25; 
        group = GROUP_2; 
        break;
      case G4_IMPERIAL_MARCH:
        voiceData = 26; 
        group = GROUP_2; 
        break;
      case G4_MAHNA_MAHNA:
        voiceData = 27; 
        group = GROUP_2; 
        break;
      case G4_CANTINA:
        voiceData = 28; 
        group = GROUP_2; 
        break;
      }
      
      break;
    }
}

void requestVoiceData() {
   
  Wire.write(voiceData);
  
}

Code for the master

#include <MP3Trigger.h>
 MP3Trigger trigger;

#include <Wire.h>

#define I2C_SLAVE_ADDRESS 9

byte vol = 20; // 0 = full volume, 255 off

int voiceData;
                                         
void setup()  
{
  
  Serial.begin(9600);
  
  trigger.setup(&Serial1);               //Serial 1 is used on Arduino Micro for MP3 Player
  trigger.setVolume(vol);                //Anything with trigger is used for the MP3 Player

  Wire.begin();
  
}   

void loop() {

    Wire.requestFrom(I2C_SLAVE_ADDRESS, 1); 
       
    if (Wire.available()){
    voiceData = Wire.read();
    }  
    voicecontrol();
  }


void voicecontrol()  
{
  if (voiceData == 0)
    {
      (trigger.play(70));   
      Serial.println("Understand That\r\n");   
    }
   else if (voiceData == 1)// 
    {                           
     (trigger.play(88));   
      //voicecmd = -1;
      Serial.println("Say Hello\r\n");                       
      }
      
   else if (voiceData == 2)
     {                      
      (trigger.play(89));
      
       Serial.println("Say Goodbye\r\n");
       }
      
   else if (voiceData == 3)
      {                      
        //utilityArm()             
        (trigger.play(5));
        
        Serial.println("Shake Hands\r\n");
        }
     
    else if (voiceData == 4)  
      {                      
        (trigger.play(5));
         
         Serial.println("That's Funny\r\n");
         }
     
     else if (voiceData == 5) 
       {                      
        if (vol>0)
          {
            vol--;
            trigger.setVolume(vol);
            }    
            Serial.println("Volume Down\r\n");
            }
      
      else if (voiceData == 6)  
        {                      
         if (vol<255)
           {
             vol++;
             trigger.setVolume(vol);
             }   
             Serial.println("Volume Up\r\n");
             }
      
      else if (voiceData == 7)  
        {                      
          //R2ShutDown();
          
          Serial.println("Sleep Time\r\n");
          }
      
      else if (voiceData == 8)  
        {                      
          //guardRoom();
         
          Serial.println("Guard Room\r\n");
          }
      
      else if (voiceData == 9) 
        {                      
          (trigger.play(70));
         
          Serial.println("Lets Play\r\n");
          }
//***************************************************************************************     

      else if (voiceData == 10)  
        {                      
          (trigger.play(70));
          
          Serial.println("Do you remember\r\n");
          }
      
      else if (voiceData == 11)  
        {                      
          (trigger.play(70));
          
          Serial.println("Music\r\n");
          }
      
      else if (voiceData == 12)  
        {                     
          //danceing();                       
          
          Serial.println("Dance for me\r\n");
          }
      
      else if (voiceData == 13)  
        {                      
         
          Serial.println("Lights\r\n");
          }
      
      else if (voiceData == 14) 
        {                     
          //SpinAround();
          
          Serial.println("Spin Around\r\n");
          }
      
      else if (voiceData == 15)  
        {                     
          (trigger.play(70));
          
          Serial.println("End Play\r\n");
          }
//********************************************************************************      
      else if (voiceData == 16) 
        {                     
          (trigger.play(3));
           
           Serial.println("Luke Skywalker\r\n");
           }
      
      else if (voiceData == 17)  
        {                                               
          (trigger.play(3));
           
           Serial.println("Anakin\r\n");
           }
       else if (voiceData == 18) 
         {                                               
           (trigger.play(3));
            
            Serial.println("C-3PO\r\n");
            }
       
       else if (voiceData == 19)  
         {                                               
           (trigger.play(3));
            
            Serial.println("Darth Vader\r\n");
            }
      
       else if (voiceData == 20)  
         {                                               
           (trigger.play(3));
            
            Serial.println("Princess Leia\r\n");
            }
      
       else if (voiceData == 21)  
         {                                               
           (trigger.play(3));
            
            Serial.println("Obi-Wan\r\n");
            }
       
       else if (voiceData == 22)   
         {                                              
           (trigger.play(3));
           
            Serial.println("Han Solo\r\n");
            }
       
       else if (voiceData == 23)   
         {                                              
            (trigger.play(3));
            
             Serial.println("Chewbacca\r\n");
            }
//************************************************************************************      
       else if (voiceData == 24)  
         {                                               
           (trigger.play(86));
            
            Serial.println("Happy Birthday\r\n");
            }
       
       else if (voiceData == 25)  
         {                                               
           (trigger.play(23));
            
            Serial.println("Starwars\r\n");
            }
       
       else if (voiceData == 26)  
         {                                               
           (trigger.play(25));
            
            Serial.println("Imperial March\r\n");
          }
       
       else if (voiceData == 27)  
          {                                               
            (trigger.play(87));
             
             Serial.println("Mahna Mahna\r\n");
            }
       
       else if (voiceData == 28)  
         {                                               
            (trigger.play(22));
             
             Serial.println("Cantina\r\n");
            }
//*************************************************************************************      
                
      else if (voiceData == 98)  
        {                                               
           (trigger.play(62));
            
            Serial.println("R2 Is no longer waiting for 2nd command\r\n");
            }
      
      //else if (voiceData == 99)  
        //{                                               
           //(trigger.play(90));
            //voicecmd = -1;
            //Serial.println("R2 didnt understand the command\r\n");
            //}
    
      return;
  }