This code is part of a bigger project (ultimately). A message is supposed to play on the MP3 player when a button is pressed on an IR remote. It worked fine several times, but then only worked intermittently, and now it doesn’t play at all. I didn’t think I changed the code, but must have somehow. I did notice that the two Serial.print commands:
Serial.println("Enabling IRin"); //for the IR remote
Serial.println("Enabled IRin");//for the IR remote
that are in lines 25 and 27 don’t always print out, but sometimes they do. This seems strange.
My ultimate goal is to have different messages play depending on which button is pressed. This sketch was to just play message 1 when any button the IR remote is pressed.
The question is: “why do Serial.println("Enabling IRin") and Serial.println("Enabled IRin") not print out every time, and why does it only play message 1 once in a while (now not At all) when a button is pressed? It used to work. Thanks.
Here is the code:
#include <SoftwareSerial.h> //Allows us to assign different pins for serial use
#include <DFRobotDFPlayerMini.h>
#include <IRremote.h>
//This sketch is an upgraded sketch that adds the IR Remote triggering the MP3 player
int rxPin = 3;
int txPin = 2; //Sets up the send/receive from the Mp3 player
int track = 0001; //This is the track number on the micro SD card
SoftwareSerial fxSerial(rxPin, txPin); //calls the Mp3 player fxSerial
DFRobotDFPlayerMini fxPlayer;
int IRPIN = 11; //for the ir remote
void setup() {
// put your setup code here, to run once:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT); // Tells Arduino what the Mp3 pins are doing
fxSerial.begin(9600); //Sets up the serial function for the Mp3 player
fxPlayer.begin(fxSerial); //this tells Arduino that the serial path for the Mp3 player is fxSerial (the name of the MP3 player)
Serial.begin(9600); // Do I need this?
fxPlayer.volume(20);// Volume can be 10 to 30). Set this to 20 to use less power
Serial.println("Enabling IRin"); //for the IR remote
IrReceiver.begin(IRPIN, ENABLE_LED_FEEDBACK);//for the IR remote
Serial.println("Enabled IRin");//for the IR remote
}
void loop() {
// put your main code here, to run repeatedly:
if (IrReceiver.decode()){ //TRUE if an IR button was pressed, FALSE if not //for the IR remote
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);/*prints the decoded raw IR data to the serial monitor in hexadecimal format. The decodedRawData field holds the raw, unprocessed IR signal. */
fxPlayer.play(track);// plays message 1 because button 1 was pressed
}//end of IF statement
delay(10000); //this plays track 1 for 15 seconds. The speech must be less than 15 seconds long
IrReceiver.resume();/*prepares the IR receiver to start listening for the next IR signal. It essentially tells the IR receiver to reset its internal state and prepare for a new incoming signal.*/
} //End of void loop
Just a SWAG, but perhaps the beginning has failed. That obviously stops the 2nd print, but it can affect the first as well due to buffering. Try adding a flush after each print to see what happens. If the first print shows up but not the second, then something is wrong with the beginning.
Here are some are photos (sorry about the quality). I checked for loose wires, but didn't find any. Like I said, it worked great and then stopped working.
Yes, it is. I do apologize, but I am still relatively new to the forum, and tried but could not find a way to call and add my post to the previous post. I am learning.
I added the Flush commands, and it did not help. I checked the wiring as well. It is supposed to play a 9 second message when a button is pressed on an IR remote. It worked once, but not after (no wires were bumped). As I said earner, it doesn't always print out the
"Enabling IRin" and the "Enabled IRin". This confuses me, because it is the setup loop, and I would think it would always print out.
I used the SoftwareSerial option because a tutorial I watched said to use it if I am using a DFRobotDFPlayerMini. I am not experienced here, and wonder if I did something wrong there? But again, this did work once.
I really appreciate your help; I am running out of ideas.
#include <SoftwareSerial.h> //Allows us to assign different pins for serial use
#include <DFRobotDFPlayerMini.h>
#include <IRremote.h>
//This sketch is an upgraded sketch that adds the IR Remote triggering the MP3 player
int rxPin = 3;
int txPin = 2; //Sets up the send/receive from the Mp3 player
int track = 0001; //This is the track number on the micro SD card
SoftwareSerial fxSerial(rxPin, txPin); //calls the Mp3 player fxSerial
DFRobotDFPlayerMini fxPlayer;
int IRPIN = 11; //for the ir remote
void setup() {
// put your setup code here, to run once:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT); // Tells Arduino what the Mp3 pins are doing
fxSerial.begin(9600); //Sets up the serial function for the Mp3 player
fxPlayer.begin(fxSerial); //this tells Arduino that the serial path for the Mp3 player is fxSerial (the name of the MP3 player)
Serial.begin(9600); // Do I need this?
fxPlayer.volume(20);// Volume can be 10 to 30). Set this to 20 to use less power
Serial.println("Enabling IRin"); //for the IR remote
Serial.flush(); //to ensure that the data you've sent has been completely transmitted before moving on in your program
IrReceiver.begin(IRPIN, ENABLE_LED_FEEDBACK);//for the IR remote
Serial.println("Enabled IRin");//for the IR remote
Serial.flush(); //to ensure that the data you've sent has been completely transmitted before moving on in your program
}
void loop() {
// put your main code here, to run repeatedly:
if (IrReceiver.decode()){ //TRUE if an IR button was pressed, FALSE if not //for the IR remote
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);/*prints the decoded raw IR data to the serial monitor in hexadecimal format. The decodedRawData field holds the raw, unprocessed IR signal. */
fxPlayer.play(track);// plays message 1 because button 1 was pressed
}//end of IF statement
delay(10000); //this plays track 1 for 10 seconds. The speech must be less than 10 seconds long
IrReceiver.resume();/*prepares the IR receiver to start listening for the next IR signal. It essentially tells the IR receiver to reset its internal state and prepare for a new incoming signal.*/
} //End of void loop
That resistor is supposed to go between the Arduino TX and the DFplayer RX.
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
are not needed (as that's taken care of in/by SoftwareSerial).
When I set up SoftwareSerial, to keep things straight, I use a lower pin number for RX than TX - because 'R' comes before 'T' (and it's rxPin, txPin).
I think I have the resistor between the DF player RX (its pin#2) and the Arduino Tx (Arduino pin #2)? I removed the
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
lines. When I upload the sketch. it serials prints
Enabling IRin
Enabled IRin
The speaker makes a short click, but nothing happens when I press the IR remote. The blue light on the remote does not light.
Pin#1 of the DFRobot (the VCC pin) goes to the positive side of a 4.5 volt power supply.
Pin#2 of the DFRobot (the RX pin) goes through a 1K resistor to pin#2 of the Arduino Uno.
Pin#6 and pin #8 of the DFRobot (the SPK pins) go to a 2 inch speaker(I don't know the wattage) .
Pin#3 of the DFRobot (the TX pin) goes to pin#3 of the Arduino Uno.
Pin#7 of the DFRobot (the GND pin) goes to a common ground with the 4.5 volt power supply and the Arduino and the IR Remote.
The minus pin of the IR remote goes to the common ground.
The plus pin of the IR remote goes to the 5 volt pin on the Arduino Uno
The signal pin of the IR remote goes to pin 11 of the Arduino Uno.
I am obviously new to Arduino programming, but I was wondering if my problems have to do with what the Arduino is doing between button pushes on the IR remote. Should I be using a more robust interrupt situation like While loops, or something similar? I thought the IrReceiver.resume() helped there. As I have said, this setup works (usually once) and then stops working, Thanks again for all your help.
It seems to me it's not a matter of "newness".
Your documentation does not square well with your recounting.
So far as this IR aspect of things goes - you ought to develop an understanding of how it works with a sketch where the result is a letter printed in Serial Monitor for each button, without involving other hardware.
From there, instead of printing a letter on SerialMonitor you could send a command (via SoftwareSerial) to the DFPlayer - which, in a way, is nearly the same thing.
Haven't seen where (2 topics) you've successfully gotten the DFPlayer to work, simply enough, with the SoftwareSerial implementation.
I did write a successful sketch that decoded the buttons on my IR remote This is it:
#include <IRremote.h>
int IRPIN = 11;
int decodedRawData;
void setup()
{
Serial.begin(9600);
Serial.println("Enabling IRin");
IrReceiver.begin(IRPIN, ENABLE_LED_FEEDBACK);
Serial.println("Enabled IRin");
}
void loop()
{
if (IrReceiver.decode())
{
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
if (IrReceiver.decodedIRData.decodedRawData ==0xBA45FF00){
Serial.println("start taking");
}
IrReceiver.resume();
}
delay(500);
}
I am not sure what you mean by "you could send a command (via SoftwareSerial) to the DFPlayer "
When I used the DFPlayer sketch (posted earlier), It played the message on the DFPlayer once, but then would not play it again. This happened several times; it plays once, and then not again. I tried just now, and it did not play at all.
My suggestion is to improve the clarity of your communication. For example, are you still asking for help with your original post about the DFR Player, before you started this thread adding IR?
And please use the correct Reply button so that we know who you’re replying to.
As I told you a week or two ago, your player sketch worked here. I recommend you remove all reference to IR from the discussion for the time being until we methodically get the DFR section working properly.
EDIT: Here's your sketch with all IR lines commented out. It works as expected.
/*
Fri 20 Jun 2025 0935; removed all IR stuff
*/
#include <SoftwareSerial.h> //Allows us to assign different pins for serial use
#include <DFRobotDFPlayerMini.h>
//#include <IRremote.h>
//This sketch is an upgraded sketch that adds the IR Remote triggering the MP3 player
//int rxPin = 10; // my edit
int rxPin = 3;
//int txPin = 11; //Sets up the send/receive from the Mp3 player, // my edit
int txPin M= 2; //Sets up the send/receive from the Mp3 player
int track = 1; //This is the track number on the micro SD card, // my edit
// int track = 0001; //This is the track number on the micro SD card
SoftwareSerial fxSerial(rxPin, txPin); //calls the Mp3 player fxSerial
DFRobotDFPlayerMini fxPlayer;
//int IRPIN = 11; //for the ir remote
void setup()
{
// put your setup code here, to run once:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT); // Tells Arduino what the Mp3 pins are doing
fxSerial.begin(9600); //Sets up the serial function for the Mp3 player
fxPlayer.begin(fxSerial); //this tells Arduino that the serial path for the Mp3 player is fxSerial (the name of the MP3 player)
Serial.begin(115200); // Do I need this?, // my edit
// Serial.begin(9600); // Do I need this?
fxPlayer.volume(20);// Volume can be 10 to 30). Set this to 20 to use less power
Serial.println("Enabling IRin"); //for the IR remote
Serial.flush(); //to ensure that the data you've sent has been completely transmitted before moving on in your program
// IrReceiver.begin(IRPIN, ENABLE_LED_FEEDBACK);//for the IR remote
Serial.println("Enabled IRin");//for the IR remote
Serial.flush(); //to ensure that the data you've sent has been completely transmitted before moving on in your program
Serial.println("setup ended"); // my edit
}
void loop()
{
// put your main code here, to run repeatedly:
// if (IrReceiver.decode()) //TRUE if an IR button was pressed, FALSE if not //for the IR remote
{
// Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);/*prints the decoded raw IR data to the serial monitor in hexadecimal format. The decodedRawData field holds the raw, unprocessed IR signal. */
fxPlayer.play(track);// plays message 1 because button 1 was pressed
}//end of IF statement
delay(10000); //this plays track 1 for 10 seconds. The speech must be less than 10 seconds long
// IrReceiver.resume();/*prepares the IR receiver to start listening for the next IR signal. It essentially tells the IR receiver to reset its internal state and prepare for a new incoming signal.*/
} //End of void loop
Sorry for any confusion on my part. I will get better at using the forum. Between the replies to my questions, I made their suggestions, researched the available documents, and did some trial and error. At this point, my sketch works as I wanted it to. All the previous questions I had are solved. Thank you all.
As for hitting the wrong “reply” button, I believe I am now doing the correct thing? I click on this green button:
I do have a new question (I think this should this be a new post, but I wanted to address Terrypin's comments?) I have a sketch with a servo, and get this error message:
C:\Users\Owner\Documents\Arduino\servoTestEraseMe6-21-25\servoTestEraseMe6-21-25.ino: In function 'void setup()':
C:\Users\Owner\Documents\Arduino\servoTestEraseMe6-21-25\servoTestEraseMe6-21-25.ino:10:20: error: expected initializer before '.' token
Servo HeadTurnServo.attach(servoHeadPin);
^
Multiple libraries were found for "Servo.h"
Used: C:\Users\Owner\Documents\Arduino\libraries\Servo
Not used: C:\Users\Owner\AppData\Local\Arduino15\libraries\Servo
exit status 1
Compilation error: expected initializer before '.' token
All of the tutorials and documents I found say my code is correct:
#include <Servo.h>
//This sketch is an just test for servo
Servo HeadTurnServo;
int servoHeadPin = 12; //This is the servo that rotates the head. Does this need to be a squiggly pin?
void setup() {
// put your setup code here, to run once:
Servo HeadTurnServo.attach(servoHeadPin);
HeadTurnServo.write (0);//Zeroes the head to straight ahead.
}
void loop() {
// put your main code here, to run repeatedly:
HeadTurnServo.write(90); //turns the head to the right
delay(1000); //gives the head a short pause before turning back.
HeadTurnServo.write(-90); //turns the head back to straight ahead
} //End of void loop
Obviously I did something wrong. I don't know how to initialize the servo differently.