Hi, can anyone tell me how I going to know what input digit did I enter? How to define it?
Below is my code. Thanks.
#include "VoiceRecognitionV4.h" //VOICE using HardwareSerial
#include <Servo.h>
// 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;
int led = 13;
Servo lock;
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, OUTPUT);
digitalWrite(led, LOW);
lock.attach(A3);
pinMode(A3,OUTPUT);
lock.write(0);
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, HIGH);
openDoor();
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.");
digitalWrite(led,LOW);
closeDoor();
// 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)
{
NewPasscode1[EnteredDigitsIndex++] = input + '0'; // Convert to character
NewPasscode1[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)
{
NewPasscode2[EnteredDigitsIndex++] = input + '0'; // Convert to character
NewPasscode2[EnteredDigitsIndex] = '\0'; // Null terminator
}
if (input == ENTER_COMMAND)
{
if (strcmp(NewPasscode1, NewPasscode2) == 0)
{
Serial.println("New password set. Say 'Clear' to lock.");
strcpy(PasscodeDigits, NewPasscode2); // 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[1];
return -1; // No input this time
}
void openDoor()
{
Serial.println("Door open");
lock.attach(A3);
lock.write(90);
delay(1000);
lock.detach();
delay(500);
}
void closeDoor()
{
lock.attach(A3);
delay(1000);
lock.write(0);
delay(1000);
lock.detach();
}