Pages: [1]   Go Down
Author Topic: My EasyVR project  (Read 992 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my first Arduino project. I have to say the experience has been a blast.

This program will turn lights on or off depending on the command (kitchen or livingroom). I want to set a limit for the amount of errors accumulated. As of right now when the limit is reached (3) it sets pin7 HIGH (I did this just to see if my code would work). What I want it to do when the limit is reached is to go to case GROUP_0:.

If anyone could help me out I would appreciate it.


Code:
#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);

#define SND_COMMANDERROR    1

//Groups and Commands
int count=0;
int pin7 = 7;
int pin8 = 8;
int pin4 = 4;
int val;
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0
{
  G0_COMPUTER = 0,
};

enum Group1
{
  G1_KITCHEN = 0,
  G1_LIVINGROOM = 1,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  pinMode(pin8, OUTPUT);
  pinMode(pin4, 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()
{
  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)
    {
      count++;
      easyvr.playSound(1, EasyVR::VOL_FULL);
      Serial.print("Error ");
      Serial.println(err, HEX);
    }
    if (count == 3)     //if 3 errors are sent do something
    {
      digitalWrite(7, HIGH); 
    }
  }
}
 
void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_COMPUTER:
        group = GROUP_1;
       
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_KITCHEN:
        group = GROUP_0;
        val = digitalRead(pin8);
        if (val == LOW)
        {
          digitalWrite(pin8, HIGH);
        }
         else
         {
           digitalWrite(pin8, LOW);
         }           
        break;
      case G1_LIVINGROOM:
        group = GROUP_0;
        val = digitalRead(pin4);
        if (val == LOW)
        {
          digitalWrite(pin4, HIGH);
        }
        else
        {
          digitalWrite(pin4, LOW);
        }
        break;
      }
      break;
    }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What I want it to do when the limit is reached is to go to case GROUP_0
You can't go to a case.

What you need to do is look at what causes that case to be executed. In your code, GROUP_0 is a value that group can have. When you want that case to be executed again, you need to set group to GROUP_0 and call the function containing the switch statement.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (count == 3)     //if 3 errors are sent do something
    {
      group = GROUP_0;
      switch (idx)
      {
        case G0_COMPUTER:
        group = GROUP_1;
      }
    }

Thanks alot.
This brings me to a new question though lol. If I want to "reset" the count, do i need to use an array?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I want to "reset" the count, do i need to use an array?
For what? Resetting count is easy:
Code:
count = 0;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried that, I must have put it in the wrong place?

Code:
if (count == 3)     //if 3 errors are sent do something
    {
      group = GROUP_0;
      switch (idx)
      {
        case G0_COMPUTER:
        group = GROUP_1;
        count = 0;
      }

After 3 errors it will go back to GROUP_0. But the count is still increasing (I presume).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AH ok nm, I did put it in the wrong place. Thanks for the help I appreciate it.
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 215
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!!! I'm trying to test an easyvr shield, but when i open the serial monitor i have this error... EasyVR not detected! some ideas?!!? Thanks!!
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!!! I'm trying to test an easyvr shield, but when i open the serial monitor i have this error... EasyVR not detected! some ideas?!!? Thanks!!

You need to give alittle more information..  Possibly start a new thread since this thread isn't related to your problem.
Logged

//LiNK

Pages: [1]   Go Up
Jump to: