Hello, this is my first time working with arduinos for a school project. I'm not experienced with it at all and neither am I with coding. What I'm trying to make is a dart shooter of sorts that plays a sound through an mp3 module and speaker while also having led light up whenever the microphone picks up the sound of someone blowing on it.
The materials I'm using are:
- DFRobot DFPlayer Mini mp3 module
- 3W 4ohm speaker
- NeoPixel Ring 16 x WS2812 5050 RGB LED
- MAX 9814 microphone
Currently I'm testing it on an UNO right now but I'm planning on moving it to a NANO later. My problem is that at some point my loop() stops working and I don't know why. It works fine if I only make it play a sound or make it light up, but whenever I try to do both, after blowing into the mic a few times the loop stops working and my led are stuck on.
I've noticed that whenever the mp3 module plays a sound, it momentarily stops the reading of the microphone. I don't know if that's what's causing an issue.
I also have the 5v pin connected to both the mp3 module and led. Is the shared usage a problem?
I tried making a schematic to my best abilities. I hope it is readable.
Mp3 module datasheet
WS2812B led datasheet
Here is my code
int micVolume = 0;
#include <FastLED.h>
#define NUM_LEDS 16
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
//
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
//
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Using a soft serial port
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/10, /*tx =*/11);
#define FPSerial softSerial
#else
#define FPSerial Serial1
#endif
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
FastLED.clear(true);
//
#if (defined ESP32)
FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
FPSerial.begin(9600);
#endif
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) { //Use serial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); //Set volume value. From 0 to 30
}
void loop() {
serialPlotter(0, 700);
// Records mic volume
micVolume = (analogRead(0));
Serial.println(micVolume);
delay(5);
if(micVolume > 500)
{
fill_solid( leds, NUM_LEDS, CRGB(200,0,0));
FastLED.show();
} else {
FastLED.clear(true);
}
int currentMP3 = int(random(1, 7));
if(micVolume > 500)
{
myDFPlayer.play(currentMP3);
}
}
void serialPlotter(int min, int max)
{
// Setting a limit so the serial plotter becomes readable
Serial.print(min);
Serial.print(" ");
Serial.print(max);
Serial.print(" ");
}
