Hello, sometimes you cant see the forest for the trees.
Working on a 1/350 scale model of the Enterprise with a friend. He wants lights and sound effects. Everything will be lit with 5v LEDs, and using a Music Maker Shield to play MP3's. Here's the sequence:
On power up, play a startup noise ONE TIME (track001, works good)
Rocker switch, two positions, starts in the OFF position.
When you close the rocker, it plays a startup sequence and audio track002 (handleSwitchCruise), and lights up the LEDs in sequence with a few second delays in between them. (should be the IF portion of handleSwitchCruise). After that, it should continiusly play a bridge sound effect as a loop (which it does).
When you open the rocker, it should play track022, and then after a second or two, shut off all the LEDs (the ELSE portion of handleSwitchCruise). The background bridge noise should NOT play here (which it does).
Funny, everything works - but my IF and ELSEs are backwards. It does the ELSE section when you close the rocker, and then the IF portion when you open it again. Im sure its something stupid and minor, I just cant see it.
// include SPI, MP3 and SD libraries
#undef TEST
#ifdef TEST
// Do not use the swpcific hardware to play sound
// use Serial.println() instead
#else
#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);
// Set volume for left, right channels. lower numbers == louder volume!
// musicPlayer.setVolume(1,1);
#endif
// 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;
};
//define the Switch Type
void SwitchType::setPin(byte aPin) {
pin = aPin;
pinMode(pin, INPUT_PULLUP);
lastState = digitalRead(pin);
}
//Define the SwitchType changed
boolean SwitchType::changed() {
byte actState = digitalRead(pin);
if (lastState != actState) {
lastState = actState;
lastChange = millis();
changeOk = true;
}
if (millis() - lastChange > 30 && changeOk) {
changeOk = false;
state = lastState;
return true;
} else return false;
}
// End of Struct to handle the switches
// Definition of Switches
const byte NoOfSwitches = 1;
SwitchType Switch[NoOfSwitches]; // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {22}; // pins of the switches
enum {Cruise}; // Enumeration that keeps track of the human switch naming
void setup() {
// put your setup code here, to run once:
pinMode (26, OUTPUT);
pinMode (28, OUTPUT);
pinMode (30, OUTPUT);
pinMode (32, OUTPUT);
pinMode (34, OUTPUT);
pinMode (36, OUTPUT);
pinMode (38, OUTPUT);
// setup Pin Modes
for (int i = 0; i < NoOfSwitches; i++) {
Switch[i].No = i + 1;
Switch[i].setPin(SwitchPin[i]);
}
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"));
// Set volume for left, right channels. lower numbers == louder volume!
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(1,1
);
#endif
}
//decalre variables
bool startup = false;
void loop() {
// put your main code here, to run repeatedly:
// Play the Startup sound one time on power up
if (startup == false) {
Serial.println(F("Playing track 001"));
musicPlayer.playFullFile("/track001.mp3");
startup = true;
}
handleSwitchCruise();
ResumeBridgeTrack();
}
void handleSwitchCruise(){
if (Switch[Cruise].changed()) {
if ( Switch[Cruise].state ) {
StartMP3("track002.mp3");
delay(1000);
digitalWrite(26, HIGH);
delay(1750);
digitalWrite(28, HIGH);
delay(2200);
digitalWrite(30, HIGH);
delay(4000);
digitalWrite(32, HIGH);
delay(4500);
digitalWrite(34, HIGH);
delay(10000);
digitalWrite(36, HIGH);
delay(11000);
digitalWrite(38, HIGH);
}
else
{
StartMP3("track022.mp3");
delay(5000);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
}
}
{
};
}
void StartMP3(char *aFilename){
#ifdef TEST
Serial.print("Playing ");
Serial.println(aFilename);
#else
if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
musicPlayer.startPlayingFile(aFilename);
musicPlayer.setVolume(1,1);
#endif
}
void ResumeBridgeTrack(){
if (musicPlayer.stopped() && Switch[Cruise].state ) StartMP3("track003.mp3");
}