Hi all, could anyone help me on this? I'm now wanted to add a command called "enter" so that when this command was called, it only ask for password enter by user, otherwise it cannot performed anything.
The problem is that when I have speak the command "enter", it ask for enter password, but the password I speak doesnt run in the function. Below is my code.
#include "VoiceRecognitionV4.h" //VOICE using serial port library
VR myVR1(&Serial1);
VR myVR2(&Serial3);
uint8_t buf[32];
int led = 13;
int i=0;
const byte PasswordLength = 2;
const byte PasswordDigits[PasswordLength] = {1, 2};
byte EnteredDigits[PasswordLength];
byte EnteredDigitsIndex = 0;
byte newcode1[PasswordLength];
uint8_t VR1RecordEnables[7] =
{
0, // Zero
1, // One
2, // Two
3, // Three
4, // Four
5, // Five
6, // Six
};
uint8_t VR2RecordEnables[5] =
{
7, // Seven
8, // Eight
9, // Nine
10, //changepassword
11, //enter
};
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println("Elechouse Voice Recognition V3 Module\r\nMulti Commands sample");
/** initialize */
Serial1.begin(9600);
Serial3.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(led, 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, 5) >= 0)
{
Serial.println(F("VR2 loaded."));
}
}
void loop()
{
int ret;
ret = myVR1.recognize(buf, 50);
if (ret > 0)
{
if(buf[1] == 11)
{
// voice recognized
EnterDigit(buf[2]);
}
}
ret = myVR2.recognize(buf, 50);
if (ret > 0)
{
if(buf[1] == 11) //enter command
{
Serial.println("Enter password: ");
delay(500);
EnterDigit(buf[1]);
}
}
}
void EnterDigit(byte digit)
{
Serial.print("Entered 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");
digitalWrite(led,HIGH);
EnteredDigitsIndex = 0; // Start over
return;
}
}
void GetNewCode1(byte digit)
{
i=0;
Serial.println("Enter a new code: ");
delay(2000);
EnteredDigits[EnteredDigitsIndex++] = digit;
Serial.println(digit);
while(buf[1] != 11)
{
if(buf[1] != 11)
{
newcode1[i] = EnteredDigits[i];
i++;
}
}
}
This is something I want. Because later on I have to set two condition.
1st condition: "Enter" command to let user proceed to enter the password which is predefined.
2nd condition: "changepassword" command to let user proceed to change the password which is predefined at the beginnning.
#include "VoiceRecognitionV4.h" //VOICE using HardwareSerial
// Requires Arduino MEGA 2560
VR VR1(&Serial1);
VR VR2(&Serial3);
const byte MaxPasscodeLength = 16;
char PasscodeDigits[MaxPasscodeLength] = "12";
char EnteredDigits[MaxPasscodeLength];
char NewPasscode1[MaxPasscodeLength];
char NewPasscode2[MaxPasscodeLength];
byte EnteredDigitsIndex = 0;
const int CHANGE_PASSWORD_COMMAND = 10;
const int ENTER_COMMAND = 11;
const int CLEAR_COMMAND = 12;
uint8_t VR1RecordEnables[7] =
{
0, // Zero
1, // One
2, // Two
3, // Three
4, // Four
5, // Five
6, // Six
};
uint8_t VR2RecordEnables[6] =
{
7, // Seven
8, // Eight
9, // Nine
10, // Changepassword
11, // Enter
12, // Clear
};
enum States {EnteringPasscode, Unlocked, EnteringNew1, EnteringNew2} State = EnteringPasscode;
void setup()
{
Serial.begin(115200);
delay(200);
/** initialize */
Serial1.begin(9600); // Arduino MEGA 2560 required
Serial3.begin(9600); // Arduino MEGA 2560 required
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Locked. Say password digits followed by 'Enter' for access.");
if (VR1.clear() == 0)
{
Serial.println("VR1 cleared.");
}
else
{
Serial.println("VR1 not found!");
Serial.println("Please check connection and restart Arduino.");
while (1);
}
if (VR2.clear() == 0)
{
Serial.println("VR2 cleared.");
}
else
{
Serial.println("VR2 not found!");
Serial.println("Please check connection and restart Arduino.");
while (1);
}
if (VR1.load(VR1RecordEnables, 7) >= 0)
{
Serial.println(F("VR1 loaded."));
}
else
{
Serial.println("VR1 load failed!");
Serial.println("Please check connection and restart Arduino.");
while (1);
}
if (VR2.load(VR2RecordEnables, 6) >= 0)
{
Serial.println(F("VR2 loaded."));
}
else
{
Serial.println("VR2 load failed!");
Serial.println("Please check connection and restart Arduino.");
while (1);
}
}
void loop()
{
int input;
do
{
input = GetInput();
}
while (input < 0);
switch (State)
{
case EnteringPasscode:
if (input < 10 && EnteredDigitsIndex < MaxPasscodeLength - 1)
{
EnteredDigits[EnteredDigitsIndex++] = input + '0'; // Convert to character
EnteredDigits[EnteredDigitsIndex] = '\0'; // Null terminator
}
if (input == CHANGE_PASSWORD_COMMAND)
{
Serial.println("Can't change password while locked.");
Serial.println("Locked. Say password digits followed by 'Enter' for access.");
// Start Over
EnteredDigitsIndex = 0;
EnteredDigits[0] = '\0';
}
if (input == ENTER_COMMAND)
{
// Does it match?
if (strcmp(EnteredDigits, PasscodeDigits) == 0)
{
// Yes. Unlock.
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Unlocked. Say 'clear' to lock.");
State = Unlocked;
}
else
{
// Not a match
Serial.println("Wrong password. Try again.");
// Start Over
EnteredDigitsIndex = 0;
EnteredDigits[0] = '\0';
}
}
if (input == CLEAR_COMMAND)
{
Serial.println("Input cleared. Say password digits followed by 'Enter' for access.");
// Start Over
EnteredDigitsIndex = 0;
EnteredDigits[0] = '\0';
}
break;
case Unlocked:
if (input == CHANGE_PASSWORD_COMMAND)
{
Serial.println("Say digits for new password followed by 'Enter'.");
// Start Over
EnteredDigitsIndex = 0;
NewPasscode1[0] = '\0';
State = EnteringNew1;
}
else if (input == CLEAR_COMMAND)
{
Serial.println("Locking. Say password digits followed by 'Enter' for access.");
// Start Over
EnteredDigitsIndex = 0;
EnteredDigits[0] = '\0';
State = EnteringPasscode;
}
else
{
Serial.println("Invalid input while unlocked. Say 'Clear' to lock.");
}
break;
case EnteringNew1:
if (input < 10 && EnteredDigitsIndex < MaxPasscodeLength - 1)
{
NewPasscode2[EnteredDigitsIndex++] = input + '0'; // Convert to character
NewPasscode2[EnteredDigitsIndex] = '\0'; // Null terminator
}
if (input == ENTER_COMMAND)
{
if (EnteredDigitsIndex == 0)
{
// EMPTY PASSWORD
Serial.println("New password cannot be empty. Say digits for new password followed by 'Enter'.");
}
else
{
Serial.println("Repeat the new password followed by 'Enter'.");
EnteredDigitsIndex = 0;
NewPasscode2[0] = '\0';
State = EnteringNew2;
}
}
if (input == CLEAR_COMMAND)
{
Serial.println("Input cleared. Say digits for new password followed by 'Enter'.");
Serial.println("Or say'Changepassword' to cancel change.");
// Start Over
EnteredDigitsIndex = 0;
NewPasscode1[0] = '\0';
}
if (input == CHANGE_PASSWORD_COMMAND)
{
Serial.println("Password change canceled.");
State = Unlocked;
}
break;
case EnteringNew2:
if (input < 10 && EnteredDigitsIndex < MaxPasscodeLength - 1)
{
NewPasscode1[EnteredDigitsIndex++] = input + '0'; // Convert to character
NewPasscode1[EnteredDigitsIndex] = '\0'; // Null terminator
}
if (input == ENTER_COMMAND)
{
if (strcmp(NewPasscode1, NewPasscode2) == 0)
{
Serial.println("New password set. Say 'Clear' to lock.");
strcpy(PasscodeDigits, NewPasscode1); // Replace previous password
}
else
{
Serial.println("Repeat did not match. Password change canceled. Say 'Clear' to lock.");
}
State = Unlocked;
}
if (input == CLEAR_COMMAND)
{
Serial.println("Input cleared. Repeat digits for new password followed by 'Enter'.");
Serial.println("Or say'Changepassword' to cancel change.");
// Start Over
EnteredDigitsIndex = 0;
NewPasscode2[0] = '\0';
}
if (input == CHANGE_PASSWORD_COMMAND)
{
Serial.println("Password change canceled.");
State = Unlocked;
}
break;
}
}
int GetInput()
{
uint8_t buf[32];
int ret;
// Check for input from VR1
ret = VR1.recognize(buf, 50);
if (ret > 0)
return buf[2];
// No input from VR1. Check for input from VR2
ret = VR2.recognize(buf, 50);
if (ret > 0)
return buf[2];
return -1; // No input this time
}