element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Introducing DFPlayer module to working project breaks code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 13 replies
  • Subscribers 385 subscribers
  • Views 2555 views
  • Users 0 members are here
  • dfplayer
  • arduino mega 2560
  • arduino_code
Related

Introducing DFPlayer module to working project breaks code

colporteur
colporteur over 2 years ago

I am trying to add sound to the fireworks light project I used for a Project 14 Challenge. The project uses an Arduino Mega 2560 knockoff microcontroller.

I’ve added a DFPlayer Mini hardware and plan to use the DFRobotDFPlayerMini library. I have used this hardware and library combination before with some success.

When this instruction is called

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin. Check connection and SD card, or reset the Arduino."));
    while (true)
      ;
  }

I get one string of lights flashing and nothing else works.

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

//define array of output pins for ease of setup
const byte aryPins[] = { 21, 22, 23, 24, 25, 26, 27,
                         28, 29, 30, 31, 32, 33, 34, 35, 36,
                         37, 38, 39, 40, 41, 42, 43, 44, 45,
                         46, 47, 48, 49, 50, 51, 52, 53 };

RGBLed b1r1LED(53, 51, 49, RGBLed::COMMON_CATHODE);  //assign three pins to each RGBLED
RGBLed b1r2LED(47, 45, 43, RGBLed::COMMON_CATHODE);
RGBLed b1r3LED(41, 39, 37, RGBLed::COMMON_CATHODE);
RGBLed b2r1LED(35, 33, 31, RGBLed::COMMON_CATHODE);
RGBLed b2r2LED(29, 27, 25, RGBLed::COMMON_CATHODE);
RGBLed b2r3LED(23, 24, 22, RGBLed::COMMON_CATHODE);
RGBLed b3r1LED(52, 50, 48, RGBLed::COMMON_CATHODE);
RGBLed b3r2LED(46, 44, 42, RGBLed::COMMON_CATHODE);
RGBLed b3r3LED(40, 38, 36, RGBLed::COMMON_CATHODE);
RGBLed sparklerLED(30, 32, 34, RGBLed::COMMON_CATHODE);
/*
const byte sparkle_LED1 = 30;//assign pins to each single LED
const byte sparkle_LED2 = 32;
const byte sparkle_LED3 = 34;
*/

const byte ButtonPin = 28;  //assign pin to input button to start fireworks
const byte LEDStrip1 = 19;  //assign pin to output trigger of sequential LED's for burst1
const byte LEDStrip2 = 20;  //assign pin to output trigger of sequential LED's for burst2

const byte INITIAL_VALUE = LOW;  //The initial state of all Outputs. Can be LOW or HIGH.


//setup DF Player
int rxPin = 10;    // DFplayer RX to Arduino pin 10
int txPin = 11;    // DFplayer TX toArduinopin 11
int busyPin = 12;  // DFplayer BUSY connected to pin 12

SoftwareSerial mySoftwareSerial(rxPin, txPin);
DFRobotDFPlayerMini myDFPlayer;


//define array of RGB colours
int* getRandomRGBColor() {
  static int(*r);  // pointer variable

  switch (rand() % 7) {
    case 0: r = RGBLed::BLUE; break;
    case 1: r = RGBLed::GREEN; break;
    case 2: r = RGBLed::CYAN; break;
    case 3: r = RGBLed::RED; break;
    case 4: r = RGBLed::MAGENTA; break;
    case 5: r = RGBLed::YELLOW; break;
    case 6: r = RGBLed::WHITE; break;
  };
  return r;
};

void setup() {
  //Initiate Serial communication.
  Serial.begin(9600);
  Serial.begin(115200);
  Serial.println(F("Initializing DFPlayer..."));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin. Check connection and SD card, or reset the Arduino."));
    while (true)
      ;
  }
 /*
  Serial.println(F("DFPlayer Mini online."));
  myDFPlayer.setTimeOut(500);                   // Set serial communictaion time out 500ms
  myDFPlayer.volume(25);                        // Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_BASS);              // Set EQ to BASS (normal/pop/rock/jazz/classic/bass)
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);  // Set device we use SD as default
  myDFPlayer.enableDAC();                       // Enable On-chip DAC
*/

  for (int i = 0; i < sizeof aryPins; i++) {  //Go through all LED pins
    pinMode(aryPins[i], OUTPUT);              //Make them outputs
    digitalWrite(aryPins[i], INITIAL_VALUE);  //And set them to their initial value which is OFF
  }

  pinMode(ButtonPin, INPUT_PULLUP);  //Set the switch's pin to be an input with a pullup (default is HIGH)
                                     // pinMode(busyPin, INPUT);
}

void loop() {
  //  int soundFile = random(1, 4);  // multi soundfiles: 0001.mp3...0012.mp3
  //int soundFile = 1;              // single soundfile: 0001.mp3

  if (digitalRead(ButtonPin) == HIGH) {  //Test button pin to set off fireworks
    return HIGH;
  } else {
    randomSeed(analogRead(0));
    int burst_orderIdx = rand() % 3;  //random number 1-3 to determine what burst sequence will run

    if (burst_orderIdx == 1) {
      Serial.println(F("Start fireworks burst pattern 1,2,3"));
      //LED Strip 1 launch
      digitalWrite(LEDStrip1, HIGH);
      delay(250);
      digitalWrite(LEDStrip1, LOW);

      //Burst 1 On
      b1r1LED.setColor(getRandomRGBColor());
      delay(500);
      b1r2LED.setColor(getRandomRGBColor());
      delay(500);
      b1r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 1 Off
      b1r1LED.off();
      b1r2LED.off();
      b1r3LED.off();
      delay(3000);
      Serial.println(F("Burst 1 completed "));

      //LED Strip 2 launch
      digitalWrite(LEDStrip2, HIGH);
      delay(250);
      digitalWrite(LEDStrip2, LOW);

      //Burst 2 On
      b2r1LED.setColor(getRandomRGBColor());
      delay(500);
      b2r2LED.setColor(getRandomRGBColor());
      delay(500);
      b2r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 2 Off
      b2r1LED.off();
      b2r2LED.off();
      b2r3LED.off();
      delay(3000);
      Serial.println(F("Burst 2 completed "));

      //Burst 3 On
      b3r1LED.setColor(getRandomRGBColor());
      delay(500);
      b3r2LED.setColor(getRandomRGBColor());
      delay(500);
      b3r3LED.setColor(getRandomRGBColor());
      //delay(500);

      //Sparklers
      for (int n = 1; n <= 3; n++)  // Count to five
      {
        sparklerLED.fadeIn(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 0, 255, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 0, 255, 10, 100);
        delay(100);
      }

      //Burst 3 Off
      b3r1LED.off();
      b3r2LED.off();
      b3r3LED.off();
      delay(1000);
      Serial.println(F("Burst 3 completed "));
      Serial.println(F("End fireworks burst pattern 1,2,3"));
    } else if (burst_orderIdx == 2) {
      Serial.println(F("Start fireworks burst pattern 2,3,1"));

      //LED Strip 2 launch
      digitalWrite(LEDStrip2, HIGH);
      delay(250);
      digitalWrite(LEDStrip2, LOW);

      //Burst 2 On
      b2r1LED.setColor(getRandomRGBColor());
      delay(500);
      b2r2LED.setColor(getRandomRGBColor());
      delay(500);
      b2r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 2 Off
      b2r1LED.off();
      b2r2LED.off();
      b2r3LED.off();
      delay(3000);
      Serial.println(F("Burst 2 completed "));

      //Burst 3 On
      b3r1LED.setColor(getRandomRGBColor());
      delay(500);
      b3r2LED.setColor(getRandomRGBColor());
      delay(500);
      b3r3LED.setColor(getRandomRGBColor());
      //delay(500);

      //Sparklers
      for (int n = 1; n <= 3; n++)  // Count to five
      {
        sparklerLED.fadeIn(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 0, 255, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 0, 255, 10, 100);
        delay(100);
      }

      //Burst 3 Off
      b3r1LED.off();
      b3r2LED.off();
      b3r3LED.off();
      delay(1000);
      Serial.println(F("Burst 3 completed "));

      //LED Strip 1 launch
      digitalWrite(LEDStrip1, HIGH);
      delay(250);
      digitalWrite(LEDStrip1, LOW);

      //Burst 1 On
      b1r1LED.setColor(getRandomRGBColor());
      delay(500);
      b1r2LED.setColor(getRandomRGBColor());
      delay(500);
      b1r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 1 Off
      b1r1LED.off();
      b1r2LED.off();
      b1r3LED.off();
      delay(3000);
      Serial.println(F("Burst 1 completed "));
      Serial.println(F("End fireworks burst pattern 2,3,1"));
    } else {
      Serial.println(F("Start fireworks burst pattern 3,1,2,"));

      //Burst 3 On
      b3r1LED.setColor(getRandomRGBColor());
      delay(500);
      b3r2LED.setColor(getRandomRGBColor());
      delay(500);
      b3r3LED.setColor(getRandomRGBColor());
      //delay(500);

      //Sparklers
      for (int n = 1; n <= 3; n++)  // Count to five
      {
        sparklerLED.fadeIn(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(255, 0, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 255, 0, 10, 100);
        delay(100);
        sparklerLED.fadeIn(0, 0, 255, 10, 100);
        delay(100);
        sparklerLED.fadeOut(0, 0, 255, 10, 100);
        delay(100);
      }

      //Burst 3 Off
      b3r1LED.off();
      b3r2LED.off();
      b3r3LED.off();
      delay(1000);
      Serial.println(F("Burst 3 completed "));

      //LED Strip 1 launch
      digitalWrite(LEDStrip1, HIGH);
      delay(250);
      digitalWrite(LEDStrip1, LOW);

      //Burst 1 On
      b1r1LED.setColor(getRandomRGBColor());
      delay(500);
      b1r2LED.setColor(getRandomRGBColor());
      delay(500);
      b1r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 1 Off
      b1r1LED.off();
      b1r2LED.off();
      b1r3LED.off();
      delay(3000);
      Serial.println(F("Burst 1 completed "));

      //LED Strip 2 launch
      digitalWrite(LEDStrip2, HIGH);
      delay(250);
      digitalWrite(LEDStrip2, LOW);

      //Burst 2 On
      b2r1LED.setColor(getRandomRGBColor());
      delay(500);
      b2r2LED.setColor(getRandomRGBColor());
      delay(500);
      b2r3LED.setColor(getRandomRGBColor());
      delay(1000);

      //Burst 2 Off
      b2r1LED.off();
      b2r2LED.off();
      b2r3LED.off();
      delay(3000);
      Serial.println(F("Burst 2 completed "));
      Serial.println(F("End fireworks burst pattern 3,1,2"));
    }
  }
}


I have used a process of elimination (i.e. adding /* & */) to isolate the code that is causing me problems.

I have used this same code snipit in another project. It worked but was not combined with something else.

I am a code resurrectionist not a programmer or coder. I usually cut and paste code parts to develop a working project. Minds more familiar with Arduino have helped me solve problems in the past when I make code changes without fully understanding the underlying infrastructure. 

I'm at the point of using another sound solution. This would be a pain-in-the-***. I would have to go back in and redesign the PCB. I made this attempt to add sound about six months ago without success. I settled for the light show only, promising myself I would go back and find a solution. When I revisited it recently I did find some coding errors that I made but nothing that contributed to the current problem I am having.

I've included the full code if that helps. am hoping someone could give me some insight. Why when this line of code stops everything?

  • Sign in to reply
  • Cancel

Top Replies

  • JWx
    JWx over 2 years ago +3
    does it print "Unable to begin. Check connection and SD card, or reset the Arduino" on serial console? Because it would be exactly what it is supposed to do - print error message then enter infinite…
  • colporteur
    colporteur over 2 years ago +2
    Problem(s) discovered here are the fixes. I can get the DFPlayer working with the Mega. After bench testing a DFPlayer to Mega configuration I used the knowledge to troubleshoot the fireworks setup…
  • Andrew J
    Andrew J over 2 years ago in reply to jc2048 +1
    Returning a value is an unnecessary piece of code in Arduino C because loop() is continuously called. Behind the scenes I suspect there is an auto-generated function main() that first calls setup(), then…
Parents
  • jc2048
    jc2048 over 2 years ago

    Line 94 says to return to whatever called loop(). Is that what you intended?

    Forget that. Loop must be repeatedly called. It's in the name isn't it. It just looks wrong on the page.

    • Cancel
    • Vote Up -1 Vote Down
    • Sign in to reply
    • Cancel
  • JWx
    JWx over 2 years ago in reply to jc2048

    it seems that loop() does nothing (returning early) until test pin goes low - but returning something from function with

    void loop()

    prototype probably generates warning

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • JWx
    JWx over 2 years ago in reply to jc2048

    it seems that loop() does nothing (returning early) until test pin goes low - but returning something from function with

    void loop()

    prototype probably generates warning

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • jc2048
    jc2048 over 2 years ago in reply to JWx

    What would it do if run? Would it walk the stack until it was out of space?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to jc2048

    Returning a value is an unnecessary piece of code in Arduino C because loop() is continuously called.  Behind the scenes I suspect there is an auto-generated function main() that first calls setup(), then calls loop() continuously, e.g. within a while(true) loop.  That main code will do nothing with a returned value so it will just be dropped (it may even be compiled out as part of optimisations) but I don’t know whether it generates a warning in the Arduino compiler - my gut feeling is it does but isn’t noticed by Colporteur’s as the default generated code around it works as desired.  Anyway for the purposes of Colporteur’s problem it’s a nice to know and worth pointing out and I wouldn’t think it would impact the stack at all.  If I inspected the generated assembly, I wouldn’t be surprised to see the code optimised out, but if not, the value’s address loaded into the accumulator before unwinding the stack and just ping back into the calling code which ignores it.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube