Hello everyone,
I'm new here.
I want read MP3 files on my arduino nano, and control it with boutons on RF device.
For that, I use 433MHZ receive,
and I use MP3 DFPlayer,
https://www.amazon.fr/dp/B07Z5D1TX8?psc=1&ref=ppx_yo2ov_dt_b_product_details
I can read MP3 players, and I can associate action when I push buttons A, B, etc ....
I use RCSwitch.h and DFRobotDFPlayerMini.h librairies.
When trying to control my DFPlayer MP3 module, I encountered numerous detection issues. I suspect that the DFPlayer MP3 module heavily utilizes my Arduino's resources, and I might not be using interrupts correctly on pin D2 with RCSwitch. The provided code example was not very helpful, and when I declared my function to decode received signals as an interrupt, it didn't work as expected.
Someone have an Idea ?
void setup() {
initRFDevices();
INITDFPLAYER();
init_pin();
setVolume(15);
Serial.begin(9600);
if(DEBEUG){Serial.begin(9600);
}
if(TEST){test();} // TEST not used = test function
}
unsigned long previousData = 0; // Variable to save the previousData
void loop();
unsigned long data = RF_receive(); // Acquire value
if (data != 0 && data != previousData) {
rc_communication(data); // send new value
previousData = data; // Update previousData
}
}
void initRFDevices() {
mySwitch.enableReceive(digitalPinToInterrupt(INTERRUPT_PIN)); // enable Receive on interrupt pin D2
Serial.begin(9600);
}
unsigned long RF_receive() {
unsigned long key_push = 0;
if (mySwitch.available()) {
key_push = mySwitch.getReceivedValue();
mySwitch.resetAvailable();
}
return key_push;
}
unsigned long RF_receive() {
unsigned long key_push = 0;
if (mySwitch.available()) {
key_push = mySwitch.getReceivedValue();
mySwitch.resetAvailable();
}
return key_push;
}
unsigned long previousInfo =0;
// Define a structure to map received values to corresponding actions
struct ActionMapping {
unsigned long value;
void (*action)();
};
// Define the actions corresponding to the values received
void actionToucheA() {
Serial.println("Touche A Pressed");
playMusic2(1);
}
void actionToucheB() {
Serial.println("Touche B Pressed");
stopMusic();
}
// Create a table mapping received values to corresponding actions
ActionMapping actions[] = {
{ToucheA, actionToucheA},
{ToucheB, actionToucheB}
// Add other mappings as required
};
// Function to manage RF communication
void rc_communication(unsigned long info) {
// Browse the mapping table to find the corresponding action
for (size_t i = 0; i < sizeof(actions) / sizeof(actions[0]); ++i) {
if (info == actions[i].value) {
// Execute the corresponding action
actions[i].action();
return;
}
}
// If no matching action found
Serial.println("No corresponding action found for the value received. ");
}
SoftwareSerial mySerial(7, 8); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void INITDFPLAYER(){
mySerial.begin(9600);
if (!myDFPlayer.begin(mySerial)) {
Serial.println("DFPlayer Mini cannot be initialized! ");
while (true);
}
}
void playMusic(int tracknumber) {
myDFPlayer.play(tracknumber); // play tracknumber
K2000();
}
void pauseMusic() {
myDFPlayer.pause(); // Pause music playback
K2000();
}
void stopMusic() {
myDFPlayer.stop(); // Stop music playback
}
void setVolume(int volume) {
myDFPlayer.volume(volume); // Volume control
}
void playMusic2(int tracknumber) {
myDFPlayer.play(tracknumber); // play tracknumber
}
My receiver is connected to pin D2 with a 10kOhm pull-up resistor.
Here, you have the decoding done by RCSwitch.
Thanks a lot for your help