Play wav Files from Selected folders with push buttons

Ok, Now i got a toggle switch code (working smoothly), where i am trying to merge

this
tmrpcm.play("1.wav");

&

this
tmrpcm.stopPlayback();

for play and stop operation of a song with same input button at pin 2

Here is the code

#include <stdio.h>
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10

TMRpcm tmrpcm;

// constants won't change
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int LED_PIN    = 5; // Arduino pin connected to LED's pin

// variables will change:
int ledState = LOW;     // the current state of LED

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {

    tmrpcm.speakerPin = (9);


    //Serial.begin(9600);
    if (!SD.begin(SD_ChipSelectPin)) 

    {
    Serial.println("SD fail");
    return;
    }

    tmrpcm.setVolume(5);
    tmrpcm.play("0.wav");



  
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode

  currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) 
  {
    Serial.println("The button is pressed");
    tmrpcm.play("1.wav");

    // toggle state of LED
    ledState = !ledState;

    // control LED arccoding to the toggled state
    digitalWrite(LED_PIN, ledState); 
    
  }





 
}

PLEASE LET ME KNOW IF MY ASSUMPTION IS RIGHT OR JUST A DREAMING ARTICLE :face_with_hand_over_mouth:

Think through that a while. How will you know if the track has ended? Can you stop it and play it again?

Ya with speakers by listening to the track . .

and yes i have one more set of code for now but its not toggling properly or say alternately
I mean after pressing 2 or 3 times it changes state & sometimes work properly and so on things

But trying to work with multiple delay values & also some commands from your given code

#include <stdio.h>
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10

TMRpcm tmrpcm;

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  5;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 

{
tmrpcm.speakerPin = (9);


//Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) 

{
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(5);
tmrpcm.play("0.wav");


{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

}

void loop() {
  // read the state of the pushbutton value:

  delay(1);
  
  buttonState = digitalRead(buttonPin);       // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  while(digitalRead(2) == 0)
      delay(0);
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    tmrpcm.play("1.wav");
    delay(20);
  }
    
    
    
    delay(1);

  buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  while(digitalRead(2) == 0)
      delay(0);
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    tmrpcm.stopPlayback();
    delay(20);

    } 
    
    
    
    //else {
    // turn LED off:
    //digitalWrite(ledPin, LOW);
  //}
}

THIS ONE IS WORKING BUT SOMETIMES TRIGGER TWICE FOR A SINGLE PRESS ACTUALLY
PLEASE CHECK THIS ONE WITH PIN 2 AS PUSH BUTTON & PIN 5 AS LED OUT

AT LEAST THIS ONE IS OPERATING WITH PLAY AND STOP OPTION SO IF IT'S POSSIBLE TO MAKE CHANGES HERE THEN MUCH BETTER OR ELSE WE HAVE ANOTHER SET OF CODES WHICH REQUIRES MUCH EFFORTS

[ NOTE :- TRY PRESSING 3 OR 4 TIMES TO GET THE RESULT ]

This one is doing play and stop operation with LED glow and OFF only ONCE & then no action

#include <stdio.h>
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10

TMRpcm tmrpcm;

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  5;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 

{
tmrpcm.speakerPin = (9);


//Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) 

{
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(5);
tmrpcm.play("0.wav");


{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

}

void loop() 

{


do

while(digitalRead(2)==0)


{

  buttonState = !buttonState;
  // read the state of the pushbutton value:

  delay(1);
  
  buttonState = digitalRead(buttonPin);       // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  while(digitalRead(2) == 0)
      delay(20);
  if (buttonState == LOW) 
  
  {
    //buttonState = !buttonState;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    tmrpcm.play("1.wav");
    //delay(25);

  }
  
    //delay(1);

   do 

   while(digitalRead(2)==0)

   {

  buttonState = digitalRead(buttonPin);  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

  while(digitalRead(2) == 0)
      delay(20);
  if (buttonState == LOW) 
 
    {
    
    // turn LED on:
    digitalWrite(ledPin, !ledPin);
    
    tmrpcm.stopPlayback();
    //delay(25);

    }

  
    
    
    
    //else {
    // turn LED off:
    //digitalWrite(ledPin, LOW);
  //}
}

How to make it repeat for ever ?

There a couple of do_whiles that aren't laid out (structured) proper.
The stuff (statements) that gets done repetitively till a condition is no longer true (expression) goes between the do and the while.

do..while - Arduino Reference
vs.
while - Arduino Reference

I see . . . I am on it to get refer . . Thanksss

Would you please resend me the code by making corrections required . . ?

I'm not sure what's supposed to go on.
At any rate, refer to the "model" sketch (i.e. where and how to place what).

Ok I am on it :slight_smile:

Till the time would you please design that folder project in the beginning we discussed

Firstly Just 3 folders with a single file inside in each one . . .that's it !

If you were to have 6 pushbuttons (1, 2, 3, 4, 5, 6)
you should be able to structure things so that the first pushbutton entry is the folder number and the second, the following, is the track number.
My example puts a number into a variable, pbRegister.

Employ, utilize, the principle. You could use a folderRegister and a trackRegister. The folderRegister would be 10, 20, 30 and that would get added with the trackRegister result (1, 2, 3, 4, 5, 6).
Two variables aren't necessary. I have done so to emphasize the concept/s.

Continuing - make a Case (i.e. switch..case, as used in the example) for each possibility (11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36).

That may or may not be a pedestrian approach, but it should prove effective.

PE - Adding this, using three pushbuttons, the rest is just typing, copying, pasting. . .

const byte IN1 = 2;
const byte IN2 = 3;
const byte IN3 = 4;

byte pbRegister = 0;
byte folderRegister = 0;
//byte trackRegister = 0;

void setup() 
{
  Serial.begin(19200);
  Serial.println("Begin..");
  pinMode (IN1, INPUT_PULLUP);
  pinMode (IN2, INPUT_PULLUP);
  pinMode (IN3, INPUT_PULLUP);
}

void loop() 
{
  do
  {
    if (!digitalRead(IN1))
    {
      pbRegister = 10;
    }
    if (!digitalRead(IN2))
    {
      pbRegister = 20;
    }
    if (!digitalRead(IN3))
    {
      pbRegister = 30;
    }
  }while (pbRegister == 0);
  
  folderRegister = pbRegister;
  pbRegister = 0;
  delay(250);  // "debounce"

  do
  {
    if (!digitalRead(IN1))
    {
      pbRegister = 1;
    }
    if (!digitalRead(IN2))
    {
      pbRegister = 2;
    }
    if (!digitalRead(IN3))
    {
      pbRegister = 3;
    }
  }while (pbRegister == 0);

  pbRegister = pbRegister + folderRegister;

  Serial.print("pbRegister = ");
  Serial.println(pbRegister);

  switch(pbRegister)
  {
    case 11:
     Serial.println("f1 t1");
     break;
    case 12:
     Serial.println("f1 t2");
     break;
    case 13:
     Serial.println("f1 t3");
     break;
     
    case 21:
     Serial.println("f2 t1");
     break;
    case 22:
     Serial.println("f2 t2");
     break;
    case 23:
     Serial.println("f2 t3");
     break;
     
    case 31:
     Serial.println("f3 t1");
     break;
    case 32:
     Serial.println("f3 t2");
     break;
    case 33:
     Serial.println("f3 t3");
     break;
  }
  
  pbRegister = 0;
  folderRegister = 0;
  delay(250);  // "debounce", placing earlier would delay everything else
}

OK LE ME TRY . . A FEW THINGS I NEED TO ASK

pbREGISTER = 10; IS FOR FOLDER ???

I have only pin 2 to 6 for in put purpose & here in our case with 3 & 4 pin i need to change the folder & with pin 2 i need to play the selected folder track

If you just Upload the sketch with the three pushbuttons as before. . .

i have 3 folders in me SD card labelled 1 , 2 & 3 . .

Each contain a single track for now with a same label - 31

Have to design for it

Each Case is where you're routed to do what needs to be done.

No luck Sir . . .

Confused with everything and not able to make any

results are randomly changing on serial monitor

please help me with some final design please

What have you got so far?

I don't see where folders are supported.
Looks like only file names ("eekamouse.wav")

Can we proceed with DF Player on the basis of it's properties of containing 100 folders with each having capacity of handling some 1000 around files ??

The DFRobot player is excellent.
(tmrpcm is a pale comparison, akin to a "parlor trick".)
What's the attraction of folders?
Anyway, DFRobot's library works great - if it is used with a genuine DFRobot player.

Yaa looks great . . le me go through this . . . :blush:

Now, Regarding folders => Let's have a best example of earlier CD and DVD players

For a local disc menu, while watching complete actions of player on a TV screen or any monitor & with the help of player's remote control unit we use it's up and down cursor keys to hover over the folders of various movie songs as per it's labelled visible at left side column of screen. .

So now, with this one by one hovering action, for a folder at a particular moment), we are able to see the contents present inside the folder just on right hand side of screen like a list inside a column

Then after getting required movie name folder we use to watch the song list and after getting our song say on 3rd number, we use to press right cursor key on remote unit get to enter into the right side column where the cursor automatically moves on to the first song position of list . . .

Then we press Down cursor key 2 more times to go for the 3rd song to get it select
Now simply we press the PLAY button on remote unit to just play the song

Now song starts playing irrespective to any other actions on remote

Then at this playing moment while enjoying the song, we use to press left cursor key on remote unit to get into the left side column to select another folder from the list with Up and Down keys

Again for another folder we press Right cursor key and again go to song list of that folder for another song to play after this or to play that one canceling out the previous one already playing

So in our case, we would assign the cursor up down key as i mentioned above called perv and next keys

For songs playing action we would assign other pins for 1, 2, 3, 4, 5, an so on depending on the arduino board

Oh just now i recall on something that this kind of action is also present on remote control unit as a keypad where we are able to play the right column song list randomly :smiley:

Wow great then same action we need to apply here . . That's really cool thing for a perfect example

WE need to make the exact CD player like thing and for monitor purpose we have our internationally used 16x4 LCD screen for basic design :sweat_smile: :joy: :rofl:

Well
I ll try to make and forward a short clip with the demo of things we need here . .
I think dvd player is best example :ok_hand: :+1:

Any Update Sir ??

We just need to design DVD player function of up and down cursor and keypad buttons

that's it . . nothing else