#004 - Player Box V2

  • Resistor 330Ω x 1
  • Resistor 1KΩ x 2
  • Resistor 10kΩ x 6
  • 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
  • RFID reader x 1
  • Push button x 5
  • Slide switch 2-position x 1
  • Battery storage case for 3 packs AA x 1
  • Goldpins

Code


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

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

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

Button volupeUpButton(VOLUME_UP_PIN);
Button volumeDownButton(VOLUME_DOWN_PIN);

RfidReaderSzalek rfidReaderSzalek(1);

SoftwareSerial mySoftwareSerial(3, 2);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

boolean started = false;
int volumeValue = 10;
int cardId = 0;
int cardSongId = 0;

void setup() {

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

  //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);

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

void loop() {
  delay(100);

  cardId = rfidReaderSzalek.getId();
  if(cardId != 0 && cardSongId != cardId) {
    cardSongId = cardId;
    myDFPlayer.play(cardSongId);
  }

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

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

  if(volupeUpButton.pressed()) {
    myDFPlayer.volumeUp();
  }

  if(volumeDownButton.pressed()) {
    myDFPlayer.volumeDown();
  }

  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
#define VOLUME_UP_PIN 5
#define VOLUME_DOWN_PIN 6

RfidReaderSzalek.cpp


#include "Arduino.h"
#include 
#include 
#include "RfidReaderSzalek.h"

#define SS_PIN 8
#define RST_PIN 7

String lastUuid = "empty";

MFRC522 rfid(SS_PIN, RST_PIN);

RfidReaderSzalek::RfidReaderSzalek(int x) {
}

void RfidReaderSzalek::init(){
  SPI.begin();
  rfid.PCD_Init();
}

void RfidReaderSzalek::reset(){
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

int RfidReaderSzalek::getId() {
    String uuid = getUuid();
    if(uuid == "0x1440092b") return 1;
    else if(uuid == "0x3bf88554") return 2;
    else if(uuid == "0x0bc92151") return 3;
    else if(uuid == "0x8b138d50") return 4;
    else if(uuid == "0x0b9a9e54") return 5;
    else if(uuid == "0x2b41a154") return 6;
    else if(uuid == "0xabdd9f54") return 7;
    else if(uuid == "0x3b8ca054") return 8;
    else if(uuid == "0xebbaa854") return 9;
    else if(uuid == "0x7b50a654") return 10;
    else if(uuid == "0xeb73a454") return 11;
    else if(uuid == "0x9b8ba254") return 12;
    else if(uuid == "0xeb92a454") return 13;
    else if(uuid == "0x8b6e9a54") return 14;
    else if(uuid == "0x3b99a554") return 15;
    else if(uuid == "0x9b777050") return 16;
    else if(uuid == "0xfbf78554") return 17;
    else if(uuid == "0xfbc2a954") return 18;
    else if(uuid == "0x9b388e50") return 19;
    else if(uuid == "0xab81a254") return 20;
    return 0;
}

String RfidReaderSzalek::getUuid() {
  String uuid = byteToString(rfid.uid.uidByte, rfid.uid.size);
  reset();
  if(uuid != "0x00000000") {
    lastUuid = uuid;
  }
  return lastUuid;
}

bool RfidReaderSzalek::isValid() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if (!rfid.PICC_IsNewCardPresent())
    return false;

  // Verify if the NUID has been readed
  if (!rfid.PICC_ReadCardSerial())
    return false;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return false;
  }

  return true;
}

String RfidReaderSzalek::byteToString(byte *buffer, byte bufferSize) {
  if(!isValid()) {
    return "0x00000000";
  }

  String tmp = "0x";
  for (byte i = 0; i < bufferSize; i++) {
   if(buffer[i] < 0x10) {
      tmp += '0';
    }
   tmp += String(buffer[i], HEX);
  }

  if(tmp == "0x") {
    return "0x00000000";
  }

  return tmp;
}

RfidReaderSzalek.h


/*
 * Signal      Pin
 * ---------------
 * RST          7
 * SDA(SS)      8
 * MOSI         11
 * MISO         12
 * SCK          13
 */
#ifndef RFID_READER_SZALEK_H
#define RFID_READER_SZALEK_H

class RfidReaderSzalek {

  public:
    RfidReaderSzalek(int);
    void init();
    int getId();
    String getUuid();

  private:
    String lastUuid;
    void reset();
    bool isValid();
    String byteToString(byte*, byte);
};

#endif

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

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

Button prevButton(PREV_BUTTON_PIN);
Button startStopButton(START_STOP_BUTTON_PIN);
Button nextButton(NEXT_BUTTON_PIN);
RfidReaderSzalek rfidReaderSzalek(1);

SoftwareSerial mySoftwareSerial(3, 2);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

boolean started = false;
int volumeValue = 0;
int cardId = 0;
int cardSongId = 0;

void setup() {

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

  //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);

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

void loop() {
  delay(100);

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

  cardId = rfidReaderSzalek.getId();
  if(cardId != 0 && cardSongId != cardId) {
    cardSongId = cardId;
    myDFPlayer.play(cardSongId);
  }

  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

RfidReaderSzalek.cpp


#include "Arduino.h"
#include 
#include 
#include "RfidReaderSzalek.h"

#define SS_PIN 8
#define RST_PIN 7

String lastUuid = "empty";

MFRC522 rfid(SS_PIN, RST_PIN);

RfidReaderSzalek::RfidReaderSzalek(int x) {
}

void RfidReaderSzalek::init(){
  SPI.begin();
  rfid.PCD_Init();
}

void RfidReaderSzalek::reset(){
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

int RfidReaderSzalek::getId() {
    String uuid = getUuid();
    if(uuid == "0x1440092b") return 1;
    else if(uuid == "0x3bf88554") return 2;
    else if(uuid == "0x0bc92151") return 3;
    else if(uuid == "0x8b138d50") return 4;
    else if(uuid == "0x0b9a9e54") return 5;
    else if(uuid == "0x2b41a154") return 6;
    else if(uuid == "0xabdd9f54") return 7;
    else if(uuid == "0x3b8ca054") return 8;
    else if(uuid == "0xebbaa854") return 9;
    else if(uuid == "0x7b50a654") return 10;
    else if(uuid == "0xeb73a454") return 11;
    else if(uuid == "0x9b8ba254") return 12;
    else if(uuid == "0xeb92a454") return 13;
    else if(uuid == "0x8b6e9a54") return 14;
    else if(uuid == "0x3b99a554") return 15;
    else if(uuid == "0x9b777050") return 16;
    else if(uuid == "0xfbf78554") return 17;
    else if(uuid == "0x00000000") return 18;
    else if(uuid == "0x00000000") return 19;
    else if(uuid == "0x00000000") return 20;
    else if(uuid == "0x00000000") return 21;
    else if(uuid == "0x00000000") return 22;
    else if(uuid == "0xfbf42151") return 23;
    else if(uuid == "0xfb3ca854") return 24;
    else if(uuid == "0x1b89ab54") return 25;
    else if(uuid == "0x7b189754") return 26;
    else if(uuid == "0x00000000") return 27;
    else if(uuid == "0x00000000") return 28;
    else if(uuid == "0xdb777550") return 29;
    else if(uuid == "0xbbfca654") return 30;
    else if(uuid == "0x00000000") return 31;
    else if(uuid == "0x00000000") return 32;
    else if(uuid == "0x9b91c552") return 33;
    return 0;
}

String RfidReaderSzalek::getUuid() {
  String uuid = byteToString(rfid.uid.uidByte, rfid.uid.size);
  reset();
  if(uuid != "0x00000000") {
    lastUuid = uuid;
  }
  return lastUuid;
}

bool RfidReaderSzalek::isValid() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if (!rfid.PICC_IsNewCardPresent())
    return false;

  // Verify if the NUID has been readed
  if (!rfid.PICC_ReadCardSerial())
    return false;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return false;
  }

  return true;
}

String RfidReaderSzalek::byteToString(byte *buffer, byte bufferSize) {
  if(!isValid()) {
    return "0x00000000";
  }

  String tmp = "0x";
  for (byte i = 0; i < bufferSize; i++) {
   if(buffer[i] < 0x10) {
      tmp += '0';
    }
   tmp += String(buffer[i], HEX);
  }

  if(tmp == "0x") {
    return "0x00000000";
  }

  return tmp;
}

RfidReaderSzalek.h


/*
 * Signal      Pin
 * ---------------
 * RST          7
 * SDA(SS)      8
 * MOSI         11
 * MISO         12
 * SCK          13
 */
#ifndef RFID_READER_SZALEK_H
#define RFID_READER_SZALEK_H

class RfidReaderSzalek {

  public:
    RfidReaderSzalek(int);
    void init();
    int getId();
    String getUuid();

  private:
    String lastUuid;
    void reset();
    bool isValid();
    String byteToString(byte*, byte);
};

#endif