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);
}