Using a single button for multiple steps

I am using an arduino uno + mp3 mini dfplayer to turn an old radio into a jukebox.

I found most of the stuff i need in the "DFRobotDFPlayerMini.h" library, except for an easy way to change folders. I want to assign a button to "next folder" and one to "previous folder". I only found "void playMp3Folder(int fileNumber);" so i'll have to find a way around it.

I'm thinking of using SwitchCase and make a case to play each folder, or create a function for each folder but i have no idea how to call it.

Any ideas?

Hello

So start putting your project to the side for a moment and write a code that handles 2 buttons and has a global variable int folderNumber; that can range from 1 to 99 and have one button increase the value, the other one decrease it and then print the result.

If you don’t want to manually handle the buttons yourself, you can use libraries such as the OneButton Library or the code proposed by @bricoleau in this post (French but see attached zip)

Once you get that working inject what you learnt into your code and you can leverage DFPlayer’s function such as the following ones to address specific folders

myDFPlayer.playFolder(15, 4);  //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05

just use a variable that changes every time the button is pressed and then scroll through a list by adding one.

DH12043: just use a variable that changes every time the button is pressed and then scroll through a list by adding one.

Increment/decrement when the appropriate switch BECOMES pressed, NOT IS pressed.

Try something of the likes of this:

int buttonOne = 0;
int timesPressed = 0;

void setup {
  pinMode(3, INPUT_PULLUP);  // buttonOne Pin
}

void loop {
  buttonOne = digitalRead(3);

  if (buttonOne == LOW) {
    lastButtonPressed = buttonPressed;
    buttonPressed = 1;
  }
  else {
    lastButtonPressed = buttonPressed;
    buttonPressed = 0;

  if (buttonPressed != lastButtonPressed && buttonPressed != 0) {
    timesPressed++;


  if (timesPressed == 0) {
    //do something
  }
  if (timesPressed == 1) {
    //do something
  }
  if (timesPressed == 2) {
    //do something
  }
  if (timesPressed == 3) {
    timesPressed = 0:
}

Please not that i have not actually tried compiling it but it should work as a great example for you to work off of.

Good Luck;

--DH

  if (timesPressed == 0) {
    //do something
  }
  if (timesPressed == 1) {
    //do something
  }
  if (timesPressed == 2) {
    //do something
  }
  if (timesPressed == 3) {
    timesPressed = 0:
}

How many of these if statements can be true at the same time? If the answer is not more than one, then if/else if/else makes more sense than 4 discrete if statements that appear as though they could all be true at the same time.

yes, PaulS is correct, since timesPressed can only equal one number at a time, the first one should be an ‘if’ statement, and the following three should be ‘else if’ like this,

if (timesPressed == 0) {
    //do something
  }
  else if (timesPressed == 1) {
    //do something
  }
  else if (timesPressed == 2) {
    //do something
  }
  else if (timesPressed == 3) {
    timesPressed = 0:
}

Petronel, please tell us if this is of any help.

– DH

Thanks for the input guys.

I have changed the approach, because i realized these buttons will remain pressed until i press another button. So i opted to use a button for each folder as i have around 7 remaining buttons. Perhaps i will get back to that approach if i find a way to change the radio buttons mechanism.

In the meantime i ran into another problem. I have two normal buttons (that come back to the initial position) that i use for next and previous track. Using the DFRobotDFPlayerMini.h library i assigned them the commands "next();" and "previous();". While the commands work, after the next or previous track is over, it keeps looping it, without going automatically to the next track, and i haven't found any command in the library to help with this issue.