playing audio with servo

Hi I want to add audio with servo controlled project , code for servo and audio (df player) is working separately but when I combing both circuit is not working, kindly have a look on code below and suggest the updated code

// Include the Servo library
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>

// Declare the Servo pin
int servoPin = 9;

SoftwareSerial mySerial(18, 19); // RX, TX
// Create a servo object
Servo Servo1;

void setup() {
// We need to attach the servo to the used pin number
Serial.begin (9600);
mySerial.begin (9600);
//mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
mp3_set_volume (40);
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
mp3_play (1);
delay (15000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(200000);
}v

The software serial library is using the same processor resource as the servo driver library.
One way round this is to control the servos with an external servo driver board like the one from Adafruit.
Another is to use an Arduino with an extra serial port, like the Mega or Micro.

currently I am using arduino mega. Is my code is correct? if yes then why its not working with arduino mega?

It is not working because you are using software serial. You have to use the Mega’s extra UARTs using Serial1 or Serial2 or Serial3, to talk to them in software and wire the appropriate pair of pins to your sound module.

ok, this is totally new for me…can you please share a example with me to solve this issue, this will be very helpful for me.

.can you please share a example with me to solve this issue

Do you mean do it for you?
I have told you what you have to do, if you don't understand then ask about what you don't understand.

We like to educate people here not do things for them.

So step 1 find out what pins the extra serial ports use on the Mega, ( Google is your friend ) identify what pins among these you are not using for your servos and wire them up to your player. You might need to swap some servo pins to free up the serial port pins.

No I not mean that.

I was trying to say i not get Mega's extra UARTs using Serial1 or Serial2 or Serial3 so asked for an example (reference link) so i can solve it for me.
kindly have a look on my code i am using arduino mega so i defined SoftwareSerial mySerial(18, 19); // RX, TX but its not working..sir plz help me.

Simple example

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial1.begin(9600);
}


// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  if (Serial1.available() > 0) {
    byte data1 = Serial1.read(); 
    Serial1.print("read in: ");  
    Serial1.write (data1);            
  }
}

Change 1 to 2 or 3 depending on which pins are used.
Serial1, Serial2, and Serial3 are typically used for uC to uC comms's, and will not show up on the Serial monitor unless an FTDI Basic/equivalent is connected to them to talk to the PC

Thank you so much sir, I don’t have much experience with electronics (i just programming).

I have update my code now servo is working fine but df player audio is not playing, here is my updated code

// Include the Servo library
#include <Servo.h>
//#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>

// Declare the Servo pin
int servoPin = 9;

SoftwareSerial mySerial(18, 19); // RX, TX
// Create a servo object
Servo Servo1;

void setup() {

Serial1.begin (9600);
Serial2.begin (9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
mp3_set_volume (40);
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);

mp3_play (1);
delay (15000);

// Make servo go to 180 degrees
Servo1.write(180);
delay(200000);
}

sir i tried and its working now...thanks a lot for your support :slight_smile:

Hello!
I have the same probleme with Arduino mega I Have 9 servo connected and a MP3 module on
TX1 RX1
all work BUT when a MP3 sample start my servos move abruptly for half a second and move correctly during the sample and move abruptly again at the end of the sample....
I Looking for response on web but I dont find a good explication ...
Please can you poste your solution .
I have been stuck for several days on this problem
Thank