Hardware:
-Arduino Uno
-VS1053 Music Maker Shield w/3 amp
-TSAL6200 IR LED transmitting to a Lego power functions IR receiver
-39 ohm resister connected from IR LED negative (-) leg to Ground Pin on Arduino Uno
Software:
Adafruit VS1053 Library example sketch: player_interrupts
Code: Select all
/*
Lego train TSAL6200 IR LED transmitter:
-Positve (+) LED leg connected to Pin 13 on Arduino Uno through the Music Maker Shield Pin 13
-Negitive (-) LED leg connected to 39 ohm resister to Ground Pin on Arduino Uno through the Music Maker Shield Pin 13
*/
//Start of MP3
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// 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
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//End of MP3
//Start of Lego Train
#include <legopowerfunctions.h>
LEGOPowerFunctions lego(13); //Power Pin
//End of Lego
void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Library Test");
// initialise the music player
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"));
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
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!
musicPlayer.setVolume(20, 20);
/***** Two interrupt options! *******/
// This option uses timer0, this means timer1 & t2 are not required
// (so you can use 'em for Servos, etc) BUT millis() can lose time
// since we're hitchhiking on top of the millis() tracker
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
// This option uses a pin interrupt. No timers required! But DREQ
// must be on an interrupt pin. For Uno that's Digital #2 or #3
// *** This method is preferred
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
}
void loop() {
delay (1000); //Only temporary during testing phase
musicPlayer.startPlayingFile("/track001.mp3");
Serial.println(F("Started playing"));
Serial.println(F("Train is on the move"));
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 :)
lego.SingleOutput(0, PWM_FWD1, RED, CH1); //Sends signal to IR LED to move train forward (Train will continue to move while song plays)
Serial.print("."); //Continue to print while song plays
delay(1000);
}
if (musicPlayer.stopped()) {
Serial.println(F("Done playing music"));
lego.SingleOutput(0, PWM_FLT, RED, CH1); //Sends signal to IR LED to stop train (Train will stop at the end of song completion)
Serial.println(F("Train is at the station"));
while (1) {
delay(10); // we're done! do nothing...
}
}
}
/// 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();
}
}