Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noise is inserted into audio during communication (It's playing?) #20

Closed
rtek1000 opened this issue Nov 15, 2018 · 4 comments
Closed

Noise is inserted into audio during communication (It's playing?) #20

rtek1000 opened this issue Nov 15, 2018 · 4 comments

Comments

@rtek1000
Copy link

rtek1000 commented Nov 15, 2018

I would like to monitor the status every 1 second to see if it is playing.

I have a folder with files of small duration and others with long files, but some folders can not be played, so I can not use the continuous play commands, or play randomly, for the whole card.

I modified the example file, to randomly play some folders, but it works fine only if I use the busy pin.

I noticed that a noise appears in the audio, when the serial port is used, so I had to monitor the busy signal pin.

I've added series resistors for RX and TX, and the noise has decreased a lot, but sometimes the program reports errors such as "Time Out!", "Stack Wrong!",

How to monitor by software without causing a noise similar to the step of a wall clock in the audio?

/***************************************************
  DFPlayer - A Mini MP3 Player For Arduino
  https://www.dfrobot.com/product-1121.html

  This example shows the all the function of library for DFPlayer.

  Created 2016-12-07
  By Angelo qiao

  GNU Lesser General Public License.
  See http://www.gnu.org/licenses/ for details.
  All above must be included in any redistribution
****************************************************/

/***********Notice and Trouble shooting***************
  1.Connection and Diagram can be found here
  <https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299#Connection_Diagram>
  2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 ****************************************************/

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

//#define busyPin 12

SoftwareSerial mySoftwareSerial(5, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

bool isPlaying = true;

int lastValidFolder = 0;
const int maxFolder = 10; // (1~99)
int folderFiles[maxFolder];

void setup()
{
  delay(5000); // help in upload done with esp-link (OTA)
  //  pinMode(busyPin, INPUT); // optional

  randomSeed((analogRead(6) + analogRead(7)) / 2); //

  mySoftwareSerial.begin(9600);
  Serial.begin(9600);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  //Use softwareSerial to communicate with mp3.
  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);
  }
  Serial.println(F("DFPlayer Mini online."));

  myDFPlayer.setTimeOut(2000); //Set serial communication time out 500ms

  //----Set volume----
  myDFPlayer.volume(30); //Set volume value (0~30).
  // myDFPlayer.volumeUp(); //Volume Up
  // myDFPlayer.volumeDown(); //Volume Down

  //----Set different EQ----
  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);

  //----Set device we use SD as default----
  // myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
  // myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX);
  // myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP);
  // myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);

  //----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 (0~30)

  //----Mp3 play----
  // myDFPlayer.next(); //Play next mp3
  // delay(1000);
  // myDFPlayer.previous(); //Play previous mp3
  // delay(1000);
  // myDFPlayer.play(1); //Play the first mp3
  // delay(1000);
  // myDFPlayer.loop(1); //Loop the first mp3
  // delay(1000);
  // myDFPlayer.pause(); //pause the mp3
  // delay(1000);
  // myDFPlayer.start(); //start the mp3 from the pause
  // delay(1000);
  // myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(199); File Name(1255)
  // delay(1000);
  // myDFPlayer.enableLoopAll(); //loop all mp3 files.
  // delay(1000);
  // myDFPlayer.disableLoopAll(); //stop loop all mp3 files.
  // delay(1000);
  // myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(065535)
  // delay(1000);
  // myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(065535)
  // delay(1000);
  // myDFPlayer.stopAdvertise(); //stop advertise
  // delay(1000);
  // myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(110); File Name(11000)
  // delay(1000);
  // myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
  // delay(1000);
  // myDFPlayer.randomAll(); //Random play all the mp3.
  // delay(1000);
  // myDFPlayer.enableLoop(); //enable loop.
  // delay(1000);
  // myDFPlayer.disableLoop(); //disable loop.
  // delay(1000);

  //----Read imformation----
  Serial.print(F("State: "));
  Serial.println(myDFPlayer.readState()); //read mp3 state
  Serial.print(F("Volume (030): "));
  Serial.println(myDFPlayer.readVolume()); //read current volume
  Serial.print(F("Equalizer (Normal/Pop/Rock/Jazz/Classic/Base): "));
  printEqDetail(myDFPlayer.readEQ()); //read EQ setting
  Serial.print(F("File Counts: "));
  Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
  Serial.print(F("Current File Number of micro SD Card: "));
  Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
  for (int i = 1; i <= maxFolder; i++) { // 199
    folderFiles[i] = myDFPlayer.readFileCountsInFolder(i);
    Serial.print(F("File Counts In Folder "));
    Serial.print(i, DEC);
    Serial.print(F(": "));

    if (folderFiles[i] > 0) {
      lastValidFolder = i;
      Serial.print(folderFiles[i]); //read file counts in folder SD:/nn
      Serial.print(F(" [Valid]"));
    } else {
      Serial.print(F("Not found"));
    }

    Serial.println();
  }
}

static unsigned long timer;
static unsigned long timer2;
static unsigned long timer3;
unsigned long newMillis = 0;
bool newDots = true;
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;
int typeFb = 0;
int valueFb = 0;
int typeFb2 = 0;
int valueFb2 = 0;

void loop()
{
  newMillis = millis();

  if (myDFPlayer.available()) {
    if (cnt2 > 0) Serial.println();
    typeFb = myDFPlayer.readType();
    valueFb = myDFPlayer.read();
    printDetail(typeFb, valueFb); //Print the detail message from DFPlayer to handle different errors and states.
    if (myDFPlayer.available()) {
      typeFb2 = myDFPlayer.readType();
      valueFb2 = myDFPlayer.read();
      if ((typeFb2 != typeFb) & (valueFb2 != valueFb)) { // Is not repeated?
        printDetail(typeFb2, valueFb2); //Print the detail message from DFPlayer to handle different errors and states.
      }
    }
  } else if (((newMillis - timer3) > 1000) & (!isPlaying)) { // DFplayer busy pin state
    //  } else if (((newMillis - timer3) > 1000) & (digitalRead(busyPin))) { // DFplayer busy pin state
    timer3 = newMillis;
    playNewRandomFile();
  } else { // available = false & isPlaying = true
    if (newMillis - timer > 1000) {
      timer = newMillis;
      if (isPlaying) printDots(); // dot's playback
    } else if (newMillis - timer2 > 2000) { // playback finished
      timer2 = newMillis;
      isPlayingTest();
    }
  }
}

bool isPlayingTest() {
  //if (isPlaying == true) { // very long music? (noise)
  int state = myDFPlayer.readState();
  //    Serial.print("isPlayingTest: ");
  //    Serial.println(state, DEC);
  if ((state == 513) | (state == 529)) {
    isPlaying = true;
  } else if (state == 512) {
    isPlaying = false;
  }
  //}
  return isPlaying;
}

void printDots() {
  if (newDots) {
    newDots = false;
    cnt1 = 0;
    cnt2 = 1;
    cnt3 = 0;
  } else {
    if (cnt1 < 30) {
      cnt1++;
    } else {
      cnt1 = 1;
      Serial.println();
    }
    if (cnt3 < 9) {
      cnt3++;
      if (cnt3 == 5) {
        printTime();
      } else {
        Serial.print(F("."));
      }
    } else {
      cnt3 = 0;
      printTime();
    }
    cnt2++;
  }
}

void printTime() {
  int hours = cnt2 / 3600;
  int minutes = (cnt2 - (hours * 60)) / 60;
  int seconds = cnt2 - (minutes * 60) - (hours * 60);

  if (cnt2 < 60) {
    Serial.print(seconds, DEC);
    Serial.print(F("s"));
  } else if (cnt2 < 3600) {
    Serial.print(minutes, DEC);
    Serial.print(F("m:"));
    Serial.print(seconds, DEC);
    Serial.print(F("s"));
  } else {
    Serial.print(hours, DEC);
    Serial.print(F("h:"));
    Serial.print(minutes, DEC);
    Serial.print(F("m:"));
    Serial.print(seconds, DEC);
    Serial.print(F("s"));
  }
}

void playNewRandomFile() {
  if (lastValidFolder == 0) {
    Serial.println(F("Empty device"));
    return; // file not found
  }

  int folder = random(1, maxFolder);
  while (folderFiles[folder] < 0) { // have valid files?
    folder = random(1, maxFolder); // for playFolder: 99 max
  }

  // int folder = random(1, 15); // for playLargeFolder: 15 max
  int file = random(1, folderFiles[folder]); // for playFolder: 255 max
  // int file = random(1, 3000); // for playLargeFolder: 3000 max
  char buffer[30];
  if (cnt2 > 1) {
    Serial.print(F("Host - File playback time "));
    printTime();
    Serial.println();
  }
  Serial.print(F("Host - Play file "));
  sprintf(buffer, "%d from folder %d", file, folder);
  Serial.println(buffer);
  isPlaying = true;
  newDots = true; // reset dot's position
  //myDFPlayer.playLargeFolder(folder, file); // up to 3000 tracks and 15 folders
  myDFPlayer.playFolder(folder, file); //play specific mp3 in SD:/nn/nnn.mp3; Folder Name(199); File Name(1255)
}

void printEqDetail(int type) {
  switch (type) { // EQ" (0/1/2/3/4/5) Normal/Pop/Rock/Jazz/Classic/Base
    case 1:
      Serial.print(F("Pop"));
      break;
    case 2:
      Serial.print(F("Rock"));
      break;
    case 3:
      Serial.print(F("Jazz"));
      break;
    case 4:
      Serial.print(F("Classic"));
      break;
    case 5:
      Serial.print(F("Base"));
      break;
    default: // 0
      Serial.print(F("Normal"));
      break;
  }
  Serial.print(F("("));
  Serial.print(type, DEC);
  Serial.println(F(")"));
}

void printDetail(uint8_t type, int value) {
  //  if ((type == DFPlayerError) & (value == Advertise)) {
  //    //isPlaying = true;
  //  } else {
  //    //isPlaying = false;
  //    cnt2 = 0;
  //  }

  Serial.print(F("DFPlayer - "));

  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.println(F("Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("Error: "));
      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:
          Serial.print(F("Unknown, "));
          Serial.println(value, DEC);
          break;
      }
      break;
    default:
      Serial.print(F("Unknown type code, "));
      Serial.println(type, DEC);
      break;
  }
}

continuous random play

@rtek1000
Copy link
Author

When the "Time Out!" error appears, the song is stopped before it ends, and another is started,
(This occurs when the busy pin is not used)

Log result using the busy pin:

Host - Play file 6 from folder 3
DFPlayer - Play Finished!
Host - Play file 5 from folder 3
DFPlayer - Play Finished!
Host - Play file 63 from folder 1
DFPlayer - Play Finished!
Host - Play file 5 from folder 4
DFPlayer - Play Finished!
Host - Play file 4 from folder 3
DFPlayer - Stack Wrong!
DFPlayer - Play Finished!
Host - Play file 18 from folder 2
DFPlayer - Play Finished!
Host - Play file 21 from folder 6
DFPlayer - Play Finished!
DFPlayer - Play Finished!
Host - Play file 4 from folder 3
DFPlayer - Play Finished!
Host - Play file 17 from folder 5
DFPlayer - Play Finished!
Host - Play file 2 from folder 5
DFPlayer - Stack Wrong!
DFPlayer - Play Finished!
Host - Play file 11 from folder 4
DFPlayer - Play Finished!
Host - Play file 2 from folder 4
DFPlayer - Play Finished!
Host - Play file 2 from folder 4
DFPlayer - Play Finished!

Result without using busy pin:

Host - Play file 99 from folder 1
DFPlayer - Play Finished!
Host - Play file 98 from folder 1
DFPlayer - Play Finished!
Host - Play file 78 from folder 1
DFPlayer - Time Out!
Host - Play file 61 from folder 1
DFPlayer - Play Finished!
Host - Play file 80 from folder 1
DFPlayer - Play Finished!
Host - Play file 46 from folder 1
DFPlayer - Play Finished!
Host - Play file 8 from folder 1
DFPlayer - Time Out!
Host - Play file 84 from folder 1
DFPlayer - Play Finished!
Host - Play file 104 from folder 1
DFPlayer - Error: Cannot Find File
Host - Play file 17 from folder 1
DFPlayer - Time Out!
Host - Play file 88 from folder 1
DFPlayer - Play Finished!
Host - Play file 38 from folder 1
DFPlayer - Play Finished!
Host - Play file 105 from folder 1
DFPlayer - Time Out!
DFPlayer - Error: Cannot Find File
Host - Play file 5 from folder 1
DFPlayer - Play Finished!
DFPlayer - Play Finished!
Host - Play file 56 from folder 1
DFPlayer - Play Finished!
Host - Play file 69 from folder 1
DFPlayer - Play Finished!
Host - Play file 11 from folder 1
DFPlayer - Time Out!
Host - Play file 103 from folder 1
DFPlayer - Error: Cannot Find File
Host - Play file 58 from folder 1
DFPlayer - Play Finished!
Host - Play file 91 from folder 1
Host - File playback time 1s
Host - Play file 76 from folder 1
DFPlayer - Unknown type code, 11
Host - Play file 34 from folder 1
DFPlayer - Play Finished!
Host - Play file 19 from folder 1
DFPlayer - Play Finished!
Host - Play file 93 from folder 1
DFPlayer - Time Out!
Host - Play file 5 from folder 1
DFPlayer - Play Finished!
DFPlayer - Play Finished!

@drolsen
Copy link

drolsen commented Nov 17, 2018

I'd recommend adding a small 10uf capacitor on your vcc/grn lines going to the mini player to help pool up some dedicated mA for it. Strange noises can happen when your microcontroller(s) draw mA away from the mini player without one.

@rtek1000
Copy link
Author

Hi, thanks for the suggestion, but I already had a 10uF capacitor before and after the AMS1117-3.3 regulator. I think the problem was the lack of a 5V voltage drop from Arduino's TX to the DFPlayer's RX, it was using only a 1k series resistor. After placing a 2k resistor between the RX of the DFPlayer and the GND the noise seems to have disappeared.

@rtek1000
Copy link
Author

rtek1000 commented Nov 21, 2018

I managed to make the code more stable, I noticed that the myDFPlayer.readState() routine shows the value 512 for the stopped state. I no longer need to use the busy pin.

I updated the code, in case someone wants to try to run this.

This should generate a log on the serial (USB) port that looks like this:

DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
DFPlayer Mini online.
State: 512
Volume (030): 30
Equalizer (Normal/Pop/Rock/Jazz/Classic/Base): Normal(0)
File Counts: 438
Current File Number of micro SD Card: 0
File Counts In Folder 1: 111 [Valid]
File Counts In Folder 2: 54 [Valid]
File Counts In Folder 3: 12 [Valid]
File Counts In Folder 4: 20 [Valid]
File Counts In Folder 5: 59 [Valid]
File Counts In Folder 6: Not found
File Counts In Folder 7: Not found
File Counts In Folder 8: Not found
File Counts In Folder 9: Not found
File Counts In Folder 10: Not found
Host - Play file 4 from folder 3
....5s....10s....15s....20s....25s....30s
....35s....40s....45s....50s....55s....1m:0s
....1m:5s....1m:10s....1m:15s....1m:20s....1m:25s....1m:30s
....1m:35s....1m:40s....1m:45s....1m:50s....1m:55s....2m:0s
....2m:5s....2m:10s....2m:15s....2m:20s....2m:25s....2m:30s
....2m:35s....2m:40s....2m:45s....2m:50s...
DFPlayer - Play Finished!

DFPlayer - Play Finished!
.Host - File playback time 2m:55s
Host - Play file 23 from folder 1
.
DFPlayer - Play Finished!

DFPlayer - Play Finished!
Host - File playback time 2s
Host - Play file 23 from folder 5
....5s....10s....15s....20s....25s....30s
....35s....40s....45s....50s....55s....1m:0s
....1m:5s....1m:10s....1m:15s....1m:20s....1m:25s....1m:30s
....1m:35s....1m:40s....1m:45s....1m:50s....1m:55s....2m:0s
.
DFPlayer - Play Finished!

DFPlayer - Play Finished!
Host - File playback time 2m:2s
Host - Play file 63 from folder 1
.
DFPlayer - Play Finished!

DFPlayer - Play Finished!
Host - File playback time 2s
Host - Play file 5 from folder 3
....5s....10s....15s....20s....25s....30s
....35s....40s....45s....50s....55s....1m:0s
....1m:5s....1m:10s....1m:15s....1m:20s....1m:25s....1m:30s
....1m:35s....1m:40s....1m:45s....1m:50s....1m:55s....2m:0s
....2m:5s....2m:10s....2m:15s....2m:20s....2m:25s....2m:30s
....2m:35s....2m:40s....
DFPlayer - Play Finished!

DFPlayer - Play Finished!
2m:45sHost - File playback time 2m:46s
Host - Play file 2 from folder 3
....5s....10s....15s....20s....25s....30s
....35s....40s....45s....50s....55s....1m:0s
....1m:5s....1m:10s....1m:15s....1m:20s....1m:25s....1m:30s
....1m:35s....1m:40s....1m:45s....1m:50s....1m:55s....2m:0s
....2m:5s....2m:10s....

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants