Simple MP3 Shield sketch errors

Hello all,
After having too many issues to count with the Seeed Studio Music Shield I decided to try out the Sparkfun MP3 shield. My goal with this project is to have a button hooked up to my arduino uno, when the button is pressed the shield plays track 1, when the button is pressed again it plays track 2, pressed again play track 3… you get the idea. After 10 tracks it would loop back to the beginning. Here is some code for ya:

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h> 
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;

const int buttonPin = 13;
int buttonCount = 0;
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  MP3player.begin();
  pinMode(buttonPin, OUTPUT);
  MP3player.playTrack(11);  // play an opening track
}

void loop() {
  buttonState = digitalRead(buttonPin); //read the pin
 
  if(buttonCount == 10){ //reset if over 10
    buttonCount = 0;
  }
  if(buttonState == HIGH){ //if pin reads a high, increment
    buttonCount++;
    MP3player.playTrack(buttonCount);
    Serial.println(buttonCount);
    delay(500); 
  }
}

In the serial monitor everything is good, I get 1,2,3,4,5,6,7,8,9,10,1,2,3… But the tracks do not play. The opening track does, but nothing else. I have a feeling that I am making a really simple and stupid mistake, would anyone mind pointing it out for me? Thanks.

Nothing jumps out at me. You’re triggering on the button state rather than detecting transitions so it would repeatedly start a new track as long as you keep the button down, but you have a delay of half a second which would be long enough for you to release the button.

I wonder whether the problem is that you’re selecting an invalid track, or that the sequence of calls you’re making to MP3Player is incorrect.

To find out which it is, I suggest you simply play track 11 every time a button press is detected, and see whether that works.

By the way, do you have reason to believe that tracks 1 … 10 are actually loaded?

Yeah, nothing jumped out at me either. I started troubleshooting all over again and it does play all the tracks when I give it an actual number rather than a variable name. So then I tried it with a switch case:

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h> 
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;

const int buttonPin = 13;
int buttonCount = 0;
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  MP3player.begin();
  pinMode(buttonPin, OUTPUT);
  MP3player.playTrack(11);  // play an opening track
}

void loop() {
  buttonState = digitalRead(buttonPin); //read the pin
 
  if(buttonCount == 10){ //reset if over 10
    buttonCount = 0;
  }
  if(buttonState == HIGH){ //if pin reads a high, increment
    buttonCount++;
    Serial.println(buttonCount);
    delay(500);
   switch (buttonCount) {
    case 1:
    MP3player.playTrack(1);
    break;
    case 2:
    MP3player.playTrack(2);
    break;
    case 3:
    MP3player.playTrack(3);
    break;
    case 4:
    MP3player.playTrack(4);
    break;
    case 5:
    MP3player.playTrack(5);
    break;
    case 6:
    MP3player.playTrack(6);
    break;
    case 7:
    MP3player.playTrack(7);
    break;
    case 8:
    MP3player.playTrack(8);
    break;
    case 9:
    MP3player.playTrack(9);
    break;
    case 10:
    MP3player.playTrack(10);
    break;
   }
  }
}

It seems like a lot of unnecessary copying and pasting for such a simple program, but now it works… go figure. Thanks!

A link to the library you are using might be useful. It is possible that the argument type matters. If you supply a type that the library doesn't exactly support, but that it can translate to something it does understand, and that translation is not what you expect/want, that might explain the behavior you are seeing.

PaulS: A link to the library you are using might be useful. It is possible that the argument type matters. If you supply a type that the library doesn't exactly support, but that it can translate to something it does understand, and that translation is not what you expect/want, that might explain the behavior you are seeing.

Library says value should be uint8_t ( uint8_t playTrack(uint8_t); )