Adafruit MP3 Shield

I am currently trying to figure out how to play sound using the Adafruit MP3 shield. I am able to upload and run the sample code that is located in the examples (so no hardware issues). However, I noticed that the example only plays the music during the initial set up loop and not in the void loop. I tried to use similar code to use in mine but it is not working quite right. I can get my code to play sound during the set up loop (just like the example) but not in the void loop. I think I may be missing some simple line of code so that I can play sound in the void loop.

Basically I want to be able to play a sound when a button is pressed. What parts of the example code to I need to include in my set up and void loops? I don't have my code on this computer, I will have to share later when I get a chance but any advice will help.

Thanks,
Scott

Can you give a link to the sample code so we don't have to search for it?

Code I am trying to use

/ include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Simple Test");

  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
  SD.begin(CARDCS);    // initialise the SD card
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
  // Play one file, don't return until complete
  Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("track001.WAV");
  // Play another file in the background, REQUIRES interrupts!
  Serial.println(F("Playing track 002"));
  musicPlayer.startPlayingFile("track002.WAV");
}

void loop() {
  // File is playing in the background
  if (musicPlayer.stopped()) {
    Serial.println("Done playing music");
    while (1);
  }
  if (Serial.available()) {
    char c = Serial.read();
    
    // if we get an 's' on the serial console, stop!
    if (c == 's') {
      musicPlayer.stopPlaying();
    }
    
    // if we get an 'p' on the serial console, pause/unpause!
    if (c == 'p') {
      if (! musicPlayer.paused()) {
        Serial.println("Paused");
        musicPlayer.pausePlaying(true);
      } else { 
        Serial.println("Resumed");
        musicPlayer.pausePlaying(false);
      }
    }
  }

  delay(100);
}

The command:-

musicPlayer.playFullFile("track001.WAV");

is what starts off the player. Therefore just take a code that turns on an LED when the button is pushed and replace the LED turn on bit with this line.

I will have to post my code later to show what the problem is. The example code is in my void set up loop along with one play sound command to test that it works. When my code uploads and the set up loop runs it plays the sound as expected.

However, when I place a musicPlayer.playFullFile command in the void loop anywhere (first line of code or even within a button push if statement) no sound plays. It is as if that line of code (musicPlayer.playFullFile) is skipped for some reason. Note there are no typos because the code complies it just does not execute the play command.

Thanks,
Scott

I will have to post my code later to show what the problem is.

Yes you will. That way gives us a chance in spotting what is wrong.

Note there are no typos because the code complies

That does not always follow.

It is as if that line of code (musicPlayer.playFullFile) is skipped for some reason.

But it is not I can assure you.

My guess is that you are doing that line many many times and there is not enough time for the music to play before it stops and starts again. Hence no sound at all.

But that is only a guess, we need to see your code.

my code attached in a note file

hockeygame.txt (34.5 KB)

Well that is one gargantuan mess of a code.

You really must learn how to write functions to break your code down into manageable chunks. The structure of what that code is trying to do is lost in the excessive verbiage of the code.

The thing when this happens is to write a short piece of code that concentrates on just illustrating the problem.

I admit I am new at coding, however I was trying to get everything working (sound included) and then simplifying the code. Is it possible to get the sound to play properly using the .txt code or do I need to rewrite it to create some functions like a clock function and a read switches function.

I think I know how to create that...do I use the void function?

Thanks,
Scott

Is this better Grumpy_Mike? I think this is how the functions should look. I still have a ways to go.

hockeygame-with functions.txt (11.7 KB)

Better but still no coconut. I have no idea what that code is trying to do. I can see no calls to the sound module in the working code, i can just see some setup calls.
What do you want to achieve? How do you want it to work?

Please read this:-
How to use this forum It asks you to provide links to non standard libraries. I do not have the Adafruit_VS1053.h

As I said:-

he thing when this happens is to write a short piece of code that concentrates on just illustrating the problem.

That means cutting out all the display stuff and just concentrating on the code you need to get the sound working.

however I was trying to get everything working (sound included)

That way you are sure not to succeed in anything. No one gets everything working at once. The way to approach a project it one element at a time, test and get working. Then slowly build up the project always on the back of working code.

Grumpy_Mike, the new code that I posted was just to show how I was planning on restructuring my code using functions; not to debug the sound code. Sorry if I was not clear. The code is for a hockey scoreboard for a stiga hockey game. The first "mess" I posted works great (just no sound, looks like it is repeating too fast and not having time to play the sound as you suggested). That code works exactly how I want it but it needs cleaned up with functions as you said. The goal is to have the following:

1.) Read Switches
2.) Based on the switch states (if, else if, else if) the game is Off (LED displays are off), Game mode 1(to X Goals Game) or game mode 2 (timed game with X long periods).
a.) Function will run based on switch state to determine the starting parameters (period time, goal limit, etc)
3.) Once a set-up button is pushed a function will run to setup the scoreboard will set score to 0-0 (instead of ---), this gives visual indication that game is ready (not coded to a function yet, had to stop here) also play start up sound.
4.) Once a start button is pushed a function will run to start the game, the time will start to start the game. (not coded to a function yet)
a.) break beam sensors will detect goals, stop clock, change score and play sound
b.) random crowd sounds will also be played during game
5.) Once game is finished play end game sound and display the final for 10 seconds

Any suggestions on how to make the sound play when it is looping too fast. I have no idea where to start?