Keypad and voice VR3 mode change

Hi all, can someone help me on the mode changing part? For my case, I have set the voice command "keypad". So, when this command was called, it will change to keypad mode. So that i can enter password through keypad but not voice. Below is my code. Thanks.

#include "VoiceRecognitionV4.h" //VOICE using HardwareSerial 
#include <Servo.h>
#include <TMRpcm.h>
#include <SD.h>                           //include SD module library                     
#include <SPI.h>          
#include <Keypad.h>
#include <EEPROM.h>

#define SD_ChipSelectPin 53

TMRpcm tmrpcm;

// Requires Arduino MEGA 2560
VR VR1(&Serial1);
VR VR2(&Serial3);

const byte MaxPasscodeLength = 16;
char PasscodeDigits[MaxPasscodeLength] = "124895";
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;
const int KEYPAD_COMMAND = 13;     

//keypad
const byte numRows= 4;          //number of rows on the keypad
const byte numCols= 4;          //number of columns on the keypad

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

char keypressed;//Where the keys are stored it changes very often
char code[]= {'1','2','3','4','5','6'};  //The default code, you can change it or make it a 'n' digits one

char code_buff1[sizeof(code)];  //Where the new key is stored
char code_buff2[sizeof(code)];  //Where the new key is stored again so it's compared to the previous one

short k=0,i=0,s=0,j=0;          //Variables used later

byte rowPins[numRows] = {32,30,28,26}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols]= {24,22,7,6}; //Columns 0 to 3


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

uint8_t VR2RecordEnables[7] =
{
  7, // Seven
  8, // Eight
  9, // Nine
  10, // Changepassword
  11, // Enter
  12, // Clear
  13, //keypad
};


enum States {EnteringPasscode, Unlocked, EnteringNew1, EnteringNew2} State = EnteringPasscode;
int led = 13;
Servo lock;   
int mode1 = 0;
int doorSens = A1;
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
#define buzzer A2 

void setup()
{
  //Serial.begin(115200);
  delay(200);
  /** initialize */
  Serial1.begin(9600);  // Arduino MEGA 2560 required
  Serial3.begin(9600);  // Arduino MEGA 2560 required

  //keypad............................................
  Serial.begin(9600);
  pinMode(A1,INPUT_PULLUP);
  pinMode(doorSens, INPUT_PULLUP);                 
  Serial.println("STANBY!");
  //...................................................
  
  tmrpcm.speakerPin = 5; 
  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, 7) >= 0)
  {
    Serial.println(F("VR2 loaded."));
  }
  else
  {
    Serial.println("VR2 load failed!");
    Serial.println("Please check connection and restart Arduino.");
    while (1);
  }


          for(i=0 ; i<sizeof(code);i++)
          {        //When you upload the code the first time keep it commented
            EEPROM.get(i, code[i]);             //Upload the code and change it to store it in the EEPROM
          }  

           
     if (!SD.begin(SD_ChipSelectPin)) 
     {      //see if the card is present and can be initialized
        Serial.println("SD fail");
        speaker11();
        return;                               //don't do anything more if not
     }
     else
     {
      Serial.println("sucess");
      speaker10();
     }
}




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 == KEYPAD_COMMAND)
      {
        Serial.println("keypad mode");
        loop1();
      }

      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;
  }
}

void loop1()
{

  myKeypad.addEventListener(keypadEvent);
  myKeypad.getKey();
    
  keypressed = myKeypad.getKey(); 
  
    if(keypressed == '*')
    {
            
            Serial.println("Enter code");
            speaker3();
            delay(2000);
            GetCode();                          //Getting code function
                  if(k==sizeof(code))
                   openDoor1();
                  else
                  {
                    Serial.println("Wrong passowrd");
                    speaker2();
                 
                  
                  }
            delay(1000);
            Serial.println("Stanby!");
          
    }

     if(keypressed == '#')
     {
   
      ChangeCode();
      delay(1000);
      Serial.println("Stanby!");                 //When done it returns to standby mode
     }
  

}

void keypadEvent(KeypadEvent eKey) 
{
  switch (myKeypad.getState()) {
    case PRESSED:

      Serial.print("Enter:");
      Serial.println(eKey);
      playsound();
      }
}

void playsound()
{
  tone(buzzer, 131);
  delay(200);
  noTone(buzzer);
}

void GetCode(){                  //Getting code sequence
       i=0;                      //All variables set to 0
       k=0;
       //j=0; 
                                     
     while(keypressed != 'A')
     {                                     //The user press 0 to confirm the code otherwise he can keep typing
           keypressed = myKeypad.getKey();                         
            if(keypressed != NO_KEY && keypressed != 'A' )
            {       //If the char typed isn't A and neither "nothing"
              //Serial.println(j,1);                                  //This to write "*" on the LCD whenever a key is pressed it's position is controlled by j
             // Serial.println("*");
              //j++;
            if(keypressed == code[i]&& i<sizeof(code))
            {            //if the char typed is correct a and i increments to verify the next caracter
                 k++;                                              //Now I think maybe I should have use only a or i ... too lazy to test it -_-'
                 i++;
            
            }
            else
            {
                k--;   
          
            }
            //if the character typed is wrong a decrements and cannot equal the size of code []
            }
     }
            
    keypressed = NO_KEY;

}


void ChangeCode(){                      //Change code sequence
      Serial.println("Changing code");
      speaker4();
      delay(1000);
      Serial.println("Enter old code");
      speaker5();
      GetCode();                      //verify the old code first so you can change it
      
            if(k==sizeof(code))
            {      //again verifying the a value
            Serial.println("Changing code");
            GetNewCode1();            //Get the new code
            GetNewCode2();            //Get the new code again to confirm it
            s=0;
              for(i=0 ; i<sizeof(code) ; i++){     //Compare codes in array 1 and array 2 from two previous functions
              if(code_buff1[i]==code_buff2[i])
              s++;                                //again this how we verifiy, increment s whenever codes are matching
              }
                  if(s==sizeof(code)){            //Correct is always the size of the array
                  
                   for(i=0 ; i<sizeof(code) ; i++){
                  code[i]=code_buff2[i];         //the code array now receives the new code
                  EEPROM.put(i, code[i]);        //And stores it in the EEPROM
                  
                  }
                  Serial.println("Code Changed");
                  speaker8();
                  delay(3000);
                  }
                  else{                         //In case the new codes aren't matching
            
                  Serial.println("Code not match");
                  speaker9();
                  delay(2000);
                  }
            
          }
          
          else{                     //In case the old code is wrong you can't change it
          Serial.println("Wrong");
          delay(2000);
          }
}

void GetNewCode1(){                      
  i=0;
  //j=0;
  Serial.println("Enter a new code and press A");
  speaker6();
  delay(2000);
             
         while(keypressed != 'A'){            //A to confirm and quits the loop
             keypressed = myKeypad.getKey();
               if(keypressed != NO_KEY && keypressed != 'A' ){
                //Serial.println(j,0);
                //Serial.println(ekey);
                code_buff1[i]=keypressed;     //Store caracters in the array
                i++;
                //j++;              
                }
                }
keypressed = NO_KEY;
}

void GetNewCode2(){                         //This is exactly like the GetNewCode1 function but this time the code is stored in another array
  i=0;
  //j=0;
  
  Serial.println("Confirm code and press A");;
  speaker7();
  delay(3000);

         while(keypressed != 'A'){
             keypressed = myKeypad.getKey();
               if(keypressed != NO_KEY && keypressed != 'A' ){
               //Serial.println(j,0);
               //Serial.println("*");
               //Serial.println("nothing");
                code_buff2[i]=keypressed;
                i++;
                //j++;             
                }
                }
keypressed = NO_KEY;
}

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();
  
}

void openDoor1() //keypad mode
{             //Lock opening function open for 3s
  
  Serial.println("Door open"); 
  lock.attach(A3);
  delay(1000);
  lock.write(90);
  delay(1000);
  lock.detach();
  delay(500);
  speaker();
 
  
  while(digitalRead(doorSens)){
    Serial.println("waiting for the action");  
  }
  closeDoor1();
 
}

void closeDoor1(){ //keypad mode

  lock.attach(A3);
  delay(1000);
  lock.write(0);
  delay(1000);
  lock.detach(); 
  delay(1000);
  speaker1();
 
  
}

void speaker()//open door
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("1.wav"); 
  }
  
}

void speaker1()//close door
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("3.wav"); 
  }
  
}

void speaker2()//wrong password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("2.wav"); 
  }
  
}

void speaker3()//please enter your password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("4.wav"); 
  }
  
}

void speaker4()//change password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("5.wav"); 
  }
  
}

void speaker5()//enter old password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("6.wav"); 
  }
  
}

void speaker6()//enter new password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("7.wav"); 
  }
  
}

void speaker7()//confirm password
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("8.wav"); 
  }
  
}

void speaker8()//password changed successful
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("9.wav"); 
  }
  
}

void speaker9()//password not match
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("10.wav"); 
  }
  
}

void speaker10()//System sucess
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("11.wav"); 
  }
  
}

void speaker11()//system fail
{
  if (SD.begin(SD_ChipSelectPin))
  {
     tmrpcm.quality(1);
     tmrpcm.setVolume(5);                    //0 to 7. Set volume level
     tmrpcm.play("12.wav"); 
  }
  
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.