Keypad and speaker works simultaneously

Hi all, Now I'm facing an issues which to works with both speaker and keypad together. The correct logic is that when I enter the correct password, the servo motor will turn and speaker to speech the specified word that have been set well. Then after the key '#' was pressed, the servo will turn back to the origin position in order to close the door. But the problem now is that after I enter the correct password, the servo will turned and the speaker to output the words. Then, when I press key '#', the servo wont turn back again but the speaker still works. Please advise how to proceed ya guys. Thanks. Below is my code.

#include <Password.h>
#include <Keypad.h> 
#include <Servo.h> 
#include <SD.h>                           //include SD module library
#include <TMRpcm.h>                       //include speaker control library
#include <SPI.h>
#define SD_ChipSelectPin 10 


Servo myservo; //declares servo
Password password = Password( "123" ); //password to unlock, can be changed
TMRpcm tmrpcm; 

  const byte ROWS = 4; // Four rows
  const byte COLS = 3; // columns
  // Define the Keymap
  char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
  };
  // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  byte rowPins[ROWS] = { 8, 7, 6, 5 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  byte colPins[COLS] = { 4, 3, 2 };
  

// Create the Keypad
  Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  Serial.begin(9600);
  delay(200); 
  myservo.attach(A3); //servo on digital pin 13 //servo
  myservo.write(0);
 
  tmrpcm.speakerPin = 9; 
  int buzzer = A2;
 
  

   if (!SD.begin(SD_ChipSelectPin)) {          
    return;                              
  }  
  
  }

void loop()
{
    char key = keypad.getKey();
    keypad.getKey();
    keypad.addEventListener(keypadEvent); //add an event listener for this keypad

    if (key == '#')
    {
      closeDoor();
    }

  
}
  


void keypadEvent(KeypadEvent eKey){

  switch (keypad.getState()){
  case PRESSED:
  playsound();
  Serial.print("Pressed: ");
  Serial.println(eKey);
  
  
  delay(10);
  
  switch (eKey){
    case '*': checkPassword(); delay(1); break;
    
    case '#': password.reset() ; delay(1); break;
    
    default: password.append(eKey); delay(1);
}
}

}

void checkPassword()
{

  
if (password.evaluate())
{  //if password is right open
    
    Serial.println("Password correct!!! Door open");
    delay(500); 
    openDoor();
    
    
}

else
{
    password.reset();
    Serial.println("Wrong Password !!! Please enter again"); //if passwords wrong keep locked
   
}


}

void openDoor()
{
   
    myservo.write(90); //deg
    delay(500);
    speaker();
}
void closeDoor()
{
   myservo.write(0);
   delay(500);
   
}


void playsound() 
{
  tone(A2, 100);
  delay(200);
  noTone(A2);
}


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

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

@peterjin, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Can you please edit your post, select all code and click the </>; next save your post. This will prevent the forum software from mangling your code and makes it easier to read and copy. Thanks.

Thanks. Edited.

1 Like

Without even looking at your code logic, those delay()s are going to slow everything down as far as responsiveness.

Look in to simple state machines to contain your operating states (or phases), then implement each of those using millis() timing, then finally stitch it all together.

It probably won’t be much longer or shorter than you’re siting decide, but it will be a lot more reliable and mai trainable.

Did you means shorten the delay time to operate the servo? It can solve the problem that I face? Because after the speaker output the sound, I cant be able to input another key to control the servo.