Hi everyone. This is my first post here, most of all because i always wants to find out the answer myself trough google or this forum, but i cant seem to find what i am looking for here.
I am working on a home automation project, like many others. I use the Easy VR shield for voice recoqnition and a simple 433 Mhz radio transmitter. Everything works great so far. I can easely turn on and off my power sockets in my home.
I want to take it a step up. Right now i am using a speek command like this:
Toggleword -> turnonlights // to turn on a plug
Toggleword -> turnofflights // to turn off a plug
All this is working with my switch case.
I would like to only say:
Toggleword -> Light // to turn on and when it is said the second time turn it off again.
Is there a way where i can get the arduino to remember if it is turned on and off, and then do the opposit when i say my voice command, maybe even in the same switch cases that i already have. Hope this makes sense to you guys.
Here is my 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"
#include "RCSwitch.h"
EasyVR easyvr(port);
//Groups and Commands
enum Groups
{
GROUP_0 = 0,
GROUP_1 = 1,
};
enum Group0
{
G0_ARDUINO = 0,
};
enum Group1
{
G1_TOEJTEAND = 0,
G1_TOEJSLUK = 1,
G1_LEDON = 2,
G1_LEDOFF = 3,
G1_LUFTUD = 4,
G1_STOPLUFT = 5,
};
EasyVRBridge bridge;
int8_t group, idx;
RCSwitch mySwitch = RCSwitch();
void setup() {
mySwitch.enableTransmit(10);
// 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; //<-- 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; <-- 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 G0_ARDUINO:
// 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_LEDON:
group = GROUP_0;
mySwitch.switchOn("11001", "01000"); // Turns on light 1
break;
case G1_LEDOFF:
group = GROUP_0;
mySwitch.switchOff("11001", "01000"); // Turns off light 1
break;
case G1_TOEJTEAND:
group = GROUP_0;
mySwitch.switchOn("11101", "10000"); // Turns on light 2
break;
case G1_TOEJSLUK:
group = GROUP_0;
mySwitch.switchOff("11101", "10000"); // Turns off light 2
break;
case G1_LUFTUD:
group = GROUP_0;
mySwitch.switchOn("11101", "01000"); // Turns on light 3
break;
case G1_STOPLUFT:
group = GROUP_0;
mySwitch.switchOff("11101", "01000"); // Turns off light 3
break;
}
break;
}
}
Thanks in advance!