My code works in uno but not in mega

Hello.
I ran the following code in uno. There is nothing wrong. That is, it runs all mp3 files between 0001 and 0022 in order. Then it starts from the beginning again.

But I couldn't run it with mega. skips the first mp3 file. Then it plays mp3 files 2 and 3. Then he doesn't show any reaction.

can you help me.

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

static const uint8_t PIN_MP3_TX = 2; // DFPlayer modülünde RX  pinine
static const uint8_t PIN_MP3_RX = 3; // DFPlayer modülünde TX pinine
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Oynatıcı objemizi Player adında oluşturuyoruz.
DFRobotDFPlayerMini player;

void setup() 
{
  // Seri haberleşmeyi başlatıyoruz
  Serial.begin(9600);
  softwareSerial.begin(9600);
  if (player.begin(softwareSerial)) 
{
   Serial.println("OK");
    player.volume(25);
    player.play(1);
  }
else 
{
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}
void loop() 
{
  delay(4000);
  player.next();  //Play next mp3 every 4 second.
}

Welcome to the forum

A question for you. why are you using SoftwareSerial on a Mega when it has 4 hardware UARTS ?

2 Likes

You can't use pins 2 & 3 on Mega for SoftwareSerial. See the "Limitations of the library" in SoftwareSerial readme:
https://docs.arduino.cc/learn/built-in-libraries/software-serial/

2 Likes

Does the assignment require you to use SoftwareSerial ?

That makes sense on a Uno but not on a Mega

Why not ?

How are the external devices connected to the Mega ? I assume that they both use a serial interface. If so, then how are they implemented ?

It is not the logic that I was asking about.

Can you please confirm that what you want to do is to use an app on your 'phone to control the MP3 player connected to the Arduino Mega ?

OK so far

The Mega has 4 hardware serial ports each with their own Tx and Rx pins. Connect the HC-05 to pins 18 (Tx pin) and 19 (Rx pin), remembering to cross connect Rx to Tx and vice versa. You can now use Serial1 to communicate with the HC-05

Write a small sketch to test this. The sketch should test whether data is available() on Serial1 and, if so, write it to Serial. The data should be visible in the Serial monitor

Get this working before adding anything else

SoftwareSerial bluetooth(12, 13); // RX, TX pins
SoftwareSerial mp3(10, 11); // RX, TX pins

How many more times do you need to be told that you cannot use two instances of SoftwareSerial in the same sketch and what's more, if you are using a Mega then you don't need to use SofttwareSerial at all

1 Like

How should I code?

It's not just the coding. The physical connections need to be right too

Start by following the advice in post #17 and use Seria1 for the HC05. To use Serial1 you need to use Serial1.begin() with the required baud rate then Serial1.available() and Serial1.read()

You can put the MP3 player on Serial2 using pins 16 and 17 then use Serial2.begin(), Serial2.print() etc. etc

I really would advise getting the HC-05 on Serial1 working first then adding the MP3 player

Your first objective should be to make the HC-05 work at all.

Only then can you move on to parsing what is received

I made the fix and it worked. bluetooth

What exactly have you got working ?

I suggest that you post your working code

I moved on to other steps.
I sent word via bluetooth and the command worked. But it doesn't work when sent from PC. I skipped this part for now.
I installed sg90 and adjusted its settings.
but this time it gave a new error. When I add the servo library, the dsplayer library does not recognize the player command.
So I removed the servo. I skipped this step for now.
I connected one ultrasonic sensor. Now I can send word commands via Bluetooth and different mp3s work depending on the obstacle distance.

Can I get help with both of my problems?

I will share the whole code when everything works. It may be confusing because it is very long and is only partially working at the moment.

The problem could be in line 23 of your code or maybe in line 78, but as I can't see it I can't be sure

Do you see what I am getting at ?

1 Like

Hello.

serial > mega to pc
serial1 > mega to bluetooth
serial2 > mega to mp3 player.

I have a problem with this code.

read in void loop section if (Serial.available())

if (Serial1.available())

If I write as:

char c = Serial.read();

does not accept. It says c must be int.

while (Serial1.available()) {
  delay(10);
  char c = Serial1.read();
  if (c == '#')

If I write as

while (Serial1.available()) {

I can not add. That is, I want to send a command to Mega from both the computer and Bluetooth and send information about the command to each other. But I could not.

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>
Servo sg90;


String voice;
DFRobotDFPlayerMini player;

#define TRIG_PIN_BACK 45  //arka ultrasonic trig
#define ECHO_PIN_BACK 44  //arka ultrasonic trig

unsigned long donusbaslama = 0;
unsigned long donmesuresi = 1000;
int engelmesafesi = 30;

void setup() {
  pinMode(22, OUTPUT);  //Set Pin 13 as Output (Connected to LED)
  Serial.begin(9600);   //Tx0 and Rx0  //Set Baud Rate to 9600 for Serial Communication Tx0 and Rx0
  delay(100);
  Serial1.begin(9600);
  delay(100);
  Serial2.begin(9600);  // DFPlayer mini ile arduino arasındaki haberleşmeyi başlatıyoruz.
  delay(100);

  //u.sensor pin mod durumu
  pinMode(TRIG_PIN_BACK, OUTPUT);
  pinMode(ECHO_PIN_BACK, INPUT);

  if (player.begin(Serial2))  //
  {
    player.volume(15);  // Ses seviyesini belirliyoruz (0 to 30).
  } else {
    delay(100);
  }
}

void loop() {
  while (Serial1.available()) {
    delay(10);                //Delay added to make thing stable
    char c = Serial1.read();  //Conduct a serial read
    if (c == '#') {
      break;
    }
    voice += c;  //Shorthand for voice = voice + c
  }
  if (voice.length() > 0) {
    Serial.println(voice);
    delay(10);

    if (voice == "*ali" || voice == "ali" || voice == "Ali")  //amr_voice ile göndereceksen *ali diye kodla ali diye oku seri porta ali geliyor. bluetooth terminal ile yazıp göndereceksen *ali yazarsan buraya *ali gelir

    {
      forward_car();
    }
    voice = "";  //Reset the variable after initiating
  }
}

void forward_car()  //
{
  int distance = distanceToBackObstacle();
  if (distance >= engelmesafesi) {
    digitalWrite(22, HIGH);  //Turn ON LED
    Serial.println("ali");
    Serial.println("LED ON");
    Serial1.println("ali");
    Serial1.println("LED ON");
    player.play(1);  // 0001.mp3 play
  } else {
    digitalWrite(22, LOW);  //Turn ON LED
    Serial1.println("LED OFF");
    player.play(2);  // 0001.mp3 play
  }
}

int distanceToBackObstacle() {
  digitalWrite(TRIG_PIN_BACK, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN_BACK, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN_BACK, LOW);
  long duration = pulseIn(ECHO_PIN_BACK, HIGH);
  int distance = duration * 0.034 / 2;
  return distance;
  delay(100);
}

Please post the full error message

It seems like your code is designed to play MP3 files in a loop using a DFPlayer Mini module. The issue you're facing, where the first MP3 file is skipped when using the Mega, might be due to timing or resource constraints.

One potential reason could be the timing of initialization. The Mega has multiple hardware serial ports, so you might want to try using one of those instead of SoftwareSerial. This could potentially improve the reliability of communication with the DFPlayer Mini.
Here's how you can modify your code to use HardwareSerial on the Mega:

#include <Arduino.h>
#include "DFRobotDFPlayerMini.h"

// Define the hardware serial port you want to use
#define DFPLAYER_SERIAL Serial1

// Oynatıcı objemizi Player adında oluşturuyoruz.
DFRobotDFPlayerMini player;

void setup() 
{
  // Seri haberleşmeyi başlatıyoruz
  Serial.begin(9600);
  DFPLAYER_SERIAL.begin(9600);
  if (player.begin(DFPLAYER_SERIAL)) 
  {
    Serial.println("OK");
    player.volume(25);
    player.play(1);
  }
  else 
  {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() 
{
  delay(4000);
  player.next();  //Play next mp3 every 4 second.
}

In this modified code:

  • DFPLAYER_SERIAL is defined to specify which hardware serial port to use (e.g., Serial1).
  • DFPLAYER_SERIAL.begin(9600) initializes the serial communication with the DFPlayer Mini.
  • player.begin(DFPLAYER_SERIAL) initializes the DFPlayer Mini using the specified hardware serial port.

Using hardware serial might help in ensuring reliable communication, which could potentially resolve the issue of the first MP3 file being skipped. Give it a try and see if it improves the behavior on the Mega.