Voice password arduino using voice recognition V3

Hi all, can anyone pls tell me on how to code the voice password based system. By referring to the code below, I set my voice password to "1234" and once it correct, the led will be light up. voice command "zero" is use to wake up the device. However, I cant get work on this code. Please advise me. Thank you.

#include "VoiceRecognitionV4.h" //VOICE using serial port library

VR myVR1(&Serial1);
VR myVR2(&Serial2);


uint8_t record[10]; // save record
uint8_t buf[32];
int led = 13;

char code[]= {'1','2','3','4'};  //The default code, you can change it or make it a 'n' digits one
char code_buff1[sizeof(code)];
int8_t k=0,i=0;

#define group0Record0       (0)
#define group0Record1       (1) 
#define group0Record2       (2) 
#define group0Record3       (3) 
#define group0Record4       (4) 
#define group0Record5       (5) 
#define group0Record6       (6) 

#define group1Record0       (0) 
#define group1Record1       (1) 
#define group1Record2       (2) 


void setup()
{
  /** initialize */
  Serial1.begin(9600);
  Serial2.begin(9600);
  
  Serial.begin(115200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nMulti Commands sample");
  
  pinMode(led, OUTPUT);
    
  
  record[0] = group0Record0;
  record[1] = group0Record1;
  record[2] = group0Record2;
  record[3] = group0Record3;
  record[4] = group0Record4;
  record[5] = group0Record5;
  record[6] = group0Record6;
 
  record[0] = group1Record0;
  record[1] = group1Record1;
  record[2] = group1Record2;

  
  if(myVR1.load(record, 7) >= 0){
    printRecord(record, 7);
    Serial.println(F("loaded."));
  }

  if(myVR2.load(record, 3) >= 0){
    printRecord(record, 3);
    Serial.println(F("loaded."));
  }


}


void loop()
{
  int ret,ret1;

  ret = myVR1.recognize(buf, 50);
  ret1 = myVR2.recognize(buf, 50);
  if(ret>0)
  {
    /** voice recognized */
    printVR(buf);


  }
  if(ret1>0)
  {
    /** voice recognized */
    printVR(buf);
  }


  if(ret == group0Record0)
  {
     Serial.println("Enter code: ");
     actions();
            if(k==sizeof(code))
            digitalWrite(led,HIGH);
            else
            {
              digitalWrite(led,LOW);
            }
  }



}

void actions()
{
  i=0;
  k=0;
  int ret;


  while(ret != group0Record5)
  {
      ret = myVR1.recognize(buf, 50);
      myVR1.read();
    if(ret != group0Record5)
    {
     
      if(ret == code[i] && i<sizeof(code))
      {
        k++;
        i++;
      }
      else
      {
         k--;
      }
      
    }
  }
}


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");
  Serial.println();
}

void printRecord(uint8_t *buf, uint8_t len)
{
  Serial.print(F("Record: "));
  for(int i=0; i<len; i++){
    Serial.print(buf[i], DEC);
    Serial.print(", ");
  }
}

I would advise you to tell us what hardware you are using and how you have connected it up.

I'm using Arduino Mega. Two voice recognition V3 module since one module only limited to 7 command effective at the same time.

For connection. Two module are using serial port communication.

Have you tried getting your code to work with 1 module?

I assume that the voice recognition module you have is from Elechouse? That's the first one I found when Googling for "Arduino voice recognition v3".

Try this sketch. Does it recognize the digits from zero to nine when you say them? What does it show on Serial Monitor?

// Board: Arduino MEGA 2560
#include "VoiceRecognitionV4.h" //VOICE using serial port library

VR myVR1(&Serial1);
VR myVR2(&Serial2);

uint8_t buf[32];

uint8_t VR1RecordEnables[7] =
{
  0, // Zero
  1, // One
  2, // Two
  3, // Three
  4, // Four
  5, // Five
  6, // Six
};

uint8_t VR2RecordEnables[3] =
{
  0, // Seven
  1, // Eight
  2, // Nine
};


void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nMulti Commands sample");

  /** initialize */
  Serial1.begin(9600);
  Serial2.begin(9600);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW); // Turn LED off

  if (myVR1.clear() == 0)
  {
    Serial.println("Recognizer 1 cleared.");
  }
  else
  {
    Serial.println("Not find VoiceRecognitionModule 1.");
    Serial.println("Please check connection and restart Arduino.");
    while (1);
  }

  if (myVR2.clear() == 0)
  {
    Serial.println("Recognizer 2 cleared.");
  }
  else
  {
    Serial.println("Not find VoiceRecognitionModule 2.");
    Serial.println("Please check connection and restart Arduino.");
    while (1);
  }

  if (myVR1.load(VR1RecordEnables, 7) >= 0)
  {
    Serial.println(F("VR1 loaded."));
  }

  if (myVR2.load(VR2RecordEnables, 3) >= 0)
  {
    Serial.println(F("VR2 loaded."));
  }
}


void loop()
{
  int ret;

  // Check for voice input from VR1
  ret = myVR1.recognize(buf, 50);
  if (ret > 0)
  {
    // voice recognized
    printVR(buf);
  }


  // Check for voice input from VR2
  ret = myVR2.recognize(buf, 50);
  if (ret > 0)
  {
    // voice recognized
    printVR(buf);
  }
}

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");
  Serial.println();
}

ya correct, is elechouse. I try using first one befor but doesnt works

Yes. It does. It will show the digit from zero to nine in serial monitor

It should show a lot more than that.

but i only train from digit zero to digit nine. It should display these number only right?

And how I going to define this variable so that it will recognize my command and turn on the led?

It's if(ret == 1) ?

if(ret == group0Record0)
  {
     Serial.println("Enter code: ");
     actions();
            if(k==sizeof(code))
            digitalWrite(led,HIGH);
            else
            {
              digitalWrite(led,LOW);
            }
  }

Have you TRIED the sketch? What output did you see on Serial Monitor?

I have tried. When i speak from zero to nine. It display this number

Then something is terribly wrong. For each word it recognizes it is SUPPOSED to display the header:
'VR Index Group RecordNum Signature`
with data under each column. If it is only showing one digit then the output is very wrong.

ohh no no. It have display the one that u said just now. T havemisunderstood ur meaning.

These is the output from serial.

VR Index	Group	RecordNum	Signature
5		NONE	5		five
VR Index	Group	RecordNum	Signature
0		NONE	0		zero
VR Index	Group	RecordNum	Signature
1		NONE	8		eight
VR Index	Group	RecordNum	Signature
0		NONE	0		zero
VR Index	Group	RecordNum	Signature
1		NONE	1		one
VR Index	Group	RecordNum	Signature
2		NONE	2		two
VR Index	Group	RecordNum	Signature
3		NONE	3		three

So you said "five zero eight zero one two three" and it recognized every word correctly? That is excellent!

Now you can take the places where 'printVR(buf)' are called and use the digit in 'buf[2]' (Record Number) to add to your entered password. When you have four digits entered, compare them against your master password.

Ya it recognized the word correctly. But I dun knw how the way to write the password in voice format. Could you give an example on settings 2 digit of password to me?

From the code you sent by Direct message it looks like you failed to do this:

You commented out the two calls to 'printVR(buf)' and didn't put anything in their place. You also use buf[1] instead of buf[2]. Here is how I would write it.

// Board: Arduino MEGA 2560
#include "VoiceRecognitionV4.h" //VOICE using serial port library

VR myVR1(&Serial1);
VR myVR2(&Serial2);

uint8_t buf[32];

const byte PasswordLength = 2;
const byte PasswordDigits[PasswordLength] = {1, 2};
byte EnteredDigits[PasswordLength];
byte EnteredDigitsIndex = 0;

uint8_t VR1RecordEnables[7] =
{
  0, // Zero
  1, // One
  2, // Two
  3, // Three
  4, // Four
  5, // Five
  6, // Six
};

uint8_t VR2RecordEnables[3] =
{
  0, // Seven
  1, // Eight
  2, // Nine
};


void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nMulti Commands sample");

  /** initialize */
  Serial1.begin(9600);
  Serial2.begin(9600);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW); // Turn LED off

  if (myVR1.clear() == 0)
  {
    Serial.println("Recognizer 1 cleared.");
  }
  else
  {
    Serial.println("Not find VoiceRecognitionModule 1.");
    Serial.println("Please check connection and restart Arduino.");
    while (1);
  }

  if (myVR2.clear() == 0)
  {
    Serial.println("Recognizer 2 cleared.");
  }
  else
  {
    Serial.println("Not find VoiceRecognitionModule 2.");
    Serial.println("Please check connection and restart Arduino.");
    while (1);
  }

  if (myVR1.load(VR1RecordEnables, 7) >= 0)
  {
    Serial.println(F("VR1 loaded."));
  }

  if (myVR2.load(VR2RecordEnables, 3) >= 0)
  {
    Serial.println(F("VR2 loaded."));
  }
}

void EnterDigit(byte digit)
{
  Serial.print("Entred digit: ");
  Serial.println(digit);

  EnteredDigits[EnteredDigitsIndex++] = digit;
  
  if (EnteredDigitsIndex >= PasswordLength)
  {
    Serial.println("Entry complete.");

    for (int i = 0; i < PasswordLength; i++)
    {
      if (PasswordDigits[i] != EnteredDigits[i])
      {
        // Password mismatch
        Serial.println("password incorrect");

        EnteredDigitsIndex = 0;  // Start over
        return;
      }
    }
    // Success!
    Serial.println("password correct");
    EnteredDigitsIndex = 0;  // Start over
    return;
  }
}

void loop()
{
  int ret;

  // Check for voice input from VR1
  ret = myVR1.recognize(buf, 50);
  if (ret > 0)
  {
    // voice recognized
    EnterDigit(buf[2]);
  }

  // Check for voice input from VR2
  ret = myVR2.recognize(buf, 50);
  if (ret > 0)
  {
    // voice recognized
    EnterDigit(buf[2]);
  }
}

Alright. Thanks! It works for me! and I have change it according to the requirement of FYP. Next, I would proceed on changeable passcode without the need of changing in coding but directly from voice input.