So, things have reached a plateau. It is at a point right now where it will successfully play track001.mp3 through headphones when switch 1 is triggered, but it is not set up yet to run correctly with the relays that make it switch over from the RCA input to input coming from the Arduino.
I could use some help finishing this project.
The following is the code that I have at this point that has been written mostly by a friend. The relays are not set up in the code to do what I want properly. I don't think that he knew exactly how I had them set up. I drew a picture of the way that I have them wired.
I have the 4 toggle switches wired to the GPIO terminals on the Adafruit shield. This is what I want them to do - Switch one (gpio 2). - play track 001. Switch 2 (gpio4) - play track 002. Switch 3 (gpio5)- play random selection from a range of track 003- track 018. Switch 4 (gpio 6)- play track 019.
Eventually I might have switch 4 also be a random selection of a range of songs that people have suggested to me as their best suggestion for a toggle switch song. But if that is figured out successfully for switch 3, that will be easy to copy.

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
//Defining constants and varibles
//---------------UNO Digitial Pins---------------
// These are the UNO pins used for the music maker shield
const int SHIELD_RESET = -1; // VS1053 reset pin (unused!)
const int SHIELD_CS = 7; // VS1053 chip select pin (output)
const int SHIELD_DCS = 6; // VS1053 Data/command select pin (output)
// These are common pins between UNO breakout and shield
const int CARDCS = 4; // Card chip select pin
const int DREQ = 3; // VS1053 Data request, ideally an Interrupt pin
//These are the pins on the UNO (through shield breakout) that will control the relay state
const int RIGHT_AUDIO_RELAY = 2;
const int LEFT_AUDIO_RELAY = 5;
const int GROUND_AUDIO_RELAY = 8;
const int AMP_RELAY = 9;
//-----------------------------------------------
//-------MP3 Shield Pins-------------------------
// These are the GIO pins on the MP3 Shield that will connect to the toggle switches
const int SWITCH01_Pin = 2;
const int SWITCH02_Pin = 4;
const int SWITCH03_Pin = 5;
const int SWITCH04_Pin = 6;
//-----------------------------------------------
//Initialise a varible to store the CURRENT state of a switch
int SWITCH01_STATE = LOW;
int SWITCH02_STATE = LOW;
int SWITCH03_STATE = LOW;
int SWITCH04_STATE = LOW;
//Initialise a varible to store the LAST state of a switch
int SWITCH01_LSTATE = LOW;
int SWITCH02_LSTATE = LOW;
int SWITCH03_LSTATE = LOW;
int SWITCH04_LSTATE = LOW;
//Initialise a varible to store the current and last state of a audio play
int PlAY_STATE = false;
int PlAY_LSTATE = false;
Adafruit_VS1053_FilePlayer musicPlayer =
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//------------------Setup Routine-------------------------------------------------------
// the setup routine runs once when you press reset:
void setup()
{
// Configure toggle switch connections as Input
musicPlayer.GPIO_pinMode(SWITCH01_Pin, INPUT);
musicPlayer.GPIO_pinMode(SWITCH02_Pin, INPUT);
musicPlayer.GPIO_pinMode(SWITCH03_Pin, INPUT);
musicPlayer.GPIO_pinMode(SWITCH04_Pin, INPUT);
// Configure relay ctrl lines output
pinMode(RIGHT_AUDIO_RELAY, OUTPUT);
pinMode(LEFT_AUDIO_RELAY, OUTPUT);
pinMode(GROUND_AUDIO_RELAY, OUTPUT);
pinMode(AMP_RELAY, OUTPUT);
//Initialize state of relays to disabled
disable_uno_audio_source();
//Init serial coms
Serial.begin(9600);
Serial.println("Initial test of the Cruiser Code");
// initialise the music player
if (! musicPlayer.begin())
{
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1); // don't do anything more
}
Serial.println(F("VS1053 found"));
musicPlayer.sineTest(0x44, 500); // Make a tone for 500ms to indicate VS1053 is working
// initialise the SD Card
if (!SD.begin(CARDCS))
{
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println("SD OK!");
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume! 0 = loudest - 254(0xFE) = Silence
musicPlayer.setVolume(20,20);
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
}
//------------------Main Loop-------------------------------------------------------
// This routine runs over and over again forever:
void loop()
{
// Read the current state of the the toggle switches
SWITCH01_STATE = musicPlayer.GPIO_digitalRead(SWITCH01_Pin);
SWITCH02_STATE = musicPlayer.GPIO_digitalRead(SWITCH02_Pin);
SWITCH03_STATE = musicPlayer.GPIO_digitalRead(SWITCH03_Pin);
SWITCH04_STATE = musicPlayer.GPIO_digitalRead(SWITCH04_Pin);
// Store the current music state
PlAY_STATE = musicPlayer.playingMusic;
// If any of the toggle switch 1 transistion from low to high, play the file on SD Card named "track001.mp3"
if (SWITCH01_STATE == HIGH & SWITCH01_LSTATE == LOW) play_file();
//Matt if this sketch is still not working as is try commenting out the above line and uncomment out the
//the following line. If this works we may have an interupt issue. It will also be interesting to see if you can get some info from serial monitor
//if (SWITCH01_STATE == HIGH & SWITCH01_LSTATE == LOW) musicPlayer.playFullFile("track001.mp3");
if (SWITCH01_STATE == LOW & SWITCH01_LSTATE == HIGH) stop_audio();
//If music stops from end of song or toggle switch disable the uno audio source(turn relays off)
if (PlAY_STATE == false & PlAY_LSTATE == true) disable_uno_audio_source();
// Set the last switch state equal to the current state
SWITCH01_LSTATE = SWITCH01_STATE;
SWITCH02_LSTATE = SWITCH02_STATE;
SWITCH03_LSTATE = SWITCH03_STATE;
SWITCH04_LSTATE = SWITCH04_STATE;
// Store the last music state
PlAY_LSTATE = PlAY_STATE;
//Wait 100ms then loop again
delay(100);
}
//--------------Play File Function------------------------------
//Start playing the file that is provided
void play_file()
{
enable_uno_audio_source();
if (! musicPlayer.startPlayingFile("track001.mp3"))
{
Serial.println("Could not open file track001.mp3");
disable_uno_audio_source();
}
Serial.println(F("Started playing"));
}
//--------------Stop Play Function------------------------------
//Stop playing the audio
void stop_audio()
{
musicPlayer.stopPlaying();
Serial.println(F("Stop playing"));
}
//--------------Enable Uno Audio Function------------------------------
/* The following function sets the relay states so the audio produced by the Arduino
is sent to the Amp. It also enable the amp (turn it on)*/
void enable_uno_audio_source()
{
digitalWrite(RIGHT_AUDIO_RELAY, HIGH);
digitalWrite(LEFT_AUDIO_RELAY, HIGH);
digitalWrite(GROUND_AUDIO_RELAY, HIGH);
digitalWrite(AMP_RELAY, HIGH);
}
//--------------Disable Uno Audio Function------------------------------
/* The following function sets the relay states so the auxiliary audio
is sent to the Amp.*/
void disable_uno_audio_source()
{
digitalWrite(RIGHT_AUDIO_RELAY, LOW);
digitalWrite(LEFT_AUDIO_RELAY, LOW);
digitalWrite(GROUND_AUDIO_RELAY, LOW);
digitalWrite(AMP_RELAY, LOW);
}
//--------------Print Directory Function------------------------------
// File listing helper
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}