SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By kkenna
#192296
I'm using the Sparkfun MP3 shield to play a song if a switch is activated within a certain amount of time (in this case 5 seconds). If the switch isn't activated again within 5 seconds, the music will stop. This code worked fine with other components (LED strip) but each time I add the MP3 code to play music, it stops updating the timer so it's stuck on 5 seconds, therefore not stopping the music.

Based on this post: http://www.billporter.info/forum/topic/ ... -function/, I've changed the line in SFEMP3ShieldConfig.h to #define USE_MP3_REFILL_MEANS USE_MP3_Polled
This fixed the timer so it resets now, but the music does not play, even though it prints the "play" text.

I am not an experienced programmer, so any help will be much appreciated!
Code: Select all
#include <CountDownTimer.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

CountDownTimer conductorTimer; //Timer instance
const int magSensor = 2;

SdFat sd;
SFEMP3Shield MP3player;
int8_t current_song = 1; //current track playing from SD card
volatile boolean isOn = false; //if mag is on or not
volatile boolean began = false; //Boolean stating whether the first song has begun


void setup() {
  Serial.begin(9600);

  MP3player.begin();

  pinMode(magSensor, INPUT_PULLUP); // The D2 pin's internal pull-up resistor is used to bias the pin high.

  attachInterrupt(digitalPinToInterrupt(2), magCheck, LOW); //attaches interrupt to mag switch to check if it's low

  conductorTimer.SetTimer(0, 0, 5);
  conductorTimer.StartTimer();
}


void loop() {
  conductorTimer.Timer();

  if (conductorTimer.TimeHasChanged())
  {
    Serial.print("Time: ");
    Serial.println(conductorTimer.ShowSeconds());
  }


  if (conductorTimer.TimeCheck(0, 0, 0)) {
    isOn = false;
  }

  if (isOn) {
    musicOn();
  }
  else {
    musicOff();
  }


}

void magCheck() {
  conductorTimer.ResetTimer();
  isOn = true;
}


void musicOn() {

  if (MP3player.getState() == paused_playback) { //In the middle of song: resume track
    MP3player.resumeMusic();
    Serial.println("resume");
  } else if (!MP3player.isPlaying() && !began) { //Before the first song: play track
    MP3player.playTrack(current_song);
    began = true;
    Serial.println("play");
  } else if (!MP3player.isPlaying() && began) { //Between songs: move to next track and play
    current_song++;
    MP3player.playTrack(current_song);
    Serial.println("next");
  }

}

void musicOff() {
  MP3player.pauseMusic();
}