Trigger the DF player by sound module

Hi, I am wanting to create an Ardunio project in a school classroom where a DF player will make a loud sound when the class noise goes over a certain level (as a fun way to let them know if they are getting too loud). I have a UNO, a DF Player and a sound module, I have found the code to test the player and it works as expected but I have tried a few methods of using the sound module to trigger the player but as a total newbee I have had no success, is anyone willing to help me with this one?
Cheers

Yes, there are many forum members who will be pleased to help you. But we can't do that with no information to base or help on, so please read the forum guide in the sticky post near the top of most forum sections. It will tell you what you need to include in your post so that others will be able to help .

Thank you for your response, I had a look at many other questions in this forum and noted that they had less information than mine and had better responses than the usual RTFM type. Sorry to have bothered you. I will close my account and try somewhere else
Cheers

I suggest you try a forum for psychics. They have super powers to see into your mind and figure out what's causing your problem. Good luck with that.

Once you have figured out that they are all Charlatans, come back, read the guide and this forum will be glad to help.

1 Like

Don't go away mad. No matter what impression you got from reading many posts on these fora, it is quite typical for us to need more than ppl usually offer in post #1.

Good start. Post that code. I've never used one; your valid test code would be useful.

Did you find and test similar code for the sound sesnor? You could post that as well.

Have you done any other programming, like making an LED be on when you are pressing a button, or making an LED go on and off alternating with each button prsss, or little things like that?

Without making a book of it, it is hard to help not knowing where you are at.

BTW great idea. I had a commercial unit that did that, a little device I grabbed out of the discount bin back in the days of Radio Shack.

I think it makes a good goal for a noob.

What Arduino board do you have?

a7

Is Radio Shack still going in some countries? We had their shops in cities and larger towns everywhere in the UK for decades, but here they were called "Tandy". But they all closed down years ago. Their great rivals in the UK were, of course, Maplin Electronics. Now also long gone (except for a small on-line presence, in both cases).

I used this code to run the Sound module and the LED will activate when a sound is created

const int LED = 13;
const int Sensor = A0;

int level;
const int threshold = 100;

void setup() 
{
pinMode (LED, OUTPUT);
Serial.begin(9600);
}

void loop() { 
level = analogRead(Sensor);
if (level > threshold)
{
digitalWrite (LED, HIGH);
delay (1000);
digitalWrite (LED, LOW);
}
else
{
digitalWrite(LED, LOW);
}
}

I then use the following code to run the DF player and it plays my sound once which is as expected

/* AIRLab DFMiniplayer example
Based on the 'getstarted.ino'-example by [Angelo qiao](Angelo.qiao@dfrobot.com)
Get Library here: https://github.com/DFRobot/DFRobotDFPlayerMini/archive/1.0.3.zip */

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // TX, RX
DFRobotDFPlayerMini myDFPlayer;

void setup()
{
  mySoftwareSerial.begin(9600); // Serial connecting Arduino and MP3 module
  Serial.begin(115200); // Serial connecting computer and Arduino
  
  Serial.println(F("Initializing DFPlayer Mini - May take 3~5 seconds..."));  
  if (!myDFPlayer.begin(mySoftwareSerial)) { 
    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(30);  //Initial volume - value must be from 0-30
  myDFPlayer.play(1);  //Play the first mp3 on SD-card
}

void loop()
{
  
}

I am hoping to combine the two and have the DF player run when a sound is loud enough to activate the sound module, I would also hope to use the busy output on the DF player to stop the sound the player is creating from reactivating the loop
Cheers

Thanks for reading the forum guide and using code tags.

It will be good to see the schematic and links to the components you are using.

Next post your attempt to combine the two sketches and we can help you figure out what you got wrong.

I found this...MP3 Clap and Motion sensors and if I could get this to work I would be more than happy as it ticks all the boxes...however after buying all the components and following the instructions to the letter I am unable to get this to work either.

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

MedianFilter ultrasonic_distance(7, 0);

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int files=0;

volatile unsigned long LastPulseTimeA;
int durationA;
int distance;
#define trigPinA 7
#define echoPinA 2
#define microphone 3
#define pinDFBusy 12
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
int status_microphone = 1; //normally HIGH, LOW when there is a signal / clap
boolean clap_status = false;
boolean status_lights = false;
unsigned long sonar_time = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
long randNumber;
int result;
int ultrasonic_flag = 0;

void setup()
{
   pinMode(microphone, INPUT);
   pinMode(trigPinA, OUTPUT);
   pinMode(echoPinA, INPUT);
   pinMode(13,OUTPUT);
   pinMode(pinDFBusy, INPUT);   // init Busy pin from DFPlayer (lo: file is playing / hi: no file playing)  
   randomSeed(analogRead(0)); //random
   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
   currentMillis = millis();
   previousMillis = currentMillis;
   attachInterrupt(digitalPinToInterrupt(echoPinA), EchoPinA_ISR, CHANGE);  // Pin 2 interrupt on any change

   mySoftwareSerial.begin(9600);
 
    Serial.println();
    Serial.println(F("DFRobot DFPlayer Mini Demo"));
    Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
    if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial 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);
      }
    Serial.println(F("DFPlayer Mini online."));
 
  myDFPlayer.setTimeOut(500); //Set serial communication time out 500ms
  myDFPlayer.volume(27);  //Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
//  myDFPlayer.EQ(DFPLAYER_EQ_POP);
//  myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
//  myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
//  myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
//  myDFPlayer.EQ(DFPLAYER_EQ_BASS);
 
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
 
  //----Mp3 control----
//  myDFPlayer.sleep();     //sleep
//  myDFPlayer.reset();     //Reset the module
  myDFPlayer.enableDAC();  //Enable On-chip DAC
//  myDFPlayer.disableDAC();  //Disable On-chip DAC
//  myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
 
  //----Read information----
  Serial.println(myDFPlayer.readState()); //read mp3 state
  Serial.println(myDFPlayer.readVolume()); //read current volume
  Serial.println(myDFPlayer.readEQ()); //read EQ setting
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
 // Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  //Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
}

void loop(){
  //delay(50);
  microphone_check();
  currentMillis = millis();
  // delay(29);                  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 
   if (currentMillis - previousMillis >= 50)
        {
          digitalWrite(trigPinA, LOW);
          delayMicroseconds(2);
          digitalWrite(trigPinA, HIGH);
          delayMicroseconds(10);
          digitalWrite(trigPinA, LOW);
          previousMillis = currentMillis; // remember the time
          Serial.print("Ping: ");
          Serial.print(LastPulseTimeA);
          Serial.print('\t');
          distance = LastPulseTimeA/2/29.1;
          ultrasonic_distance.in(distance);
          result = ultrasonic_distance.out();
          Serial.println(distance);
           Serial.print(result);
          Serial.println("cm");
         
           if (ultrasonic_flag == 0){
              if (result > 1 && result < 12 ){
                randNumber = random(1,5);
                Serial.println(randNumber);
                 //files = 3;
                    myDFPlayer.playLargeFolder(1, randNumber); //play specific mp3 in SD:/01/0004.mp3; Folder Name(1~10); File Name(1~1000)
                    Serial.println(myDFPlayer.readCurrentFileNumber());
                   // delay(10000);
                     do{
                     //  do nothing while busy
                       }
                     while (digitalRead(pinDFBusy) == LOW);
                     ultrasonic_flag = 1;
                  }
              if (result > 200 && result < 250 ){
                    myDFPlayer.playLargeFolder(2, 1); //play specific mp3 in SD:/02/0001.mp3; Folder Name(1~10); File Name(1~1000)
                    Serial.println(myDFPlayer.readCurrentFileNumber());
                     do{
                      //  do nothing while busy
                       }
                     while (digitalRead(pinDFBusy) == LOW);
                     ultrasonic_flag = 1;
                  }    
          }
          if (ultrasonic_flag > 0){
            ultrasonic_flag ++;
            Serial.println("ultrasonic_flag++");
          }
 
          if (ultrasonic_flag == 15){
            ultrasonic_flag = 0;
            Serial.println("ultrasonic_flag = 0");
          }      

      }
        if (clap_status == true){
              clap_status = false;
              randNumber = random(1,5);
              Serial.println(randNumber);
              myDFPlayer.playLargeFolder(1, randNumber); //play specific mp3 in SD:/01/0004.mp3; Folder Name(1~10); File Name(1~1000)
              Serial.println(myDFPlayer.readCurrentFileNumber());
            do{
               //Serial.println("low");
            }
              while (digitalRead(pinDFBusy) == LOW);
           
            //  delay(4000);    
        }
       
        if (myDFPlayer.available()) {
          printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
        }
}

void EchoPinA_ISR() {
    static unsigned long startTimeA;

    if (digitalRead(2)) // Gone HIGH
        startTimeA = micros();
    else  // Gone LOW
        LastPulseTimeA = micros() - startTimeA;
}
void microphone_check(){
   status_microphone = digitalRead(microphone);
   if (status_microphone == 0)
      {
            if (clap == 0)
            {
          Serial.println("clap = 0");
                  detection_range_start = detection_range = millis();
                  clap++;
                  }
            else if (clap > 0 && millis()-detection_range >= 100)
            {
             
          Serial.println(clap);
          Serial.println("clap > 0 && millis >50");
                  detection_range = millis();
                  clap++;
                  }
      }
    if (millis()-detection_range_start >= 350)
      {
            if (clap == 2)
            {
              clap_status = true;
              Serial.println("clap = success");
                  if (!status_lights)
                  {
                        status_lights = true;
                        digitalWrite(13, HIGH);
                        }
                  else if (status_lights)
                  {
                        status_lights = false;
                        digitalWrite(13, LOW);
                        }
            }
       clap = 0;
       }
}
void printDetail(uint8_t type, int value){
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerUSBInserted:
      Serial.println("USB Inserted!");
      break;
    case DFPlayerUSBRemoved:
      Serial.println("USB Removed!");
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
 
}

If anyone out there has any idea why this is not working I would be greatly appreciative
Cheers

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