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.
#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;
}
}