Build your own MP3 player like the Toniebox
FunduinoLast updated on
Eine Audiobox bauen, die per RFID-Karte Hörspiele und Musik startet
Material
Assembly and wiring
Prepare SD card
Install libraries
Source code
Sketch: MP3 box with RFID control
//Library to work with the DFPlayer.
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
//Libraries to work with the RFID module.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
SoftwareSerial mySoftwareSerial(6, 7); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
//Assigns a pin to each button.
int TasteVolumeUP = 3;
int TasteVolumeDOWN = 5;
int Pause = 4;
//Stores whether a button is pressed or not.
int STATUSup = 0;
int STATUSdown = 0;
int STATUSpause = 0;
int Volume = 15; //Stores the set volume
int iPause = 0; //Indicates whether playback is paused or not.
void setup()
{
SPI.begin();
mfrc522.PCD_Init();
mySoftwareSerial.begin(9600);
Serial.begin(115200);
pinMode(TasteVolumeUP, INPUT);
pinMode(TasteVolumeDOWN, INPUT);
pinMode(Pause, INPUT);
if (!myDFPlayer.begin(mySoftwareSerial)) { //SoftwareSerial for communication with the MP3 module
Serial.println(F("Error:"));
Serial.println(F("1.Connect the MP3 module!"));
Serial.println(F("2.Insert a memory card!"));
while (true);
}
myDFPlayer.volume(Volume); //Set the volume between 0 and 30. We assigned the variable a value of 15 as the starting value.
}
void loop()
{
STATUSup = digitalRead(TasteVolumeUP);
STATUSdown = digitalRead(TasteVolumeDOWN);
STATUSpause = digitalRead(Pause);
// Now check what should happen when the pause button is pressed. If the device is playing, it should pause. If it is already paused, playback should resume.
if (STATUSpause == HIGH)
{
if (iPause == 0)
{
myDFPlayer.pause();
delay(500);
iPause = 1;
}
else if (iPause == 1)
{
myDFPlayer.start();
delay(500);
iPause = 0;
}
}
if (STATUSup == HIGH && Volume < 30) // If Volume+ is pressed and the maximum volume (value 30) has not yet been reached, increase the volume.
{
Volume = Volume + 5;
myDFPlayer.volume(Volume);
Serial.println(Volume);
delay(500);
}
if (STATUSdown == HIGH && Volume > 0) // If Volume- is pressed and the minimum volume (value 0) has not yet been reached, decrease the volume.
{
Volume = Volume - 5;
myDFPlayer.volume(Volume);
Serial.println(Volume);
delay(500);
}
// Read the RFID tag here
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code = 0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code = ((code + mfrc522.uid.uidByte[i]) * 10);
}
// Display the card number in the Serial Monitor.
Serial.print("The card number is: ");
Serial.println(code);
// IMPORTANT! In our case, the code of the first RFID tag is 446250. You must first determine the code of your own RFID tag by uploading this sketch and reading the number in the Serial Monitor.
if (code == 446250) //If the code is 446250, play the MP3 file named 0002.mp3
{
iPause = 0;
myDFPlayer.play(2); //Call the MP3 file "0002.mp3" here
delay(600);
}
if (code == 1427270) //If the code is 1427270, play the MP3 file named 0001.mp3
{
iPause = 0;
myDFPlayer.play(1); //Call the MP3 file "0001.mp3" here
delay(600);
}
}Assign an MP3 file to an RFID tag
Previously in the sketch
if (code == 446250)
{
iPause = 0;
myDFPlayer.play(2); // The MP3 file "0002.mp3" is called at this point
delay(600);
}Adapted for the new RFID tag
if (code == 123456)
{
iPause = 0;
myDFPlayer.play(12); // The mp3 file "0012.mp3" is called at this point
delay(600);
}Enclosure
Rate the instructions