// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#define TEST
// 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);
// Begin of Struct to handle the switches
struct SwitchType {
byte No;
byte pin;
boolean state = false;
void setPin(byte aPin);
boolean changed();
unsigned long lastChange = 0;
byte lastState;
bool changeOk = false;
};
void SwitchType::setPin(byte aPin){
pin = aPin;
pinMode(pin,INPUT_PULLUP);
lastState = digitalRead(pin);
}
boolean SwitchType::changed(){
byte actState = digitalRead(pin);
if (lastState != actState) {
lastState = actState;
lastChange = millis();
changeOk = true;
}
if (millis()-lastChange > 30 && changeOk){
changeOk = false;
state = !state;
return true;
} else return false;
}
// End of Struct to handle the switches
// begin of Struct to handle the LEDs
struct LEDType {
byte pin;
byte state;
boolean DoFlash = false;
uint16_t FlashTime = 500;
unsigned long lastFlash = 0;
void setPin(byte aPin);
void setFlashtime(uint16_t aFlashTime);
void off();
void on();
void handleFlash();
};
void LEDType::setPin(byte aPin){
pin = aPin;
pinMode(pin,OUTPUT);
off();
}
void LEDType::on(){
state = HIGH;
digitalWrite(pin, state);
}
void LEDType::off(){
state = LOW;
digitalWrite(pin, state);
}
void LEDType::handleFlash(){
if (DoFlash) {
if(millis()-lastFlash > FlashTime){
lastFlash = millis();
if (state) off();
else on();
}
}
}
void LEDType::setFlashtime(uint16_t aFlashTime){
FlashTime = aFlashTime;
}
// end of Struct to handle the LEDs
// Definition of Switches
const byte NoOfSwitches = 6;
SwitchType Switch[NoOfSwitches]; // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {35 ,37, 39, 41, 31, 43}; // pins of the switches
enum {No1, No2, No3, No4, No5, Reed}; // Enumeration that keeps track of the human switch numbering
// Definition of LEDs
const byte NoOfLEDs = 6;
LEDType LED[NoOfLEDs]; // Declaration of the LEDs
byte LEDPin[NoOfLEDs] = {28,26,30,32,34,36}; // pins of the LEDs
enum {CommLight, Switch5Light, Switch1Light, Switch2Light, Switch3Light, Switch4Light}; // Enumeration that keeps track of the human LED names
void setup() {
Serial.begin(115200);
Serial.println("Adafruit VS1053 Simple Test");
#ifdef TEST
// No musicplayer, no SD card required
#else
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"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
#endif
// put your setup code here, to run once:
// setup Pin Modes
for (int i=0; i<NoOfSwitches;i++){
Switch[i].No = i+1;
Switch[i].setPin(SwitchPin[i]);
}
for (int i=0; i<NoOfLEDs;i++){
LED[i].setPin(LEDPin[i]);
}
LED[Switch1Light].setFlashtime(800); // Just to demonstrate the use of this ...
}
boolean DoFlash = false;
void loop() {
handleSwitchNo5();
handleSwitchNo1();
handleSwitchNo2();
handleSwitchNo3();
handleSwitchNo4();
handleReed();
//playCommTrack();
//playComputerTrack();
LED[Switch1Light].handleFlash();
LED[Switch2Light].handleFlash();
LED[Switch3Light].handleFlash();
LED[Switch4Light].handleFlash();
}
void handleReed(){
// Just a dummy read on the reed switch
// which takes care that Switch.[Reed].state
// shows the actual switch state
if (Switch[Reed].changed()) {
// Uncomment the following lines
// if you want the playlists to be reset
// by a change of Reed state
//playCommTrack(true);
//playComputerTrack(true);
};
}
void handleSwitchNo5(){
if(Switch[No5].changed()) {
if (Switch[No5].state) {
LED[CommLight].on();
LED[Switch5Light].on();
}
else {
LED[CommLight].off();
LED[Switch5Light].off();
}
}
}
void handleSwitchNo1(){
if (Switch[No1].changed()){
if (Switch[No1].state) {
StartMP3("track001.mp3");
Serial.println("Flash!");
LED[Switch1Light].DoFlash = true;
}
else {
LED[Switch1Light].off();
LED[Switch1Light].DoFlash = false;
StartMP3("track005.mp3");
Serial.println("No Flash!");
}
}
}
void handleSwitchNo2(){
if (Switch[No2].changed()){
if (Switch[No2].state) {
StartMP3("track002.mp3");
Serial.println("Flash!");
LED[Switch2Light].DoFlash = true;
}
else {
LED[Switch2Light].off();
LED[Switch2Light].DoFlash = false;
StartMP3("track006.mp3");
Serial.println("No Flash!");
}
}
}
void handleSwitchNo3(){
if (Switch[No3].changed()){
if (Switch[No3].state) {
StartMP3("track004.mp3");
Serial.println("Flash!");
LED[Switch3Light].DoFlash = true;
}
else {
LED[Switch3Light].off();
LED[Switch3Light].DoFlash = false;
StartMP3("track007.mp3");
Serial.println("No Flash!");
}
}
}
void handleSwitchNo4(){
if (Switch[No4].changed()){
if (Switch[No4].state) {
if (Switch[Reed].state) playCommTrack(false);
else playComputerTrack(false);
Serial.println("Flash! No4");
LED[Switch4Light].DoFlash = true;
}
else {
LED[Switch4Light].off();
LED[Switch4Light].DoFlash = false;
StartMP3("track008.mp3");
Serial.println("No Flash!");
}
}
}
void playCommTrack(boolean Restart) {
static int commOutput = 1;
if (Restart) {
commOutput = 1;
return;
}
if ( commOutput > 17) commOutput = 1;
switch (commOutput) {
case (1):
StartMP3("track027.mp3");
break;
case (2):
StartMP3("track028.mp3");
break;
case (3):
StartMP3("track029.mp3");
break;
case (4):
StartMP3("track030.mp3");
break;
case (5):
StartMP3("track031.mp3");
break;
case (6):
StartMP3("track032.mp3");
break;
case (7):
StartMP3("track033.mp3");
break;
case (8):
StartMP3("track034.mp3");
break;
case (9):
StartMP3("track035.mp3");
break;
case (10):
StartMP3("track036.mp3");
break;
case (11):
StartMP3("track037.mp3");
break;
case (12):
StartMP3("track038.mp3");
break;
case (13):
StartMP3("track039.mp3");
break;
case (14):
StartMP3("track040.mp3");
break;
case (15):
StartMP3("track041.mp3");
break;
case (16):
StartMP3("track042.mp3");
break;
case (17):
StartMP3("track043.mp3");
break;
}
commOutput ++;
}
void playComputerTrack(boolean Restart) {
static int computerOutput = 1;
if (Restart) {
computerOutput = 1;
return;
}
if (computerOutput > 12) computerOutput = 1;
switch (computerOutput) {
case (1):
StartMP3("track014.mp3");
break;
case (2):
StartMP3("track015.mp3");
break;
case (3):
StartMP3("track016.mp3");
break;
case (4):
StartMP3("track017.mp3");
break;
case (5):
StartMP3("track018.mp3");
break;
case (6):
StartMP3("track019.mp3");
break;
case (7):
StartMP3("track020.mp3");
break;
case (8):
StartMP3("track021.mp3");
break;
case (9):
StartMP3("track022.mp3");
break;
case (10):
StartMP3("track023.mp3");
break;
case (11):
StartMP3("track024.mp3");
break;
case (12):
StartMP3("track025.mp3");
break;
}
computerOutput ++;
}
void StartMP3(char *aFilename){
#ifdef TEST
Serial.print("Playing ");
Serial.println(aFilename);
#else
if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
musicPlayer.startPlayingFile(aFilename);
#endif
}