How to play sound according to button DFPlayer

Hello. I want to play different sound by push button, so 1 button can play 1 sound. When i'm trying, I only got random noises (sounds like speaker storing). I don't know what to do anymore with this code, I hope somebody can help to solve this. Thanks

this is what happen with this code DFPlayer - YouTube

#include <Servo.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

Servo servo1; 
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

int pushPin1 = 7;
int button1Reading = 0;
int pushPin2 = 6;
int button2Reading = 0;
int pushPin3 = 5;
int button3Reading = 0;
int pushPin4 = 4;
int button4Reading = 0;
int pushPin5 = A2;
int button5Reading = 0;
int pushPin6 = A3;
int button6Reading = 0;

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2 ; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;


void setup() {
  // put your setup code here, to run once:
  pinMode(pushPin1, INPUT_PULLUP);
  pinMode(pushPin2, INPUT_PULLUP);
  pinMode(pushPin3, INPUT_PULLUP);
  pinMode(pushPin4, INPUT_PULLUP);
  pinMode(pushPin5, INPUT_PULLUP);
  pinMode(pushPin6, INPUT_PULLUP);

  servo1.attach(13);
  servo2.attach(12);
  servo3.attach(11);
  servo4.attach(10);
  servo5.attach(9);
  servo6.attach(8);
  
  servo1.attach(13);
  servo2.attach(12);
  servo3.attach(11);
  servo4.attach(10);
  servo5.attach(9);
  servo6.attach(8);


  Serial.begin(9600);
  // Init USB serial port for debugging
  
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
    Serial.println("OK");
    // Set volume to maximum (0 to 30).
    player.volume(30);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() {
  // Init serial port for DFPlayer Mini
  //softwareSerial.begin(9600);
  // put your main code here, to run repeatedly:
  button1Reading = digitalRead(pushPin1);
  if (button1Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo1.write(0);
    player.stop();
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo1.write(115);
    player.play(1);
  }

   button2Reading = digitalRead(pushPin2);
  if (button2Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo2.write(0);
    player.stop();
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo2.write(180);
    player.play(2);
  }

   button3Reading = digitalRead(pushPin3);
  if (button3Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo3.write(0);
    player.stop();
  if (button3Reading == LOW)
  {
    servo3.write(0);
    player.stop();
  } 
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo3.write(180);
    player.play(3);
  }

   button4Reading = digitalRead(pushPin4);
  if (button4Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo4.write(0);
    player.stop();
    // startPlayback(guru, sizeof(guru));
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo4.write(180);
    player.play(4);
  }
  
     button5Reading = digitalRead(pushPin5);
  if (button5Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo5.write(0);
    player.stop();
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo5.write(180);
    player.play(5);
  }

     button6Reading = digitalRead(pushPin6);
  if (button6Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    servo6.write(0);
    player.stop();
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    servo6.write(180);
    player.play(6);
  }
}

Are you using pushbuttons that latch on when you release the button or are they only on whilst you hold the button down ?

i'm using that latch pushbutton for this project

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