pause button code

hi guys,

i am making a arduino based music player which plays .wav files. unfortunately, the pause button does not work. i am sure that the circuit is well connected. what should be the problem with my codes? please help asap. thanks! btw, when the pause button is pressed, it shoul pause the current playing song, and when pressed again it should resume playing the song but it does not happen

#include “SD.h”
#include “TMRpcm.h”
#include “SPI.h”
#define SD_ChipSelectPin 4
TMRpcm music;

int song_number=0;

boolean debounce1=true;
boolean debounce2=true;
boolean play_pause;

void setup()
{
music.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin))
{
Serial.println(“SD fail”);
return;
}

pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
music.setVolume(5);
music.quality(1);

}

void loop()
{

if (digitalRead(2)==LOW && debounce1 == true)
{

song_number++;
if (song_number==5)

{
song_number=1;
}
debounce1=false;
Serial.println(“KEY PRESSED”);
Serial.print(“song_number=”);
Serial.println(song_number);

if (song_number ==1)
{
music.play((char*)“1.wav”);
delay(300);
}

if (song_number ==2)
{
music.play((char*)“2.wav”);
delay(300);
}

if (song_number ==3)
{
music.play((char*)“3.wav”);
delay(300);
}

if (song_number ==4)
{
music.play((char*)“4.wav”);
delay(300);
}

if (digitalRead(3)==LOW && debounce2 == true)

{

music.pause();
Serial.println(“PLAY / PAUSE”);
debounce2=false;

}

if (digitalRead(2)==HIGH)
debounce1=true;

if (digitalRead(3)==HIGH)
debounce2=true;
}

}

musicplayer.ino (1.3 KB)

Instead of all that ‘debounce, debounce, debounce’ stuff, try putting a 1 to 10 uF cap across the switch. Coupled with the internal pull_up resistor, this should give you an RC pause of about 20 to 100 ms.

So should i remove the "debounce"? What should the whole code look like? Can you reply it here plss. Sorry im a newbie

mind_curious:
So should i remove the "debounce"? What should the whole code look like? Can you reply it here plss. Sorry im a newbie

It's amazing how quickly they go from, "can you give a little help" to "can you just write the whole code for me for free"

OP: Read the two threads circled in this picture:

They're full of information on how to get good help here.

You should also note that @ChrisTenone was giving you a suggestion on how to do things better. He was assuming that you understood what you have now. Is that a safe assumption? It was not meant as a magic bullet, just fix this line and the whole thing works.

My bad @Delta_G. Im just too confused and pressured in making this project.

you should try using functions and interrupts. Makes things a bit more clearer. You have a lot going on in your loops

I would put your play/pause button especially on an interrupt pin (2/3)
look up attachInterrupt and copy code in regards to that.

Something like this for the pause function and it will interrupt and active the pause.

void pausePlayback () {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
 // If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 300) {
  if (mode == 1){
    audio.pause();
    }
  }
  last_interrupt_time = interrupt_time;
}

I wouldn't use an interrupt for a button a human is going to press. That's a really slow process for a microprocessor. Interrupts are for things that happen so fast you could miss them. Unless The Flash is pushing the button you shouldn't need the added complexity of an interrupt. That's just a hack to keep from having to write non-blocking code.

mind_curious:
My bad @Delta_G. Im just too confused and pressured in making this project.

It's just code. Don't let it pressure you.

_G, your point is well taken.

My method mind_curious, is to go slow and think each step through. Ordinarily you read a switch attached to an Arduino pin by using digitalRead(). So start out just reading a switch. Send the output to the serial monitor, and you will see why you want to “debounce” a switch. Instead of being just on or off it’s onoffonoffonoff. Debouncing causes the switch to take some time to change states. That way it settles down to either on or off before you take action based on its state.

However software debouncing adds a lot of complexity to a program, so I prefer to do it with hardware. When you put in a capicitor that charges up through a resistor, it takes some time to do so. That time is the RC value I mentioned earlier. A few tens of microfarads of capicitance with the approximately 20K ohms resistor of the internal pull up provides this wait time.

I hope this helps you understand debouncing at a more fundamental level.

Can i use 10k resistor instead of 20k @ChrisTenone?

mind_curious:
Can i use 10k resistor instead of 20k @ChrisTenone?

Yes you could. However there is a built in resistor that is automatically engaged (between the pin and the 5 volts) when you set pinMode() to INPUT_PULLUP . That resistor is somewhere between 20K and 50K ohms. So I find it always easier to use the built in resistor, that way, I don't need to remember to put one on the board. A pull up resistor makes it so that digitalRead() of the pin is HIGH unless you pull it low by connecting it to ground through the switch. Larger values are preferred so that very little current will flow when the switch is closed. (20K ohms means that .25 milliamps will flow when the switch is closed.)

Btw, this is where i based my circuit.

I got help from the community for this code, but some of it is my own work. Hope it helps!

I didn’t check to see if the stripped down code compiles, but this should be everything you need to get most, if not all the way there.

Let me know if it worked for you!

Pause.ino (1.26 KB)

ChrisTenone:
Instead of all that 'debounce, debounce, debounce' stuff, try putting a 1 to 10 uF cap across the switch. Coupled with the internal pull_up resistor, this should give you an RC pause of about 20 to 100 ms.

Thanks for this bit of knowledge( I know this is an old post), this seems so much cleaner than all of the button debounce code that you have to add in. I'm going to give this a try on a button I've been using and see how it works in my situation.

Thanks @ChrisTenone