// 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);
//define switches
const char Switch5 = 31;
const char Switch1 = 35;
//define global variables
bool pressed = false;
int dt = 500;
unsigned long flash = 800;
int CommLightPin = 28;
int Switch5LightPin = 26;
int Switch1LightPin = 30;
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
}
#endif
// put your setup code here, to run once:
// setup Pin Modes
pinMode(Switch5, INPUT_PULLUP);
pinMode(Switch1, INPUT_PULLUP);
pinMode (CommLightPin, OUTPUT);
pinMode (Switch5LightPin, OUTPUT);
pinMode (Switch1LightPin, OUTPUT);
}
boolean Switch5State = false;
boolean Switch1State = false;
boolean DoFlash = false;
void loop() {
//read Switch5
if(Switch5Changed()) {
Switch5State = !Switch5State;
if (Switch5State) {
digitalWrite (CommLightPin, HIGH);
digitalWrite (Switch5LightPin, HIGH);
}
else {
digitalWrite (CommLightPin, LOW);
digitalWrite (Switch5LightPin, LOW);
}
}
//read Switch1
if (Switch1Changed()){
Switch1State = !Switch1State;
if (Switch1State) {
#ifdef TEST
// No musicplayer required
#else
musicPlayer.playFullFile("track001.mp3");
#endif
DoFlash = true;
Serial.println("Flash!");
}
else {
digitalWrite (Switch1LightPin, LOW);
DoFlash = false;
Serial.println("No Flash!");
}
}
if (DoFlash) Flash();
}
void Flash(){
static unsigned long lastFlash = 0;
if(millis()-lastFlash > flash){
lastFlash = millis();
digitalWrite(Switch1LightPin,!digitalRead(Switch1LightPin));
}
}
boolean Switch1Changed(){
static unsigned long lastChange = 0;
static byte lastState = digitalRead(Switch1);
static bool changeOk = false;
byte state = digitalRead(Switch1);
if (lastState != state) {
lastState = state;
lastChange = millis();
changeOk = true;
}
if (millis()-lastChange > 30 && changeOk){
changeOk = false;
return true;
} else return false;
}
boolean Switch5Changed(){
static unsigned long lastChange = 0;
static byte lastState = digitalRead(Switch5);
static bool changeOk = false;
byte state = digitalRead(Switch5);
if (lastState != state) {
lastState = state;
lastChange = millis();
changeOk = true;
}
if (millis()-lastChange > 30 && changeOk){
changeOk = false;
return true;
} else return false;
}