Arduino UNO loop stops working after a while

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(" ");
}
1 Like

I can't help right now but that is a very pretty schematic! The only improvement would be to label the numbered pins on the blocks. All those signals have names like Vcc, GND, dIn and so forth.

But totally usable. Nice work.

And you put the code in code tags, very nice. I look forward to becoming motionless knfront of a bigger window I take a closer look.

a7

2 Likes

Your diagram shows the mic connected to A0, but the sketch is using 0. Pin 0 is not the same as Pin A0.

Serial uses pins 0 and 1, and in this case that is also a conflict with the mic.

1 Like

I hadn't noticed that as the mic seemed to pick up sound just fine. I changed it to A0 in the code but the problem still persists.

Thank you! I will keep that in mind and try to add it in my drawing.

Will draw too much current from the 5V output. Remove the Neopixels, get the rest working, and get a separate 5V power supply for the Neopixels.

For analogRead(), it is OK to use either. "0" is regarded as analog channel 0, and is treated exactly like "A0". That was not a wise design decision by Arduino.

I don't think FastLED and SoftwareSerial work together well, if at all. Interrupts are a problem.

Good to know.

Thank you

Well, a master of understatement, indeed. Even hardware serial has a hard time working together with FastLED. Outbound transmission is okay, but multicharacter inbound messages will suffer if the LED string is long enough and/or the baud rate is high enough.
Ironically, our usual advice to 'raise the baud rate' is exactly the wrong thing to do in this instance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.