I’m trying to build a sound board where one of 2 IR break beam sensors triggers a DFplayer mini to play a specific track (one track for each sensor). And another track to play on load.
I’m modifying a bit of code I found online for something close to what Im looking for.
I have gotten it to upload with no problems. HOWEVER there is no sound.
I have tested the DFPlayer (2 different ones same result), and when I ground the IO1 and IO2 pins the tracks play just fine. and the LEDs are working as well.
Whats happening:
When you trip the AWAY IR, the LED lights up and shuts off Perfectly but NO SOUND
When I trip the Home IR there is NO SOUND, and the LED Does NOT light up. However I can see a state change in the on board TX led.
Also no sound on start up (supposed to play 0003.mp3)
The serial Monitor shows:
14:09:33.682 → track 1
(same for track 2 and track 3)
When the IR Break beams are tripped (track 3 on upload).
Can anyone please help? I’m still learning but I cant seam to see what may be the problem.
// Name Files as follows
//
// sd:/mp3/0001.mp3 // Home Goal
// sd:/mp3/0002.mp3 // Away Goal
// sd:/mp3/0003.mp3 // Start Up Sound
const int buttonPin2 = 4; // Home IR
const int buttonPin3 = 2; // Away IR
const int buttonPin4 = 3; // not used (future option)
const int ledPin1 = 8; // Home Led
const int ledPin2 = 12; // Away Led
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
// implement a notification class,
// its member methods will get called
//
class Mp3Notify
{
public:
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(uint16_t globalTrack)
{
Serial.println();
Serial.print(“Play finished for #”);
Serial.println(globalTrack);
}
static void OnCardOnline(uint16_t code)
{
Serial.println();
Serial.print("Card online ");
Serial.println(code);
}
static void OnCardInserted(uint16_t code)
{
Serial.println();
Serial.print("Card inserted ");
Serial.println(code);
}
static void OnCardRemoved(uint16_t code)
{
Serial.println();
Serial.print("Card removed ");
Serial.println(code);
}
};
// instance a DFMiniMp3 object,
// defined with the above notification class and the hardware serial class
//
//DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);
// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
//SoftwareSerial secondarySerial(10, 11); // RX, TX
//DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
Serial.begin(9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
Serial.println(“initializing…”);
// Play start up Track
Serial.println(“track 3”);
mp3_play (3);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
// HOME check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
// play track 1:
Serial.println(“track 1”);
mp3_play (1);
//mp3.playMp3FolderTrack(1); // sd:/mp3/0001.mp3
delay(2500);
// turn LED on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
// AWAY check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
// play track 2:
Serial.println(“track 2”);
//mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
delay(2500);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}