Running Different Scripts via Rocker Switch

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   
}

You are just nesting statements and can do it a number of ways. Either by state machine or nested while loops etc. The code that is associated with each rocker is irrelevant (more or less) what you need to do is write out the logic of the rockers, a diagram can be useful. I don't know what you mean with rocker 2-7 as if they are all in mode 1 function then there could be a very large number of variables.
If you use while loops then just put rocker 8 function in every one. if state machines and you are still running through loop each time then just put it in loop and it will function regardless of anything else

Here's a diagram that might more sense of what I am doing...

Since all the rockers must be either one or the other (unless 3 pole) then I don’t know how you can have it set up like that. In the arrangement it will play multiple things at once.

Just imagine any arrangement of switches and think what the code will do. If all switches are to the left it will me mode 1 for all of them and the code associated with mode 1 will run

Not necessarily. One can prioritise the rockers and only the highest priority one will be acted on.

Simple demo code, rockers wired between pin and GND.

// pin to select mode 1 or mode 2
const uint8_t modePin = 31;
// priority; last pin will have highest priority
const uint8_t rockerPins[] = { 33, 35, 37, 39, 41, 43, 45 };


void setup()
{
  Serial.begin(115200);

  pinMode(modePin, INPUT_PULLUP);
  for (uint8_t cnt = 0; cnt < sizeof(rockerPins); cnt++)
  {
    pinMode(rockerPins[cnt], INPUT_PULLUP);
  }
}

void loop()
{
  // selected rocker (highest priority); 0xFF indicates non selected
  uint8_t selectedRocker = 0xFF;
  // selected mode
  uint8_t mode;

  mode = digitalRead(modePin);

  for (uint8_t cnt = 0; cnt < sizeof(rockerPins); cnt++)
  {
    uint8_t rockerStatus = digitalRead(rockerPins[cnt]);
    if (rockerStatus == LOW)
    {
      selectedRocker = cnt;
    }
  }

  Serial.print(F("Selected mode =   "));
  Serial.println(mode) + 1;
  Serial.print(F("Selected rocker = "));
  if (selectedRocker == 0xFF)
  {
    Serial.println(F("None"));
  }
  else
  {
    Serial.println(selectedRocker + 1);
  }
}

Well, got close, not 100% of what Im looking for but it will work until I can figure out the last parts:

void loop
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
delay(random(25,1000));
digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);

Only do that when Switch(Red) hasnt been changed - otherwise set the int leds to all HIGH.

For the playBridgeTrack, loop that playlist constantly after Switch(Black has been changed.

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#undef TEST

#define HIGH 0x1
#define LOW  0x0

// 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

// Variable for the LEDs used
int leds[9] = {26,28,30,32,34,36,38,40,42};

int OPStatus = LOW;

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 jj; jj<sizeof(leds)/sizeof(int);jj++){
    pinMode(leds[jj],OUTPUT);
    delay(10);
  }
}

void loop() {
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
  delay(random(25,1000));
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW); 
  handleSwitchRed();
  handleSwitchYellow();
  handleSwitchBlue();
  handleSwitchWhite();
  handleSwitchLtBlue();
  handleSwitchGreen();
  handleSwitchGray();  
  handleSwitchBlack();

}

void handleSwitchRed(){
  if (Switch[Red].changed()) {
   if (Switch[Red].state ) musicPlayer.playFullFile("track061.mp3");
   else StartMP3("track062.mp3");
   } 
 {
  };
}

void handleSwitchYellow(){
  if (Switch[Yellow].changed()){
     if (Switch[Yellow].state) {
      if (Switch[Red].state) playViewScreen(false);
                       else       playShieldsKlingon(false);

    }
    else {
 StartMP3("track062.mp3");
    }
  }  
}

void handleSwitchBlue(){
  if (Switch[Blue].changed()){
     if (Switch[Blue].state) {
      if (Switch[Red].state) playAstrogator(false);
                       else       StartMP3("track005.mp3");

    }
    else {
  StartMP3("track062.mp3");
    }
  }  
}

void handleSwitchWhite(){
  if (Switch[White].changed()){
     if (Switch[White].state) {
      if (Switch[Red].state) playSensorTrack(false);
                       else       StartMP3("track006.mp3");

    }
    else {
  StartMP3("track062.mp3");
    }
  }  
}

void handleSwitchLtBlue(){
  if (Switch[LtBlue].changed()){
     if (Switch[LtBlue].state) {
      if (Switch[Red].state) playSickbayTrack(false);
                       else       playEngineeringTrack(false);

    }
    else {
  StartMP3("track062.mp3");
    }
  }  
}

void handleSwitchGreen(){
  if (Switch[Green].changed()){
     if (Switch[Green].state) {
      if (Switch[Red].state) playTransporterTrack(false);
                       else       playDestructTrack(false);

    }
    else {
  StartMP3("track062.mp3");
    }
  }  
}

void handleSwitchGray(){
  if (Switch[Gray].changed()){
     if (Switch[Gray].state) {
      if (Switch[Red].state) playMusicTrack(false);
                       else       playMainTitleTrack(false);

    }
    else {
 StartMP3("track062.mp3"); 
    }
  }  
}

void handleSwitchBlack(){
  if (Switch[Black].changed()){
     if (Switch[Black].state) {
      playBridgeTrack(false);
    }
    else {
      StartMP3("track062.mp3");
    }
  }  
}

void handleRed(){
  if (Switch[Red].changed()) {
   if ( Switch[Red].state ) musicPlayer.playFullFile("track061.mp3");
   else StartMP3("track062.mp3");
   } 
 {
  };
}


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 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 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 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 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 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 ++;
} 

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 playBridgeTrack(boolean Restart) {
  static int BridgeOutput = 1;
  if (Restart) {
    BridgeOutput = 1;
    return;
  }
  if (BridgeOutput > 8) BridgeOutput = 1;
  switch (BridgeOutput) {
    case (1):
      StartMP3("track009.mp3");
      break;
    case (2):
      StartMP3("track010.mp3");
      break;
    case (3):
      StartMP3("track011.mp3");
      break;
    case (4):
      StartMP3("track012.mp3");
      break;
    case (5):
      StartMP3("track013.mp3");
      break;
    case (6):
      StartMP3("track014.mp3");
      break;
    case (7):
      StartMP3("track015.mp3");
      break;
    case (8):
      StartMP3("track016.mp3");
      break;
                                         
  }
  BridgeOutput ++;
} 



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   
}

Hi @dylaninwv,

I have taken your code and modified it to work in Wokwi in a test mode (using serial prints) so that you can check the general way it works.

However, please be aware that playFullFile() due to Adafruit

  • playFullFile(): Play the complete file. This function will not return until the playback is complete.

should not return to the sketch and therefore would not accept any inputs during playback time.
If this is the case and it is not wanted by you replace the call playFullFile() by playFile().

Main changes done by me:

  • Introduced a structure for PlayLists including some simple struct internal functions
  • Added two functions to handle LEDs (especially with a millis() driven function for blinking) as blinking with delay() negatively influences the response on inputs
  • Changed StartMP3(char *aFilename) to playFile(int Number) to remove unnecessary strings from the code (as long as you stick to your "rules" for track names).

Here is the Wokwi Simulation

And here the code

// include SPI, MP3 and SD libraries
#define 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);
#endif

// Structure that holds the relevant playlist data
// It provides the functions to increment the index
// and to restart from the beginning
struct PlayListType {
  String  Name;             // Name of the playlist (for humans)
  int     StartFileNo;      // The number of the first file in the list
  int     MaxFiles;         // The numbers of sequential files in the list
  int LastFilePlayed;       // The last index (offset to StartFileNo) played
  void  Increment();        // Function that increments the index and avoids "overflow"
  void Restart();           // Function to reset the index to zero 
};

// This defines what the Increment() function does
// Inside this function the variables of the struct
// can be used directly
void PlayListType::Increment(){
   LastFilePlayed += 1;
   if (LastFilePlayed >= MaxFiles) { LastFilePlayed = 0;};
}

// Just to avoid that you have to cope with struct content 
// in your sketch directly. 
// MainTitle.Reset();
// is identical to
// MainTitle.LastFilePlayed  = 0;
void PlayListType::Restart(){
   LastFilePlayed = 0;
}

// Now the declaration of the playlists with 
// PlayListType PlayListName {"Name", StartFileNo, MaxFiles, LastFilePlayed}; 
PlayListType ShieldKlingon {"ShieldsKlingon", 8, 2 , 0};
PlayListType ViewScreen {"ViewScreen", 1, 2, 0};
PlayListType Astrogator {"Astrogator",  3, 2, 0};
PlayListType SensorTrack {"SensorTrack",  17, 4, 0};
PlayListType SickbayTrack {"SickbayTrack",  21, 4, 0};
PlayListType EngineeringTrack {"EngineeringTrack", 31, 4, 0};
PlayListType TransporterTrack {"TransporterTrack", 25, 3, 0};
PlayListType DestructTrack {"DestructTrack", 35, 4, 0};
PlayListType MainTitle {"MainTitle",   28,  3, 0};
PlayListType MusicTrack {"MusicTrack",  39, 22, 0};
PlayListType BridgeTrack {"BridgeTrack", 9, 8, 0};

// This function takes one of those playlist structures and 
// creates the appropriate filename "online"
// and uses the common function startPlayingFile()
void playList(PlayListType &pl, boolean restart){
   if (restart) pl.Restart();
   playFile(pl.StartFileNo+pl.LastFilePlayed);
   pl.Increment();
}

// 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

// Variable for the LEDs used
int leds[9] = {26,28,30,32,34,36,38,40,42};

int OPStatus = LOW;

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 jj; jj<sizeof(leds)/sizeof(int);jj++){
    pinMode(leds[jj],OUTPUT);
    delay(10);
  }
}

void loop() {
  if (Switch[Red].state) blinkLEDs();
                   else setLEDsTo(HIGH);
  handleSwitchRed();
  handleSwitchYellow();
  handleSwitchBlue();
  handleSwitchWhite();
  handleSwitchLtBlue();
  handleSwitchGreen();
  handleSwitchGray();  
  handleSwitchBlack();
}

void setLEDsTo(byte state){
  for (int i=0;i<sizeof(leds)/sizeof(int);i++)
    digitalWrite(leds[i],state);
}

void blinkLEDs(){
 static long int delayTime = 0;
 static long int lastSwitchTime = 0;
 static byte state = HIGH;
  if (millis()-lastSwitchTime > delayTime) {
    lastSwitchTime = millis();
    delayTime      = random(25,1000);
    digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],state);
    state = !state;
  }
}


void playFile(int Number){
   char aFilename[80];
   sprintf(aFilename,"track%03d.mp3", Number);
#ifdef TEST 
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
   if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
   musicPlayer.startPlayingFile(aFilename);
   musicPlayer.setVolume(1,1);
#endif   
}

void playFullFile(int Number){
   char aFilename[80];
   sprintf(aFilename,"track%03d.mp3", Number);
#ifdef TEST
  Serial.print("Playing Fullfile ");
  Serial.println(aFilename);
  delay(5000);  // Simulates a playbacktime of 5 seconds ... 
#else
    musicPlayer.playFullFile(aFilename);
#endif
}

void handleSwitchRed(){
  if (Switch[Red].changed()) {
   if (Switch[Red].state ) playFullFile(61);
                      else playFile(62);
  };
}

void handleSwitchYellow(){
  if (Switch[Yellow].changed()){
     if (Switch[Yellow].state) {
      if (Switch[Red].state) playList(ViewScreen,false);
                       else  playList(ShieldKlingon,false);

    }
    else {
       playFile(62);
    }
  }  
}

void handleSwitchBlue(){
  if (Switch[Blue].changed()){
     if (Switch[Blue].state) {
      if (Switch[Red].state) playList(Astrogator,false);
                       else  playFile(5);

    }
    else {
      playFile(62);
    }
  }  
}

void handleSwitchWhite(){
  if (Switch[White].changed()){
     if (Switch[White].state) {
      if (Switch[Red].state) playList(SensorTrack,false);
                       else  playFile(6);

    }
    else {
       playFile(62);
    }
  }  
}

void handleSwitchLtBlue(){
  if (Switch[LtBlue].changed()){
     if (Switch[LtBlue].state) {
      if (Switch[Red].state) playList(SickbayTrack,false);
                       else  playList(EngineeringTrack,false);

    }
    else {
       playFile(62);
    }
  }  
}

void handleSwitchGreen(){
  if (Switch[Green].changed()){
     if (Switch[Green].state) {
      if (Switch[Red].state) playList(TransporterTrack,false);
                       else  playList(DestructTrack,false);

    }
    else {
       playFile(62);
    }
  }  
}

void handleSwitchGray(){
  if (Switch[Gray].changed()){
     if (Switch[Gray].state) {
      if (Switch[Red].state) playList(MusicTrack,false);
                       else  playList(MainTitle,false);

    }
    else {
       playFile(62); 
    }
  }  
}

void handleSwitchBlack(){
  if (Switch[Black].changed()){
     if (Switch[Black].state) {
      playList(BridgeTrack, false);
    }
    else {
      playFile(62);
    }
  }  
}

As mentioned in our private conversation the separate, sequential evaluation of each different switch may raise unexpected results, especially when switches are changing states "in parallel".

To avoid this you could gather the switch positions (On/Off) as a bit array (one byte is sufficient for all 8 switches) and evaluate changes and states in one step.

Another issue is that you have to make up your mind how to handle the start situation as the switches might be in "random" configuration at startup time. As of now the sketch waits for the first change of any of the switch positions.

Good luck with your project!

Nothing is older than the last post ... :wink:

I found a mistake in the previous versions of the switch evaluation; the returned state always started with "false" and toggled on each change.

//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; // Now correct, was "state = !state;" before ...
    return true;
  } else return false;
}

I corrected that in a new version to be found here Wokwi Simulation

In addition I wrote a function that collects the function calls depending on the state of switch Red. This should make the code more readable. Here is the sketch

// include SPI, MP3 and SD libraries
#define 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);
#endif

// Structure that holds the relevant playlist data
// It provides the functions to increment the index
// and to restart from the beginning
struct PlayListType {
  String  Name;             // Name of the playlist (for humans)
  int     StartFileNo;      // The number of the first file in the list
  int     MaxFiles;         // The numbers of sequential files in the list
  int LastFilePlayed;       // The last index (offset to StartFileNo) played
  void  Increment();        // Function that increments the index and avoids "overflow"
  void Restart();           // Function to reset the index to zero
};

// This defines what the Increment() function does
// Inside this function the variables of the struct
// can be used directly
void PlayListType::Increment() {
  LastFilePlayed += 1;
  if (LastFilePlayed >= MaxFiles) {
    LastFilePlayed = 0;
  };
}

// Just to avoid that you have to cope with struct content
// in your sketch directly.
// MainTitle.Reset();
// is identical to
// MainTitle.LastFilePlayed  = 0;
void PlayListType::Restart() {
  LastFilePlayed = 0;
}

// Now the declaration of the playlists with
// PlayListType PlayListName {"Name", StartFileNo, MaxFiles, LastFilePlayed};
PlayListType ShieldsKlingon {"ShieldsKlingon", 8, 2, 0};
PlayListType ViewScreen {"ViewScreen", 1, 2, 0};
PlayListType Astrogator {"Astrogator",  3, 2, 0};
PlayListType SensorTrack {"SensorTrack",  17, 4, 0};
PlayListType SickbayTrack {"SickbayTrack",  21, 4, 0};
PlayListType EngineeringTrack {"EngineeringTrack", 31, 4, 0};
PlayListType TransporterTrack {"TransporterTrack", 25, 3, 0};
PlayListType DestructTrack {"DestructTrack", 35, 4, 0};
PlayListType MainTitle {"MainTitle",   28,  3, 0};
PlayListType MusicTrack {"MusicTrack",  39, 22, 0};
PlayListType BridgeTrack {"BridgeTrack", 9, 8, 0};

// This function takes one of those playlist structures and
// creates the appropriate filename "online"
// and uses the common function startPlayingFile()
void playList(PlayListType &pl, boolean restart) {
  if (restart) pl.Restart();
  playFile(pl.StartFileNo + pl.LastFilePlayed);
  pl.Increment();
}

// 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 = 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

// Variable for the LEDs used
int leds[9] = {26, 28, 30, 32, 34, 36, 38, 40, 42};

int OPStatus = LOW;

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 jj; jj < sizeof(leds) / sizeof(int); jj++) {
    pinMode(leds[jj], OUTPUT);
    delay(10);
  }
}

void loop() {
  handleSwitchRed();
  handleSwitchYellow();
  handleSwitchBlue();
  handleSwitchWhite();
  handleSwitchLtBlue();
  handleSwitchGreen();
  handleSwitchGray();
  handleSwitchBlack();
  if (Switch[Red].state) blinkLEDs();
  else setLEDsTo(HIGH);
}

void setLEDsTo(byte state) {
  for (int i = 0; i < sizeof(leds) / sizeof(int); i++)
    digitalWrite(leds[i], state);
}

void blinkLEDs() {
  static long int delayTime = 0;
  static long int lastSwitchTime = 0;
  static byte state = HIGH;
  if (millis() - lastSwitchTime > delayTime) {
    lastSwitchTime = millis();
    delayTime      = random(25, 1000);
    digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], state);
    state = !state;
  }
}


void playFile(int Number) {
  char aFilename[80];
  sprintf(aFilename, "track%03d.mp3", Number);
#ifdef TEST
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
  if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
  musicPlayer.startPlayingFile(aFilename);
  musicPlayer.setVolume(1, 1);
#endif
}

void playFullFile(int Number) {
  char aFilename[80];
  sprintf(aFilename, "track%03d.mp3", Number);
#ifdef TEST
  Serial.print("Playing Fullfile ");
  Serial.println(aFilename);
  delay(5000);  // Simulates a playbacktime of 5 seconds ...
#else
  musicPlayer.playFullFile(aFilename);
#endif
}

void Reaction(uint16_t aSwitch) {
  if (Switch[Red].state) {   
    // If Red is On
    switch (aSwitch) {
      case Red    : playFullFile(61);
        break;
      case Yellow :  playList(ViewScreen, false);
        break;
      case Blue   :  playList(Astrogator, false);
        break;
      case White  :  playList(SensorTrack, false);
        break;
      case LtBlue :  playList(SickbayTrack, false);
        break;
      case Green :  playList(TransporterTrack, false);
        break;
      case Gray  :  playList(MusicTrack, false);
        break;
    }
  } else {
    // If Red is Off
    switch (aSwitch) {
      case Red : playFile(62);
        break;
      case Yellow : playList(ShieldsKlingon, false);
        break;
      case Blue   : playFile(5);
        break;
      case White :  playFile(6);
        break;
      case LtBlue : playList(EngineeringTrack, false);
        break;
      case Green :  playList(DestructTrack, false);
        break;
      case Gray  :  playList(MainTitle, false);
        break;
    }
  }
}


void handleSwitchRed() {
  if (Switch[Red].changed()) Reaction(Red);
}

void handleSwitchYellow() {
  if (Switch[Yellow].changed()) {
    if (Switch[Yellow].state) Reaction(Yellow);
    else playFile(62);
  }
}

void handleSwitchBlue() {
  if (Switch[Blue].changed()) {
    if (Switch[Blue].state) Reaction(Blue);
    else playFile(62);
  }
}

void handleSwitchWhite() {
  if (Switch[White].changed()) {
    if (Switch[White].state) Reaction(White);
    else playFile(62);
  }
}

void handleSwitchLtBlue() {
  if (Switch[LtBlue].changed()) {
    if (Switch[LtBlue].state) Reaction(LtBlue);
    else playFile(62);
  }
}

void handleSwitchGreen() {
  if (Switch[Green].changed()) {
    if (Switch[Green].state) Reaction(Green);
    else playFile(62);
  }
}

void handleSwitchGray() {
  if (Switch[Gray].changed()) {
    if (Switch[Gray].state) Reaction(Gray);
    else playFile(62);
  }
}

void handleSwitchBlack() {
  if (Switch[Black].changed()) {
    if (Switch[Black].state) playList(BridgeTrack, false);
    else  playFile(62);
  }
}

However this does not solve the remarks from post #8 ...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.