1

I have a code here, from DFmini player. I have a tactile switch. when I press it on and release it will play a sound. My question is that when I press the tactile but not release it so I want it still play the same sound. But while the tactile switch is pressed down it keeps repeating the same sound. I want it to play just for once while the tactile button is still pressed down.

//#include <SoftwareSerial.h>   //If we use other pins than D0 adn D1 for TX and RX for the music player
#include <DFMiniMp3.h>          //Downlaod here: https://electronoobs.com/eng_arduino_DFPlayer_Mini_Mp3_by_Makuna.php

int sw1 = 3;
int sw2 = 4;
int sw3 = 5;
int sw4 = 6;
int sw5 = 7;
int sw6 = 8;
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);  
  }
};

DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial);   //Create the UART connection to the module

void setup() 
{
  //3 push buttons with pullups
  pinMode(sw1,INPUT);                        //Define each button as input with pullup
  pinMode(sw2,INPUT);
  pinMode(sw3,INPUT);
  pinMode(sw4,INPUT);
  pinMode(sw5,INPUT);
  pinMode(sw6,INPUT);
  digitalWrite (sw1, HIGH); //12 Votls supply set to off state
  digitalWrite (sw2, HIGH); //5 Votls supply set to off state
  digitalWrite (sw3, HIGH); //3 Votls supply set to off state
  digitalWrite (sw4, HIGH); //Ajustable Votls supply set to off state
  digitalWrite (sw5, HIGH); //3 Votls supply set to off state
  digitalWrite (sw6, HIGH); //Ajustable Votls supply set to off state
  Serial.begin(9600); 
  mp3.begin();                                      //Start communication with the DFplayer module
  uint16_t volume = mp3.getVolume();                //Get actual volume
  mp3.setVolume(30);                                //Set new volum (max is 30)
  uint16_t count = mp3.getTotalTrackCount();        //Get the total tracks on the SD card in case we want to sue this later...
}

//just a fucntion that we use to create delays in "ms" 
//without using the delay() function
void waitMilliseconds(uint16_t msWait)
{
  uint32_t start = millis();
  
  while ((millis() - start) < msWait)
  {
    // calling mp3.loop() periodically allows for notifications 
    // to be handled without interrupts
    mp3.loop(); 
    delay(1);
  }
}

void loop() 
{
  if(!digitalRead(sw1))
  {    
      mp3.playMp3FolderTrack(1);  // Play audio track 0001
      waitMilliseconds(1000);     // 1s of delay
  }
  
  if(!digitalRead(sw2))
  {    
      mp3.playMp3FolderTrack(2);  // Play audio track 0002
      waitMilliseconds(1000);     // 1s of delay
  }
  
  if(!digitalRead(sw3))
  {    
      mp3.playMp3FolderTrack(3);  // Play audio track 0003
      waitMilliseconds(1000);     // 1s of delay; 
  }
  
  if(!digitalRead(sw4))
  {    
      mp3.playMp3FolderTrack(4);  // Play audio track 0001
      waitMilliseconds(1000);     // 1s of delay
  }
  
  if(!digitalRead(sw5))
  {    
      mp3.playMp3FolderTrack(5);  // Play audio track 0002
      waitMilliseconds(1000);     // 1s of delay
  }
  
  if(!digitalRead(sw6))
  {    
      mp3.playMp3FolderTrack(6);  // Play audio track 0003
      waitMilliseconds(1000);     // 1s of delay; 
  }
  
}//end of void
5
  • 1
    Does this answer your question? DFplayer, Play sound based on pressed button – jsotola Nov 5 '20 at 2:48
  • 1
    same as your last question ... you have code that does something when you press a button ... you have no code that executes when you release a button ... you also have no code that does anything when the track ends, except for printing a message ... check the messages in the serial monitor – jsotola Nov 5 '20 at 2:53
  • @jsotola, on this line if(!digitalRead(sw4)), I have to tell the code if the button is held down so do the function. if released don't do. i tried else and break but it doesn't work. don't know what to use to tell the software . – Habib Anwari Nov 5 '20 at 9:11
  • Currently you are checking, if the button is currently pressed. Thus the code inside the if statement will execute as long, as the button is pressed. You want to check for the transition. Have a look at the Bounce2 library, which makes it way easier to check buttons. Then you have if(button.fell()) to check, if the button pin fell (for the press transition), or if(button.rose()) to check, if the button pin rose (for the release transition). – chrisl Nov 5 '20 at 10:18
  • @chrisl, Thanks, the Bounce2 Library was useful and help a lot. – Habib Anwari Nov 7 '20 at 22:23
1

BOUNCE 2

Basically, the mechanical part of buttons and switches vibrate slightly when closed or opened causing multiple undesired false states (similar to noise). This library filters out these undesired state changes. More about debouncing:

John Errington's Experiments with an Arduino : Using digital inputs: Switch bounce and solutions to it Wikipedia article : http://en.wikipedia.org/wiki/Debounce#Contact_bounce See the bottom of this page for a basic usage example and the "examples" folder for more.

The library is composed of three classes:

Debouncer : The code that does the actual debouncing. Only advanced users should play with this class. Bounce : This is the general use library. It links the Debouncer to a hardware pin on your board. Button : A special version of Bounce for buttons that are pressed.

https://github.com/thomasfredericks/Bounce2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.