#004 - Player Box

  • Resistor 330Ω x 1
  • Resistor 1KΩ x 2
  • Resistor 10kΩ x 4
  • Capacitor 10uF x 1
  • Capacitor 22pF x 2
  • ATmega328P x 1
  • DFPlayerMini x 1
  • LED x 1
  • 16mhz crystal x 1
  • USB type A socket x 1
  • Slide switch 2-position x 1
  • Battery storage case for 3 packs AA x 1
  • Goldpins

Code


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

#include "Button.h"
#include "Config.h"

Button prevButton(PREV_BUTTON_PIN);
Button startStopButton(START_STOP_BUTTON_PIN);
Button nextButton(NEXT_BUTTON_PIN);

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

boolean started = false;

int volumeValue = 0;

void setup() {

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

  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.volume(volumeValue);
  myDFPlayer.volume(15);

  delay(1000);
  started = true;
  myDFPlayer.play(1);
}

void loop() {
  delay(250);

  int readedVolumeValue = map(analogRead(VOLUME_PIN), 0, 1023, 0, 20);
  if (readedVolumeValue !=  volumeValue) {
    volumeValue = readedVolumeValue;
    myDFPlayer.volume(volumeValue);
    Serial.println(volumeValue);
  }


  if (prevButton.pressed()) {
    myDFPlayer.previous();
  }

  if (nextButton.pressed()) {
    myDFPlayer.next();
  }

  if (startStopButton.pressed()) {
    if (started == true) {
      myDFPlayer.pause();
      started = false;
    } else {
      myDFPlayer.start();
      started = true;
    }
  }
  if (myDFPlayer.available()) {
    nextAction(myDFPlayer.readType(), myDFPlayer.read());
  }
}

// https://github.com/DFRobot/DFRobotDFPlayerMini/blob/master/examples/FullFunction/FullFunction.ino
void nextAction(uint8_t type, int value) {
  switch (type) {
    case DFPlayerPlayFinished:
      myDFPlayer.next();
      break;
  }
}

Button.cpp


#include "Arduino.h"
#include "Button.h"

Button::Button(int pin) {
  _pin = pin;
  _lastState = LOW;
  pinMode(_pin, INPUT);
}

bool Button::pressed() {
  int state = digitalRead(_pin);
  if (state != _lastState) {
    _lastState = state;
    return state == HIGH;
  }
  return false;
}

Button.h


#ifndef BUTTON_H
#define BUTTON_H

class Button {

  public:
    Button(int);
    bool pressed();

  private:
    int _pin;
    int _lastState;
};

#endif

Config.h


#define PREV_BUTTON_PIN A5
#define START_STOP_BUTTON_PIN A4
#define NEXT_BUTTON_PIN A3

#define VOLUME_PIN A2