Hello all,
First, allow me to thank all whom have previous assisted me. My project is almost done. (Kirk Chair build from ST-TOS)
Right arms is good to go - many thanks to ec2021.
Now onto the left arm...
I have a bank of 9 LEDs for the top. And a bank of 8 rocker switches to control all. AT mega 2560 with a Adafruit MP3 music maker that stores all the sound files. 8 rocker switches to control everything:
Rocker 1 (RED SWITCH) - changes the mode from Mode 1 to Mode 2 (Mode 1 is the default when the Serial monitor reads it as 0, LOW, turned off, etc)
Rockers 2-7 (Yellow thru Gray) play files from different play lists - so keeping track of the switch state and plays the next file whenever the rocker is changed.
Rocker 8 (BLACK) - turns on ambient background effects from the next MP3 in the playlist every time it is turned on, regardless of what mode/position Rocker 1 is in. When it is turned off, the ambient sound doesn't play.
I have the scripts for both Mode 1 and 2 done, and they work good - just cant figure out how to combine the two. I've listed my sketches for both Mode 1 and Mode 2 below, with everything in a loop, using if (Red = 0) to try to change between the different modes, which is defintenly not working - plus, I cant quite figure out the ambient noise drive by the Black switch.
Any help is appreciated.
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#undef 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;
};
//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 = !state;
return true;
} else return false;
}
// End of Struct to handle the switches
// Definition of Switches
const byte NoOfSwitches = 8;
SwitchType Switch[NoOfSwitches]; // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31, 33 ,35, 37, 39, 41, 43, 45}; // pins of the switches
enum {Red, Yellow, Blue, White, LtBlue, Green, Gray, Black}; // Enumeration that keeps track of the human switch naming
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:
pinMode (26, OUTPUT);
pinMode (28, OUTPUT);
pinMode (30, OUTPUT);
pinMode (32, OUTPUT);
pinMode (34, OUTPUT);
pinMode (36, OUTPUT);
pinMode (38, OUTPUT);
pinMode (40, OUTPUT);
pinMode (42, OUTPUT);
// setup Pin Modes
for (int i=0; i<NoOfSwitches;i++){
Switch[i].No = i+1;
Switch[i].setPin(SwitchPin[i]);
}
}
void loop() {
if (Red = 0) {
digitalWrite(26, HIGH);
digitalWrite(28, HIGH);
digitalWrite(30, HIGH);
digitalWrite(32, HIGH);
digitalWrite(34, HIGH);
digitalWrite(36, HIGH);
digitalWrite(38, HIGH);
digitalWrite(40, HIGH);
digitalWrite(42, HIGH);
handleYellow();
handleBlue();
handleWhite();
handleLtBlue();
handleGreen();
handleGray();
}
void handleYellow(){
if (Switch[Yellow].changed()){
if (Switch[Yellow].state) {
playViewScreen(false);
}
else {
playViewScreen(false);
}
}
}
void handleBlue(){
if (Switch[Blue].changed()){
if (Switch[Blue].state) {
playAstrogator(false);
}
else {
playAstrogator(false);
}
}
}
void handleWhite(){
if (Switch[White].changed()){
if (Switch[White].state) {
playSensorTrack(false);
}
else {
playSensorTrack(false);
}
}
}
void handleLtBlue(){
if (Switch[LtBlue].changed()){
if (Switch[LtBlue].state) {
playSickbayTrack(false);
}
else {
playSickbayTrack(false);
}
}
}
void handleGreen(){
if (Switch[Green].changed()){
if (Switch[Green].state) {
playTransporterTrack(false);
}
else {
playTransporterTrack(false);
}
}
}
void handleGray(){
if (Switch[Gray].changed()){
if (Switch[Gray].state) {
playMainTitleTrack(false);
}
else {
playMainTitleTrack(false);
}
}
}
void playViewScreen(boolean Restart) {
static int ViewScreenOutput = 1;
if (Restart) {
ViewScreenOutput = 1;
return;
}
if (ViewScreenOutput > 2) ViewScreenOutput = 1;
switch (ViewScreenOutput) {
case (1):
StartMP3("track001.mp3");
break;
case (2):
StartMP3("track002.mp3");
break;
}
ViewScreenOutput ++;
}
void playAstrogator(boolean Restart) {
static int AstrogatorOutput = 1;
if (Restart) {
AstrogatorOutput = 1;
return;
}
if (AstrogatorOutput > 2) AstrogatorOutput = 1;
switch (AstrogatorOutput) {
case (1):
StartMP3("track003.mp3");
break;
case (2):
StartMP3("track004.mp3");
break;
}
AstrogatorOutput ++;
}
void playSensorTrack(boolean Restart) {
static int SensorOutput = 1;
if (Restart) {
SensorOutput = 1;
return;
}
if (SensorOutput > 4) SensorOutput = 1;
switch (SensorOutput) {
case (1):
StartMP3("track017.mp3");
break;
case (2):
StartMP3("track018.mp3");
break;
case (3):
StartMP3("track019.mp3");
break;
case (4):
StartMP3("track020.mp3");
break;
}
SensorOutput ++;
}
void playSickbayTrack(boolean Restart) {
static int SickbayOutput = 1;
if (Restart) {
SickbayOutput = 1;
return;
}
if (SickbayOutput > 4) SickbayOutput = 1;
switch (SickbayOutput) {
case (1):
StartMP3("track021.mp3");
break;
case (2):
StartMP3("track022.mp3");
break;
case (3):
StartMP3("track023.mp3");
break;
case (4):
StartMP3("track024.mp3");
break;
}
SickbayOutput ++;
}
void playTransporterTrack(boolean Restart) {
static int TransporterOutput = 1;
if (Restart) {
TransporterOutput = 1;
return;
}
if (TransporterOutput > 3) TransporterOutput = 1;
switch (TransporterOutput) {
case (1):
StartMP3("track025.mp3");
break;
case (2):
StartMP3("track026.mp3");
break;
case (3):
StartMP3("track027.mp3");
break;
}
TransporterOutput ++;
}
void playMainTitleTrack(boolean Restart) {
static int MainTitleOutput = 1;
if (Restart) {
MainTitleOutput = 1;
return;
}
if (MainTitleOutput > 3) MainTitleOutput = 1;
switch (MainTitleOutput) {
case (1):
StartMP3("track028.mp3");
break;
case (2):
StartMP3("track029.mp3");
break;
case (3):
StartMP3("track030.mp3");
break;
}
MainTitleOutput ++;
}
} else {
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
delay(random(25,1000));
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);
handleYellow();
handleBlue();
handleWhite();
handleLtBlue();
handleGreen();
handleGray();
}
void handleYellow(){
if (Switch[Yellow].changed()){
if (Switch[Yellow].state) {
playShieldsKlingon(false);
}
else {
playShieldsKlingon(false);
}
}
}
void handleBlue(){
if (Switch[Blue].changed()){
if (Switch[Blue].state) {
StartMP3("track005.mp3");
}
else {
StartMP3("track005.mp3");
}
}
}
void handleWhite(){
if (Switch[White].changed()){
if (Switch[White].state) {
StartMP3("track006.mp3");
}
else {
StartMP3("track006.mp3");
}
}
}
void handleLtBlue(){
if (Switch[LtBlue].changed()){
if (Switch[LtBlue].state) {
playEngineeringTrack(false);
}
else {
playEngineeringTrack(false);
}
}
}
void handleGreen(){
if (Switch[GreenT].changed()){
if (Switch[GreenT].state) {
playDestructTrack(false);
}
else {
playDestructTrack(false);
}
}
}
void handleGray(){
if (Switch[Gray].changed()){
if (Switch[Gray].state) {
playMusicTrack(false);
}
else {
playMusicTrack(false);
}
}
}
void playShieldsKlingon(boolean Restart) {
static int ShieldsKlingonOutput = 1;
if (Restart) {
ShieldsKlingonOutput = 1;
return;
}
if (ShieldsKlingonOutput > 2) ShieldsKlingonOutput = 1;
switch (ShieldsKlingonOutput) {
case (1):
StartMP3("track008.mp3");
break;
case (2):
StartMP3("track007.mp3");
break;
}
ShieldsKlingonOutput ++;
}
void playEngineeringTrack(boolean Restart) {
static int EngineeringOutput = 1;
if (Restart) {
EngineeringOutput = 1;
return;
}
if (EngineeringOutput > 4) EngineeringOutput = 1;
switch (EngineeringOutput) {
case (1):
StartMP3("track031.mp3");
break;
case (2):
StartMP3("track032.mp3");
break;
case (3):
StartMP3("track033.mp3");
break;
case (4):
StartMP3("track034.mp3");
break;
}
EngineeringOutput ++;
}
void playDestructTrack(boolean Restart) {
static int DestructOutput = 1;
if (Restart) {
DestructOutput = 1;
return;
}
if (DestructOutput > 4) DestructOutput = 1;
switch (DestructOutput) {
case (1):
StartMP3("track035.mp3");
break;
case (2):
StartMP3("track036.mp3");
break;
case (3):
StartMP3("track037.mp3");
break;
case (4):
StartMP3("track038.mp3");
break;
}
DestructOutput ++;
}
void playMusicTrack(boolean Restart) {
static int MusicOutput = 1;
if (Restart) {
MusicOutput = 1;
return;
}
if (MusicOutput > 22) MusicOutput = 1;
switch (MusicOutput) {
case (1):
StartMP3("track039.mp3");
break;
case (2):
StartMP3("track040.mp3");
break;
case (3):
StartMP3("track041.mp3");
break;
case (4):
StartMP3("track042.mp3");
break;
case (5):
StartMP3("track043.mp3");
break;
case (6):
StartMP3("track044.mp3");
break;
case (7):
StartMP3("track045.mp3");
break;
case (8):
StartMP3("track046.mp3");
break;
case (9):
StartMP3("track047.mp3");
break;
case (10):
StartMP3("track048.mp3");
break;
case (11):
StartMP3("track048.mp3");
break;
case (12):
StartMP3("track050.mp3");
break;
case (13):
StartMP3("track051.mp3");
break;
case (14):
StartMP3("track052.mp3");
break;
case (15):
StartMP3("track053.mp3");
break;
case (16):
StartMP3("track054.mp3");
break;
case (17):
StartMP3("track055.mp3");
break;
case (18):
StartMP3("track056.mp3");
break;
case (19):
StartMP3("track057.mp3");
break;
case (20):
StartMP3("track058.mp3");
break;
case (21):
StartMP3("track059.mp3");
break;
case (22):
StartMP3("track060.mp3");
break;
}
MusicOutput ++;
}
}
}
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
}

