Connect two controllers together to play a file, yet it delayed and seems to play the wrong file

So I connected 2 arduinos using RX pin on the board that play a specific file, and TX pin on the board that receive IR signal. It does receive signal, but I think it played the wrong file. I had the memory chip and the IR model KY-022. Here is my code for the IR receiver:

#include <IRremote.h>
// #include "SoftwareSerial.h"
// #include "DFRobotDFPlayerMini.h"

const int IR_RECEIVER_PIN = 2;
IRrecv irrecv(IR_RECEIVER_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);// serial 
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    switch(results.value) {
      case 0xE318261B: // button 1
        Serial.write(1);
        break;
      case 0xFFA25D: // button 2
        Serial.write(2);
        break;
      
      // Add more cases for additional buttons
    }
    irrecv.resume(); // Receive the next value
  }
}

Then it passed the value to the board that play music:

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

SoftwareSerial mySoftwareSerial(12, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int fileNumber;

void setup() {
  Serial.begin(9600); // start the serial communication at 9600 baud
  mySoftwareSerial.begin(9600);
  if (!myDFPlayer.begin(mySoftwareSerial, true, false)) {
    while(true){delay(0); }  
  }
  myDFPlayer.volume(10); 
}

void loop() {
  if (Serial.available() > 0) {
    fileNumber = Serial.read(); // receive the file number from the other Arduino
  }

  switch (fileNumber) {
    case 1:
      myDFPlayer.play(1); delay(1);
      break;
    case 2:
      myDFPlayer.play(2); delay(1);
      break;
    case 3:
      myDFPlayer.play(3); delay(1);
      break;
    // add more cases for additional files
  }
}

Yet it can not play the right file. Can anyone help me? Thanks in advance.

Welcome to the forum

  • Have you tried printing what you receive on the second Arduino ?
  • Have the 2 Arduinos got a common GND connection ?
  • Why use 2 Arduinos ?
  • Why not use SoftwareSerial on the first Arduino ?
  • I don't know how to that, please tell me.
  • I use it for supplying power for both arduino, while plugging it into the 2 USB.
  • I just only realized tho (it needs testing as I am using the 3.3 one)
  • First one is for receiving IR signal, so i dont think of that

Add a Serial.lprintln(fileNumber); after reading fileNumber from Serial.

Using 2 USB ports probably means that you have a common GND if they are on the same PC

I have no idea what using 3.3V has to do with it. A schematic of your project would help

receiving an IR-signal and then calling a function like

myDFPlayer.play(1);

should be easy to do on a single arduino

best regards Stefan

thank you for helping btw. I realized it only need 1 arduino to work lol. However the IR signal is somewhat intriguing as it sometimes decoded into something else (have no idea lol I use KY-022 and its remote) I dk whats wrong with that

Thank you, it turned out work well, but the remote issue ... I dont know if that is intended or it is malfunctioning tho. It works for like 60 70 then the IR signal just turned into different lol

This might be a hardware issue. How did you connect the IR-sensor to the Arduino
post a hand-drawn schematic and a picture

here's mine schematic (drawn on the Paint):

You have connected the IR-receiver to 3.3V while the arduino seems to be a 5V-µC. What does the datasheet of the KY-022 say about maximum voltage?
if it is allowed to supply with 5V you should do so.

Otherwise I would recommend a voltage-levelshifter

What does the datasheet say about the carrier-frequency of the sensor?
32, 36, 38 or 40 kHz?
What does your sender have for a carrier-frequency? The carrier-frequency must be the same

best regards Stefan

last time with 5v i burnt one of my ir receiver, so i do it again with 3.3v. and btw I see it only happens after like 10 times clicking the same button, so it is okay now i guess :smile: . thanks for assistance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.