Hi all, could anyone help me on this? I doing the voice password to trigger the led. I set my password to "1234" and once it correct, the led will be light up. But I cant get that works. Please help me on this. Your help is much appreciate!
#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;
#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)
enum Groups
{
GROUP_1 = 1,
GROUP_2 = 2,
GROUP_16 = 16,
};
enum Group1
{
G1_PASSWORD = 1,
G1_CODE_NUMBER = 2,
};
enum Group2
{
G2_ZERO = 0,
G2_ONE = 1,
G2_TWO = 2,
G2_THREE = 3,
G2_FOUR = 4,
G2_FIVE = 5,
G2_SIX = 6,
G2_SEVEN = 7,
G2_EIGHT = 8,
G2_NINE = 9,
G2_DELETE = 10,
};
enum Group16
{
G16_OPENLOCK = 0,
};
int8_t group;
int8_t pci=0;
int form = 0;
char passcode[5] = "1234";
char usercode[5] = {'x','x','x','x','\0'};
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 action();
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);
}
action();
}
void action()
{
switch(group)
{
case GROUP_1:
switch(buf[1])
{
case G1_PASSWORD:
group = 16;
break;
case G1_CODE_NUMBER:
form = 2;
group = 2;
break;
}
break;
case GROUP_2:
switch(buf[1])
{
case G2_ZERO:
if(!passCode('0',0))
{
group = 2;
}
break;
case G2_ONE:
if(!passCode('1',1))
{
group = 2;
}
break;
case G2_TWO:
if(!passCode('2',2))
{
group = 2;
}
break;
case G2_THREE:
if(!passCode('3',3))
{
group = 2;
}
break;
case G2_FOUR:
if(!passCode('4',4))
{
group = 2;
}
break;
case G2_FIVE:
if(!passCode('5',5))
{
group = 2;
}
break;
case G2_SIX:
if(!passCode('6',6))
{
group = 2;
}
break;
case G2_SEVEN:
if(!passCode('7',7))
{
group = 2;
}
break;
case G2_EIGHT:
if(!passCode('8',8))
{
group = 2;
}
break;
case G2_NINE:
if(!passCode('9',9))
{
group = 2;
}
break;
case G2_DELETE:
pci--;
usercode[pci] = 'x';
Serial.println("delete number");
Serial.print("pci: ");
Serial.println(pci);
if(pci<=0)
{
pci=0;
}
break;
case GROUP_16:
switch(buf[1])
{
case G16_OPENLOCK:
Serial.println("password correct");
delay(1000);
digitalWrite(led,HIGH);
break;
}
break;
}
}
}
bool passCode(char code_num, int number)
{
usercode[pci] = code_num;
Serial.println(number);
Serial.println(usercode);
Serial.println(pci);
pci++;
if(pci>3)
{
return comparePasscode();
}
return false;
}
bool comparePasscode()
{
if(!strcmp(usercode,passcode))
{
Serial.println(passcode);
Serial.println(usercode);
Serial.println("passcode corect");
delay(2000);
return true;
}
else
{
Serial.println(passcode);
Serial.println(usercode);
Serial.println("passcode inccorect");
delay(2000);
return false;
}
}
/**
@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");
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(", ");
}
}