Voice Recognition Module programming issue

Hi Guys
I bought a voice recognition from ebay for about $25 so far im very happy with the performance with regards to recognizing words recorded , the only problem I find is that i can not seem to get a action to be processed from a word being said, and i have track the problem down to my comparison coding.

below is the code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int ledPin = 0;
int redPin = 11; // R petal on RGB LED module connected to digital pin 11
int greenPin = 9; // G petal on RGB LED module connected to digital pin 9
int bluePin = 10; // B petal on RGB LED module connected to digital pin 10
byte com = 0; //reply from voice recognition

void setup() 
{ 
Serial.begin(9600);
mySerial.begin(9600);

pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(redPin, OUTPUT); // sets the redPin to be an output
pinMode(greenPin, OUTPUT); // sets the greenPin to be an output
pinMode(bluePin, OUTPUT); // sets the bluePin to be an output
delay(1000);
mySerial.write(0xAA);
mySerial.write(0x21);
delay(1000);
} 

void loop() // run over and over again
{ 

while(mySerial.available())
{
com = Serial.write(mySerial.read());
switch(com)
{
case 0x11:
color(255,255,255); // turn RGB LED on -- white
break;

case 0x12:
color(255, 0, 0); // turn the RGB LED red
break;

case 0x13:
color(0,255, 0); // turn the RGB LED green
break;

case 0x14:
color(0, 0, 255); // turn the RGB LED blue
break;

case 0x15:
color(0,0,0); // turn the RGB LED off
break;

}
}

} 

void color (unsigned char red, unsigned char green, unsigned char blue) // the color generating function
{ 
analogWrite(redPin, red*102/255); 
analogWrite(bluePin, blue*173/255);
analogWrite(greenPin, green*173/255);
}

I have added software serial library to the code because i wanted to see if i get my commands into the arduino from the voice module and it works.
the problem area i think is the next few lines.

while(mySerial.available())
{
com = Serial.write(mySerial.read());
switch(com)
{
case 0x11:
color(255,255,255); // turn RGB LED on -- white
break;

case 0x12:
color(255, 0, 0); // turn the RGB LED red
break;

case 0x13:
color(0,255, 0); // turn the RGB LED green
break;

case 0x14:
color(0, 0, 255); // turn the RGB LED blue
break;

case 0x15:
color(0,0,0); // turn the RGB LED off
break;

}

See when you speak into the module it will return the word spoken in a form of "Result:12" or "Result:13" and so on so I try and compare an do a action with a switch/case so when the module reports "Result:12" the comparison on 0x12 should be made and the red light should switch on.... it doesn't.

any help ?... please

com = Serial.write(mySerial.read());

The Serial.write() method returns the number of characters it has written. How is that useful information for switching on?

Hi Paul

would Serial.println be the correct way then and not Serial.write?

would Serial.println be the correct way then

No. The println() method also returns the number of characters it wrote.

It seems to me that you want to do more with the value read from the serial port than just print it. To do that, you need to save the value read in a variable of the correct type, then print that value and switch based on that value.

Thanx Paul

I see what you are saying but what doesnt makes sence to me then is that the example code that i started with will also not work
original

void loop() // run over and over again
{ 

while(Serial.available())
{
com = Serial.read();
switch(com)

and he has videos up on youtube of it working

Your first code was reading from a software serial port and writing to a hardware serial port. This last code has you reading from the hardware serial port. Did you move the VR module? Why?

No The code I posted last is the original code as from the module supplier, the code i posted first is my code modified to work on software serial so i can view serial monitor . but when i first tried with the original code it also didn't work

I don't understand what you are doing. If you didn't move the VR module to the hardware serial pins, how can you expect to read from it?

With the hardware connected to the software serial pins, what are you seeing in the Serial Monitor application?

I think we miss understood each other, at first i tried the original code form the supplier which forced me to disconnect the VR module each time i uploaded the code so i added the software serial so i didnt have to anymore , so i moved the pins to the 2,and 3 for software serial. now when i speak into the VR module i get what i expect in the serial monitor . when i say any of the given 5 words i get "Result: 11" "Result :12" exc. so the software serial read works fine . its just the comparison that doesn't seem to work.

here is the link to the manual and product page (scross down)
http://www.ebay.com/itm/Voice-Recognition-Module-Arduino-Compatible-Control-your-devices-by-voice-/140613022031?pt=LH_DefaultDomain_0&hash=item20bd30714f

so the software serial read works fine . its just the comparison that doesn't seem to work.

Show the Serial Monitor output, please.

The reason that the comparison doesn't work is because you are comparing the wrong things. What the code you first posted compares is the number of characters written to the serial monitor to some values. None of them match, of course. because each call to Serial.write(), Serial.println(), or Serial.print() with an int writes or prints different numbers of characters, depending on the value being printed. "1" has one character. "102" is 5 characters. None of the values returned by the serial output methods has anything to do with the value that was put on the port - only the number of characters involved.

What it appears that you need to do is to read AND STORE data from the software serial port, until an end of packet marker (what that would be requires some discovery on your part - possibly a or and ) and then compare that stored value to some known strings, using strcmp().

In the middle of the 3rd page, there is mention of compact mode. In this mode, the device will return 0x11 or 0x12 corresponding to "Result: 11" or "Result: 12". Dealing with one byte will be far easier than dealing with a string.

I'd look into what it takes to get the module into compact mode.

Okay this toke me while now.

when the VR module is in common mode i get the following in Serial monitor

Group1 Imported !
Common Mode
Result:12
Result:14
Result:11
Result:13
Result:15
Result:12

then When in Compact mode I get

Ì

thats all even when i speak there is no other output on the serial monitor

UPDATE: the " Ì " is HEX for "cc" whichin compact mode means " successfully setup compact mode"

Hi Paul

thanx for your help I got the unit to work as intended , and i let the supplier know that there example code does not work, next step is to program a better library to be able to do word learning via arduino without a PC

will post a video later when in better working order

I got the unit to work as intended

That's great.

Hi Paul

Im onto the next issue now but im looking for an idea of how to do it rather then a the way im doing it now.

there is two things i need help with.

  1. i have my VR module now truning on a LED when i say "red" and they off when i say " off" but this takes up an extra command for one action as in on and of for one LED , i want to do StateChangeDetection with the word "red" so when i say red the led goes high and when i say red again the "led" goes "low" my problem is the statechangedetection reads from a pin which i dont have in the software. how do i go about this ? i know i can run a wire from the red light to another input and just read the input if its high or low but thats wasting pins.

  2. How do i program the unit to go into a specific Case and stay looping in that case untill i give it a command to exit back to the main program? currently it keeps reverting back to the top of the code.

i want to do StateChangeDetection with the word "red" so when i say red the led goes high and when i say red again the "led" goes "low" my problem is the statechangedetection reads from a pin which i dont have in the software.

If you are referring to an example called "StateChangeDetection", the pin doesn't matter.

All you need to do is keep track of whether the red LED is on or off. When you get a "red", change the state to the other state, and write the new state to the red LED's pin.

  1. How do i program the unit to go into a specific Case and stay looping in that case untill i give it a command to exit back to the main program? currently it keeps reverting back to the top of the code.

We'd need to see your code. You can define loops in a case, but you must provide a way for that loop to end.

Hi Paul

okay iv had a rethink on the way i want the VR module code to work. in the manual it states that the VR unit can handle 3 groups of 5 words each , and only one group can be used at a time, this seems like an issue but it is not, see i have desided to break my code into 3 groups.

group 1 is environmental group ie which room in the house do i want to control ,

group 2 is for which appliance in that room do i want to select

group 3 is the control action i want to apply to that appliance.

currently i have all 3 groups in a switch case , but because i can only have one group imported at a time only that words that imported works out of the whole switch case.

in my code below i used "bar" in group 1 "lights" in group 2 and "start" in group 3
so what should happen is i call out "bar lights start" and the bar lights should switch on, now my problem is when i declare in setup what the bar lights pin is it does switch on as intended and off but because i want to use group 3 as a control group of words, the pinout must be vairable and be int. each time I enter that appliance void function. I have tried creating a global vairable called " int SwitchPin = Pin; " and "int Pin =0;" then when i enter group 2 "lights" in the lights void i " int Pin =9; " and then when i say "start" it should switch on ... it doesnt

#include <SoftwareSerial.h> // use for software serial tobe able to monitor serial output will speaking into the VR module 
                    
SoftwareSerial mySerial(2, 3); // RX, TX software serial pin selection 

// Variables declaration
int SwitchPin = Pin;
int Pin = 0;

int redPin = 11;   // R petal on RGB LED module connected to digital pin 11
int greenPin = 9;  // G petal on RGB LED module connected to digital pin 9
int bluePin = 10;  // B petal on RGB LED module connected to digital pin 10
byte com = 0;      // Reply from voice recognition

// Program Setup
void setup() 
{ 
  // Serial communication Speed 
  Serial.begin(38400);
  mySerial.begin(38400);

 // PinMode declaration
pinMode(redPin, OUTPUT); // sets the redPin to be an output
pinMode(greenPin, OUTPUT); // sets the greenPin to be an output
pinMode(bluePin, OUTPUT); // sets the bluePin to be an output
pinMode(Pin,OUTPUT);

 //Device Setup via AT commands 
    delay(1000);
  mySerial.write(0xAA);
  mySerial.write(0x37); // Switch module into compact mode 
    delay(1000);
  mySerial.write(0xAA);
  mySerial.write(0x21); // Import Group1 of 5 words 
      delay(1000);
} 

// Program starts 

void loop() 
{ while(mySerial.available())
{ com =  mySerial.read();
     Serial.println(com,HEX);
     switch(com)
// Rooms
{
  case 0x11:
break;
  case 0x12:
break;
  case 0x14:
break;
  case 0x13:
break;
  case 0x15:
    barGroup(); // turn the RGB LED off
break;

// Appliance
  case 0x31:
    Serial.println("BAR"); 
break;
  case 0x32:
    Serial.println("lights"); 
    lights();
break;
  case 0x33:
    Serial.println("Garage"); 
break;
  case 0x34:
    Serial.println("Music"); 
break;
  case 0x35:
    Serial.println("Strobe"); 
    
// Control
break;
  case 0x21:
    Serial.println("start"); 
  Start();
break;
  case 0x22:
    Serial.println("off"); 
break;
  case 0x23:
    Serial.println("dim"); 
break;
  case 0x24:
    Serial.println("stop"); 
break;
  case 0x25:
    Serial.println("all off"); 
    off();
break;

}
}
} 


void barGroup()
{
  mySerial.write(0xAA);
  mySerial.write(0x23); // Import Group3 of 5 words 
      delay(1000);
       Serial.println("bargroup activated");
      while(mySerial.available())
     { com =  mySerial.read();
      Serial.println("reading serail");
      Serial.println(com,HEX);
     }}
     
void CtrlGroup()
{  
  mySerial.write(0xAA);
  mySerial.write(0x22); // Import Group2 of 5 words 
      delay(1000);
       Serial.println("controls activated");
      while(mySerial.available())
     { com =  mySerial.read();
      Serial.println("reading serail");
      Serial.println(com,HEX);
     }

  }
void Start()
{
   digitalWrite(SwitchPin,HIGH);
}

void off()
{ 
   digitalWrite(SwitchPin,LOW);
}

void lights()
{
int Pin = 9;
  
  CtrlGroup();
}

how do i create function that switches pin outs for a function like start() and Stop()

LOL murhpy is a bastard , as i finished posting i found the answer

as i finished posting i found the answer

But, you don't want to share it?

yes i will just want to finish it for all 5 groups , question when im a switch case and i want to reset to go back to the main loop is there a Arduino function to reset the code or go back to the start ?