Need Help Combining EasyVR Sketch, MP3 Trigger and Arduino

I am attempting to use the EasyVR shield(Manual) to trigger the MP3 Trigger (Manual).

I have a working code for the EasyVR so that it recognizes the commands. I have implemented the MP3 Trigger code into the EasyVR sketch but it spits out garbled letters on the IDE monitor. I know that it has to the Serial communication that is screwing things up, but I do not know how to solve it.

This first sketch is for the MP3 Trigger, and it works.

#include <SoftwareSerial.h>
#include <MP3Trigger.h>

SoftwareSerial trigSerial = SoftwareSerial(2, 3);
MP3Trigger trigger;

void setup ()
{
  trigger.setup(&trigSerial);  //This also starts the serial for this device
  Serial.begin( MP3Trigger::serialRate() );
}

void loop()
{
  trigger.update();
  if (Serial.available())
  {
      char LETTER_FROM_COMPUTER = Serial.read();
      if (LETTER_FROM_COMPUTER == 1)  
      {
     trigger.play(2);
       }
  }
}

This sketch is for the EasyVR, which also works at recognizing my commands.

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif

#include "EasyVR.h"
EasyVR easyvr(port);

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0 
{
  G0_MICA = 0,
};

enum Group1 
{
  G1_HELLO = 0,
  G1_HOW_ARE_YOU = 1,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(0);

  group = EasyVR::TRIGGER; //<-- start group (customize)
}

void action();

void loop()
{
  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());
  
  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)
  {
    // print debug message
    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);
    // perform some action
    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);
    }
  }
}

void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_MICA:
        // write your action code here
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_HELLO:
        // write your action code here
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_HOW_ARE_YOU:
        // write your action code here
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    }
}

Now, as I have said earlier, I have tried to incorporate the MP3 Trigger sketch into the EasyVR sketch so that when I say a command, it triggers a track from the MP3 Trigger. This sketch compiles and uploads but it spits out garbled characters on the Monitor and, of course, doesn't work. (i.e. it doesn't trigger a sound). Below is the combined sketches, which, I am sure, is totally wrong. I believe it has to do with the Serial communication, but don't know enough to make this work.

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif

#include <MP3Trigger.h>
#include "EasyVR.h"
EasyVR easyvr(port);
SoftwareSerial trigSerial = SoftwareSerial(12, 13);
MP3Trigger trigger;

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0 
{
  G0_MICA = 0,
};

enum Group1 
{
  G1_HELLO = 0,
  G1_HOW_ARE_YOU = 1,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(0);

  group = EasyVR::TRIGGER; //<-- start group (customize)
  
  trigger.setup(&trigSerial);  //This also starts the serial for this device
  Serial.begin( MP3Trigger::serialRate() );
}

void action();

void loop()
{
  trigger.update();
  if (Serial.available())
  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());
  
  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)
  {
    // print debug message
    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);
    // perform some action
    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);
    }
  }
}

void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_MICA:
        // write your action code here
         group = GROUP_1;// <-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_HELLO:
        trigger.play(2);// write your action code here
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_HOW_ARE_YOU:
        // write your action code here
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    }
}

EasyVRBridge.cpp (3.26 KB)

EasyVRBridge.h (1.77 KB)

MP3Trigger.cpp (2.66 KB)

MP3Trigger.h (1.32 KB)

      char LETTER_FROM_COMPUTER = Serial.read();

By convention, all capital letter names are reserved for constants. You'd hardly assign a value to a constant in a function.

  SoftwareSerial port(12,13);
SoftwareSerial trigSerial = SoftwareSerial(12, 13);

Probably not a good idea have two software serial instances on the same pins.

lol, I really don't know where I got that from... But I seem to have lost the working code for the MP3 Trigger. But anyway, the trigger.play(2); is what actually triggers the sound file...

I have tried to use different pins.

Is this:

 SoftwareSerial port(12,13);
SoftwareSerial trigSerial = SoftwareSerial(2, 3);

Reassigning the SoftwareSerial if I changed the 2nd line to different pins? If it is, is there a hint to how I can make this work?

Anyway since I can't get any help with the Serial...

I attempting a different method for triggering sound from the MP3 Trigger. The MP3 trigger has pins where if you ground them, then that track is played. I am trying to get the Arduino to set a pin LOW to trigger the sound. But I cannot get it to work in the EasyVR sketch.

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif

#include "EasyVR.h"
EasyVR easyvr(port);

int pin = 6;
int pin2 = 8;
int pin3 = 9;

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0 
{
  G0_MICA = 0,
};

enum Group1 
{
  G1_HELLO = 0,
  G1_HOW_ARE_YOU = 1,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  pinMode(pin,OUTPUT);
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(0);

  group = EasyVR::TRIGGER; //<-- start group (customize)  
}

void action();

void loop()
{  
  digitalWrite(pin,HIGH);
  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());
  
  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)
  {
    // print debug message
    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);
    // perform some action
    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);
    }
  }
}

void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_MICA:
        //digitalWrite(pin,LOW);
        //digitalWrite(pin,HIGH);
        group = GROUP_1; //<-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_HELLO:
        digitalWrite(pin,LOW);
        //digitalWrite(pin,HIGH);
        //group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_HOW_ARE_YOU:
     
        // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    }
}

When I say "Hello," the pin is supposed to ground and trigger the sound. But it doesn't.

      case G1_HELLO:
        digitalWrite(pin,LOW);
        //digitalWrite(pin,HIGH);
        //group = GROUP_X; <-- or jump to another group X for composite commands
        break;

It only triggers a sound at power on or reset because of this piece:

void setup()
{
  pinMode(pin,OUTPUT);
  // bridge mode?
  if (bridge.check())
....
....

There is a digitalWrite(pin,HIGH) at the start of the loop since I do not want the pin to stay at 0v/ground. Anyone have a hint as to what I am doing wrong?

Is there a reason no one is helping me with this?

Possibly because very few of us have an EasyVR and an MP3 Trigger.

Is there a reason no one is helping me with this?

Aside from the fact that you have very unusual hardware, that not many other people are trying to use together?

You have serial print statements in your code, but you haven't shown any serial output. That makes it hard for us to see what is/is not happening in your code.

All that you have said is "it doesn't work". That's not a lot to go on.

You set pin HIGH at the start of loop. There is nothing in that name that implies what that pin is for. Is it supposed to do something with the MP3 shield?

Some of the code to alter that pin is commented out. We have no idea which group the command is in to play a track, so we can't see if appropriate code is still in place, or not. Even if the appropriate code is in place, does setting the pin LOW cause the whole track to be played? I don't think so. I think that all it does is start something happening.

Then, loop() ends, and is called again. At that time, you turn off the playing of the track, so it is possible that only a few nanoseconds of the track is played, which is not enough of it for you to hear.

The print statements is not related to my problem. My problem is that I need a digital pin to ground to trigger a sound on the MP3 Trigger. Then obviously go high to not trigger it more than once. As long as the pin is at ground, the sound loops.

So basically:

void setup(){
 int pin = 7 //the pin to trigger
 pinMode(pin,OUTPUT);
digitalWrite(pin,HIGH);  //so it doesn't trigger until a voice command is given
}
void loop(){
 if (command == true)  //if I say this command, set this pin to ground
 digitalWrite(pin,LOW); //set pin to ground so it will trigger a sound on the MP3 Trigger
digitalWrite(pin,HIGH); //set pin high to make the sound only once instead of it looping.
}

The problem is that setting the pin low like this doesn't cause the MP3 trigger to do anything. I cannot remember how I got it to work yesterday, but I did get it to trigger properly, but the sound kept looping so I added digitalWrite(pin,HIGH) to stop it. When I do that it doesn't work at all. I even added a delay() of up to several seconds between digiatlWrite(pin,LOW) and digitalWrite(pin,HIGH) but it doesn't trigger.

I don't know, I guess I will have to keep tinkering.

Do you need a brief delay after you bring the pin low?

I have tried a delay(5) up to delay(3000) and still nothing. I will have to keep experimenting.

I will post if I find anything. Maybe it will help others.

I found out what was causing issues. When I wrote a pin HIGH, none of the other triggers would work on the MP3 Trigger. In the manual, it specifically states that you can set them HIGH at 3.3v or 5v and trigger others. I have posted on the MakerJam forums but haven't got a reply yet.

So now I am just using pinMode(pin,INPUT) to stop triggering a pin then setting it to OUTPUT to trigger it with a delay(20) in between those. Any less then 20 it doesn't trigger.