Hello! I'm cutting my teeth on what I thought would be a straight forward first Arduino project. A D1 Mini and DFplayer running the LEDs and sound effects for a 3d printed Stargate.
I've been using preexisting code and a schematic from a guide provided by the creator. I'm using the Arduino IDE, I've followed guides and installed the CH341 driver, I've added all the appropriate libraries required by the sketch, I installed the ESP8266 library and selected what I think is the correct board (there are 6 variants of D1 listed).
I've used example sketches to verify that certain components are working. (Blink and a button test).
I think the problem lies with the DFplayer. I've tried two of them and have got no life out of them. I've triple checked continuity on all connectors, it's getting power (5v) and according to the code, I have the RX and TX connections correct.
I just don't know if I'm missing something obvious to more experienced users.
I bought the exact parts they said they used. Maybe the DFplayer is a clone (got it on amazon), but there were no reviews saying they were bad.
I'll attach the schematic and code.
Really appreciate any insight.
Here's the code.
#include <Button.h>
#include <FastLED.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/D5, /*tx =*/D6);
#define FPSerial softSerial
#define NUM_LEDS 50
#define DATA_PIN D7
#define NUM_LEDS2 47
#define DATA_PIN2 D8
#define BUTTON_PIN D2
//CRGB leds[NUM_LEDS];
CRGBArray<NUM_LEDS> leds;
CRGBSet chevron05(leds(5, 7));
CRGBSet chevron06(leds(10, 12));
CRGBSet chevron07(leds(15, 17));
CRGBSet chevron08(leds(20, 22));
CRGBSet lockingChevron(leds(25, 27));
CRGBSet chevron01(leds(30, 32));
CRGBSet chevron02(leds(35, 37));
CRGBSet chevron03(leds(40, 42));
CRGBSet chevron04(leds(45, 47));
CRGBArray<NUM_LEDS2> leds2;
Button button(BUTTON_PIN);
DFRobotDFPlayerMini myDFPlayer;
int startMillis = 0;
int lockChevronMillis = 0;
bool lockChevronOn = false;
int lockChevronFade = 0;
int lockedChevronFade = 0;
bool dialing = false;
bool wormholeActive = false;
int wormholeStartMillis = 0;
int wormholeBrightness = 0;
bool chevronLocked = false;
bool finalLock = false;
int buttonPressCount = 0;
// {Active, Brightness, FadeDirection, Color}
int wormholeArray[NUM_LEDS2][4] = {{0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}, {0, 255, 1, 0}};
void setup()
{
FPSerial.begin(9600);
Serial.begin(115200);
if (!myDFPlayer.begin(FPSerial, true, false)) { //Use serial to communicate with mp3.
Serial.println(F("Unable to begin:"));
while (true) {
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
button.begin();
FastLED.addLeds<WS2812B, DATA_PIN, BGR>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, DATA_PIN2, BGR>(leds2, NUM_LEDS2);
//set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
FastLED.setBrightness(240); // out of 255
//myDFPlayer.enableLoop();
}
void loop()
{
if (button.pressed()) {
buttonPressCount++;
if (buttonPressCount > 2) {
buttonPressCount = 0;
}
FastLED.clear();
FastLED.show();
// Stop playing MP3
myDFPlayer.stop();
startMillis = millis();
wormholeActive = false;
for (int i = 0; i <= NUM_LEDS2; i++) {
wormholeArray[i][0] = 0;
wormholeArray[i][1] = 255;
wormholeArray[i][2] = 1;
wormholeArray[i][3] = 0;
}
if (buttonPressCount == 1) {
myDFPlayer.stop();
dialing = true;
wormholeActive = false;
myDFPlayer.volume(19);
myDFPlayer.play(1);
}
else if (buttonPressCount == 2) {
myDFPlayer.stop();
dialing = false;
wormholeActive = true;
//myDFPlayer.volume(10);
//myDFPlayer.loop(2);
wormholeDemo();
}
else {
wormholeActive = false;
myDFPlayer.stop();
FastLED.clear();
}
}
if (dialing) {
dialingSequence();
}
if (wormholeActive) {
wormholeSequence();
}
FastLED.show();
}
void wormholeDemo() {
//wormholeStartMillis = millis();
if (wormholeActive) {
chevron01 = CHSV(19, 255, 255);
chevron02 = CHSV(19, 255, 255);
chevron03 = CHSV(19, 255, 255);
chevron04 = CHSV(19, 255, 255);
chevron05 = CHSV(19, 255, 255);
chevron06 = CHSV(19, 255, 255);
chevron07 = CHSV(19, 255, 255);
chevron08 = CHSV(19, 255, 255);
lockingChevron = CHSV(19, 255, 255);
}
}
void wormholeSequence() {
long wormholeDuration = millis() - wormholeStartMillis;
// Bright white, then puddle
if (wormholeDuration < 1700) {
wormholeBrightness++;
if (wormholeBrightness > 210)
{
wormholeBrightness = 210;
}
for (int i = 0; i < NUM_LEDS2; i++) {
leds2[i] = CRGB(wormholeBrightness, wormholeBrightness, wormholeBrightness);
}
}
else {
EVERY_N_MILLISECONDS(10) {
wormholeBrightness = 0;
int randomLED = random8(0, NUM_LEDS2);
int randomColor = random8(0, 3);
if (randomColor == 0) {
randomColor = 100; // white?
}
else if (randomColor == 1) {
randomColor = 130; // aqua
}
else {
randomColor = 160; // blue
}
if (wormholeArray[randomLED][0] == 0) {
wormholeArray[randomLED][0] = 1;
wormholeArray[randomLED][3] = randomColor;
}
}
for (int i = 0; i <= NUM_LEDS2; i++) {
// If the LED is active, start or continue the fade in/out sequence.
if (wormholeArray[i][0] == 1) {
// Increment the brightness either up or down
wormholeArray[i][1] = wormholeArray[i][1] + (1 * wormholeArray[i][2]);
// Once full brightness reached, reverse direction.
if (wormholeArray[i][1] >= 255) {
wormholeArray[i][1] = 255;
wormholeArray[i][2] = -wormholeArray[i][2];
}
// Once brightness fades back to zero, reset LED to inactive.
if (wormholeArray[i][1] <= 0) {
wormholeArray[i][0] = 0;
wormholeArray[i][1] = 0;
wormholeArray[i][2] = 1;
}
// Set each LED to blue, full saturation, brightness from loop.
if (wormholeArray[i][3] == 100) {
leds2[i] = CHSV(wormholeArray[i][3], 0, wormholeArray[i][1]);
}
else {
leds2[i] = CHSV(wormholeArray[i][3], 255, wormholeArray[i][1]);
}
}
}
}
}
void dialingSequence() {
// How long dialing sequence has been running since button press
long dialDuration = millis() - startMillis;
if (dialDuration > 4998 && dialDuration < 5003) {
lockedChevronFade = 0;
chevronLocked = false;
setLockingChevronLEDs(false);
}
if (dialDuration > 11998 && dialDuration < 12003) {
setLockingChevronLEDs(false);
}
if (dialDuration > 18998 && dialDuration < 19003) {
setLockingChevronLEDs(false);
}
if (dialDuration > 25998 && dialDuration < 26003) {
setLockingChevronLEDs(false);
}
if (dialDuration > 32998 && dialDuration < 33003) {
setLockingChevronLEDs(false);
}
if (dialDuration > 39998 && dialDuration < 40003) {
setLockingChevronLEDs(false);
}
if (dialDuration > 46998 && dialDuration < 47003) {
setLockingChevronLEDs(true);
}
if (lockChevronOn) {
lockingChevronLEDs();
}
if (dialDuration > 6198 && dialDuration < 7203) {
lockedChevronLEDs(chevron01);
}
if (dialDuration > 10000 && dialDuration < 10005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 13198 && dialDuration < 14203) {
lockedChevronLEDs(chevron02);
}
if (dialDuration > 20000 && dialDuration < 20005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 20198 && dialDuration < 21203) {
lockedChevronLEDs(chevron03);
}
if (dialDuration > 27000 && dialDuration < 27005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 27198 && dialDuration < 28203) {
lockedChevronLEDs(chevron06);
}
if (dialDuration > 33000 && dialDuration < 33005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 34198 && dialDuration < 35203) {
lockedChevronLEDs(chevron07);
}
if (dialDuration > 40000 && dialDuration < 40005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 41198 && dialDuration < 42203) {
lockedChevronLEDs(chevron08);
}
if (dialDuration > 44000 && dialDuration < 44005) {
lockedChevronFade = 0;
chevronLocked = false;
}
if (dialDuration > 48200 && dialDuration < 49205) {
lockedChevronLEDs(chevron04);
lockedChevronLEDs(chevron05);
}
if (dialDuration > 50400 && dialDuration < 50405) {
wormholeActive = true;
wormholeStartMillis = millis();
}
if (dialDuration > 66000) {
wormholeActive = false;
lockChevronOn = false;
fadeToBlackBy(leds, NUM_LEDS, 10);
fadeToBlackBy(leds2, NUM_LEDS2, 10);
}
if (dialDuration > 67000) {
dialing = false;
}
}
// Reset variables for the next locking chevron run
void setLockingChevronLEDs(bool finalCall) {
lockChevronOn = true;
lockChevronMillis = millis();
lockChevronFade = 0;
finalLock = finalCall;
}
// Sets the locking chevron LEDs on, slow fade up and down
void lockingChevronLEDs() {
// 2 seconds for top chevron to fade in, stay on, then fade out (.5, 1, .5 seconds)
int lockingChevronDuration = millis() - lockChevronMillis;
if (lockingChevronDuration < 500) {
lockChevronFade = lockChevronFade + 2;
}
if (lockingChevronDuration > 1500 && !finalLock) {
lockChevronFade = lockChevronFade - 2;
}
if (lockChevronFade > 255) {
lockChevronFade = 255;
}
if (lockChevronFade < 0) {
lockChevronFade = 0;
}
lockingChevron = CHSV(19, 255, lockChevronFade);
if (lockingChevronDuration > 2000 && !finalLock) {
lockChevronOn = false;
lockingChevron = CHSV(19, 255, 0);
}
}
// Sets locked chevron LEDs on after slow fade up
void lockedChevronLEDs(CRGBSet chevronNum) {
lockedChevronFade = lockedChevronFade + 2;
if (lockedChevronFade > 255) {
chevronNum = CHSV(19, 255, 255);
chevronLocked = true;
}
if (!chevronLocked) {
chevronNum = CHSV(19, 255, lockedChevronFade);
}
}


