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?