Go Down

Topic: VS1053 + button + Stop Playing issue (Read 701 times) previous topic - next topic

pary87

Greetings,

I can not for the life of me figure out how to stop playing an mp3 using our push button. Please take a look at the following excerpt from my code. I excerpted the portion I need help with. The full code is too big to place here but has been attached.


Code: [Select]
//////////////////////////////////////////////////////////////////////////////////////////////
   
  count = 0;
  played = 0;
  while(count<counter*10)    //delay of some selected time delay with the checking of input
  {
    pressed = debounceRead(p_button);
    delay(100);
    if (pressed == LOW)  //the button is pressed
    {
  if (! musicPlayer.startPlayingFile("MUSIC/track002.mp3")) {
    Serial.println("Could not open file track001.mp3");
    while (1);
  }
  Serial.println(F("Started playing"));

  while (musicPlayer.playingMusic) {
    // file is now playing in the 'background' so now's a good time
    // to do something else like handling LEDs or buttons :)
   
    Serial.print(".");
    if (Serial.available()) {
    char c = Serial.read();
   
    // if we get an 's' on the serial console, stop!
    pressed = debounceRead(p_button);
    delay(100);
//    if (c == 's') {
    if (pressed==LOW) {
      musicPlayer.stopPlaying();
    }
    delay(1000);
  }
  Serial.println("Done playing music");
 
}
    }
    count++;
  }
 
  pressed = HIGH;
}
/////////////////////////////////////////////////////////////////////////////////////////////


Using the serial console interface, it stops perfectly fine when I enter 's'.

When I try it using the button , it does not stop it.

Am I placing the

Code: [Select]
  pressed = HIGH;

in the wrong location?

Grumpy_Mike

That code is very badly written. Basically it is recursive, that is loop calls a function, and that function calls loop again. This has the code going round in circles using more and more stack memory. Eventually it will crash or do strange things as the stack collides with the heap.
While recursion can be useful I can not see any reason why it should be being used here. It has all the hallmarks of being badly converted from an existing C program into the Arduino. The
Code: [Select]
while(1)
is a dead giveaway of a badly conceived program.

Now specifically:-
Why are you even considering setting pressed high anyway?

Your code comments out the line:-
Code: [Select]
//    if (c == 's') {
But the matching brace ( } ) is not commented out leaving the sectional blocks all over the place.

When you have a problem like this it is best to strip out all the superfluous code and produce the minimal that shows the problem. Often in doing this you find your error, if not it makes it a whole lot easier for other people to spot the problem.   

Go Up