Speaker and Servo Motor

Hi so I am an intermediate Arduino user and coder. I currently am building a project that will open a door with a servo and when the door is open it will play music. Currently I can move the servo with ease with the Servo.h library through serial connection but when I play music through the TMRpcm.h library I can no longer move the servo

The full code is extensive but this is a simplified version:

if (Serial.available() > 0 )
{
   Incoming_value = Serial.read();

   if (Incoming_value == 'A')
   {
      ser.write(125);
   }
   else if (Incoming_value == 'B')
   {
     tmrpcm.setVolume(3); 
     tmrpcm.play("Doorbell.wav");
     delay(5000);
     tmrpcm.stopPlayback();
     //tmrpcm.disable();
   }
   else if (Incoming_value == 'C')
   {
      ser.write(0);
   }
}

This is the simplified code that I have used to debug. I can send A and C and it will move back and forth. When I send B for the bell sound it will play it but when I try and move the servo again it will not do anything (Unless I reset the program). I can still play the bell over and over again however.

I have tried both tmrpcm.stopPlayback(); and tmrpcm.disable(); or neither and it does the same thing for the servo motor sake where it will stop moving

Is there something going on in the background with the TMRpcm.h library that I am probably not aware of that would be interfering with the servo library? or any other suggestions that I am overlooking

Without knowing all the details, my guess would be that the TMR library and the servo library you are using both mess with the same timer 1.

You'll find this link helpful:
https://create.arduino.cc/projecthub/ashraf_minhaj/how-to-use-servotimer2-library-simple-explain-servo-sweep-512fd9

this blocks thread execution for 5 seconds, the processor stops there and resumes after 5 seconds from where it halted

Thank you that really helped. I can now run them both. Now the only problem I am having is the audio is now slowed down when playing songs. I have tried changing the frequency of the wav files multiple times and doing a few other things but it does not seem to play back at the speed it was when I downloaded it.

note: Audio was not a problem before. I could run the music and it would play back correctly

I do not this this was the issue. I am aware what the delay does. Thank you anyways

Hi,
Glad to see it helped.

Does the audio play slowly, meaning the frequency of the individual notes is lower, only after running the servo?

I don't know enough about audio files, but I suspect it is the data rate. I found this on the TMR wiki:

"This library can be very processor intensive, and code execution during playback will be slower than normal
Processing load can be reduced by using lower quality sounds encoded at a lower sample rate (8khz minimum)"

I believe it was because I was using the wrong file. I fixed the mistake. Thank you for the help