//final code for ATmega328
//function: voice recognition and wireless transmitter 
#include "Arduino.h"
#include "SoftwareSerial.h"

SoftwareSerial port(12,13);

#include "EasyVR.h"

EasyVR easyvr(port);
 
//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};
 
enum Group0 
{
  GO_SMART = 0,
};
 
enum Group1 
{
  G1_BEDROOM_ON = 0,
  G1_BEDROOM_OFF = 1,
  G1_BEDROOM_DIMMER= 2,
  G2_FAN_ON = 5,
  G2_FAN_OFF = 6,
  G2_FAN_LOWER=9,
  G3_LIVINGROOM_ON= 3,
  G3_LIVINGROOM_OFF=4,
  G4_MUSIC=7,
  G5_30SEC=8
};
 
 
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(3);
 
  group = EasyVR::TRIGGER; //&amp;lt;-- start group (customize)
   
   
  pinMode(11, OUTPUT); 
  digitalWrite(11, LOW);    // set the LED off
   
}
 
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; &amp;lt;-- 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);
    }
     */
     
    group = GROUP_0;
  }
}
 
void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case GO_SMART:
        // write your action code here
          group = GROUP_1; //&amp;lt;-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_BEDROOM_ON:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('a'); 
        digitalWrite(11, HIGH);     
        break;
        
      case G1_BEDROOM_OFF:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('b'); 
        digitalWrite(11, LOW);    
        break;
        
      case G1_BEDROOM_DIMMER:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('c'); 
        digitalWrite(11, HIGH);    
        break; 
        
       case G3_LIVINGROOM_ON:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('d'); 
        digitalWrite(11, HIGH);   
        break;  
        
        case G3_LIVINGROOM_OFF:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('e'); 
        digitalWrite(11, LOW);   
        break;  
       
        case G2_FAN_ON:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('f'); 
        digitalWrite(11, HIGH);    
        break;   
        
        case G2_FAN_OFF:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('g'); 
        digitalWrite(11, LOW);    
        break;   
        
        case G4_MUSIC:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('h'); 
        digitalWrite(11, HIGH);   
        break;   
        
        case G5_30SEC:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('i'); 
        digitalWrite(11, HIGH);    
        break;   
        
        case G2_FAN_LOWER:
        // write your action code here
        group = GROUP_0; //&amp;lt;-- or jump to another group X for composite commands
        Serial.print('j'); 
        digitalWrite(11, HIGH);    
        break;   
      }
      break;
    }
}