Hello all,
I am doing room automation project with ELECHOUSE Voice Recognition Module V3.
but don’t afraid yet my question is quite simple.
Besides I have very basic knowledge of programing, I got this system to work, but there is 1 problem. First about the system: I am using V3 module to switch on and off 3 things:
- Room main light (with servo)
- Room night light (with reley)
- Sound amplifier for the music (with servo)
You could wonder why I am using servos and not relays for all that stuff, its because its not my house and i do not want to disassemble main room switches etc… But that doesn’t matter. Servos working well in my case, they physically switching on/off other switches.
The problem with V3 module is that if you record sound commands in quiet conditions and near the microphone. Later you have to shout in the room and recognition is quite poor. If you recording commands from about 2m. distance, later recognition is much better, but if you are watching movies o listening music (especially louder) the system goes nuts and recognizes commands then it shouldnt that at least few times in half a hour. What I want to do is simply take 1 voice command for system to listen for lets say 10 sec for other 6 commands (on/off switching commands), and after that time system will listen again for that specific 1 voice command. As I said I have very basics of programming, so need your help guys. This is my working code so far and it needs to be a little adjusted, but I dont know how to add this functionality.
#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
#include <Servo.h>
Servo servo1;
Servo servo2;
VR myVR(12, 13); // 2:RX 3:TX, you can choose your favourite pins.
uint8_t records[7];
uint8_t buf[64];
int led = 13;
int in1 = 5; //rele1
int in2 = 6; //rele2
#define lightON (0)
#define lightOFF (1)
#define switchON (2)
#define switchOFF (3)
#define soundON (4)
#define soundOFF (5)
#define listeningON (6)
/**
@brief Print signature, if the character is invisible,
print hexible value instead.
@param buf --> command length
len --> number of parameters
*/
void printSignature(uint8_t *buf, int len)
{
int i;
for(i = 0; i < len; i++) {
if (buf[i]>0x19 && buf[i]<0x7F) {
Serial.write(buf[i]);
} else {
Serial.print("[");
Serial.print(buf[i], HEX);
Serial.print("]");
}
}
}
/**
@brief Print signature, if the character is invisible,
print hexible value instead.
@param buf --> VR module return value when voice is recognized.
buf[0] --> Group mode(FF: None Group, 0x8n: User, 0x0n:System
buf[1] --> number of record which is recognized.
buf[2] --> Recognizer index(position) value of the recognized record.
buf[3] --> Signature length
buf[4]~buf[n] --> Signature
*/
void printVR(uint8_t *buf)
{
Serial.println("VR Index\tGroup\tRecordNum\tSignature");
Serial.print(buf[2], DEC);
Serial.print("\t\t");
if (buf[0] == 0xFF) {
Serial.print("NONE");
} else if (buf[0]&0x80) {
Serial.print("UG ");
Serial.print(buf[0]&(~0x80), DEC);
} else {
Serial.print("SG ");
Serial.print(buf[0], DEC);
}
Serial.print("\t");
Serial.print(buf[1], DEC);
Serial.print("\t\t");
if (buf[3] > 0) {
printSignature(buf+4, buf[3]);
} else {
Serial.print("NONE");
}
Serial.println("\r\n");
}
void setup()
{
myVR.begin(9600);
Serial.begin(115200);
Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
pinMode(led, OUTPUT);
pinMode(in1, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo1.write(90);
servo2.write(90);
digitalWrite(in1, HIGH);
pinMode(in2, OUTPUT);
digitalWrite(in2, HIGH);
if (myVR.clear() == 0) {
Serial.println("Recognizer cleared.");
} else {
Serial.println("Not find VoiceRecognitionModule.");
Serial.println("Please check connection and restart Arduino.");
while(1);
}
if (myVR.load((uint8_t)lightON) >= 0) {
Serial.println("rele1on loaded");
}
if (myVR.load((uint8_t)lightOFF) >= 0) {
Serial.println("rele1off loaded");
}
if (myVR.load((uint8_t)switchON) >= 0) {
Serial.println("sviesaON loaded");
}
if (myVR.load((uint8_t)switchOFF) >= 0) {
Serial.println("sviesaOFF loaded");
}
if (myVR.load((uint8_t)soundON) >= 0) {
Serial.println("stipON loaded");
}
if (myVR.load((uint8_t)soundOFF) >= 0) {
Serial.println("stipOFF loaded");
}
if (myVR.load((uint8_t)LinsteningON) >= 0) {
Serial.println("viskasOFF loaded");
}
}
void loop()
{
int ret;
ret = myVR.recognize(buf, 50);
if (ret > 0) {
switch(buf[1]){
case lightON:
digitalWrite(in1, HIGH);
break;
case lightOFF:
digitalWrite(in1, LOW);
break;
case switchON:
digitalWrite(in2, LOW);
delay (300);
servo1.write(140);
delay (500);
servo1.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case switchOFF:
digitalWrite(in2, LOW);
delay (300);
servo1.write(40);
delay (500);
servo1.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case soundON:
digitalWrite(in2, LOW);
delay (300);
servo2.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case soundOFF:
digitalWrite(in2, LOW);
delay (300);
servo2.write(30);
delay (500);
digitalWrite(in2, HIGH);
break;
default:
Serial.println("Record function undefined");
break;
}
printVR(buf);
}
}
This code looks complicaded, but infact it isnt. What is most important here is this last part:
case lightON:
digitalWrite(in1, HIGH);
break;
case lightOFF:
digitalWrite(in1, LOW);
break;
case switchON:
digitalWrite(in2, LOW);
delay (300);
servo1.write(140);
delay (500);
servo1.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case switchOFF:
digitalWrite(in2, LOW);
delay (300);
servo1.write(40);
delay (500);
servo1.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case soundON:
digitalWrite(in2, LOW);
delay (300);
servo2.write(90);
delay (500);
digitalWrite(in2, HIGH);
break;
case soundOFF:
digitalWrite(in2, LOW);
delay (300);
servo2.write(30);
delay (500);
digitalWrite(in2, HIGH);
break;
default:
Serial.println("Record function undefined");
break;
This is what my system does in these “cases” It listens to “cases” and act accordingly. I need to add another “listening” case (it even defined in the code) i guess with “IF” statement and some mills() functionality, but because i am shitty programmer, It will take few days at least to figure out how to do it I rather give someone few euros (paypal or revolut) for this few code line if someone will help me. Even for lvl 3 beginners
it should not be so difficult to write such code.
P.S. If you are looking into this “case” part of the code and you dont understand why there are 2 relays. One relay is turning on power supply for my servos, otherwise they always on power mode and make noise, so I am switching them on, then necessary. Other relay is controlling lamp. Servos making turns in one direction and after time going back to previous state. So that’s all that is going in “cases”