Neophyte needs a push in the right direction

Actually, the Arduino loop() function is precisely a "while(1) { ... }". :grin:

You cannot have more than one loop() function because it is a loop.

If you wish to include several looping code sections (and have only a single processor core), then each section exists in succession inside the loop(). Because nothing ever "busy waits", there is no section of the code which delays any part of the loop, so each part of the loop sees itself repeatedly invoked and as far as it is concerned, is looping.

Ok, thanks. Makes some more sense. Im pretty good with VB, better with T-SQL, arduino sketches are new to me, but im learning, so i do appreciate everyones feedback!!

The Arduino IDE provides setup() and loop() as a convenience for developers. If you want a deeper look into it you may read here

https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/main.cpp

What you can see there is how SerialEvents are taken care of although there is no call inside loop():

for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}

No "must know" but good to know :wink:

And - after telling you something about the structs I made this example:

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

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 = 2;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31,35}; // pins of the switches 
enum {No5, No1};  // Enumeration that keeps track of the human switch numbering

// Definition of LEDs
const byte NoOfLEDs = 3;
LEDType LED[NoOfLEDs];        // Declaration of the LEDs
byte LEDPin[NoOfLEDs] = {28,26,30}; // pins of the LEDs 
enum {CommLight, Switch5Light, Switch1Light};  // 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
  }
#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();
  LED[Switch1Light].handleFlash();
}


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) {
#ifdef TEST 
    // No musicplayer required
#else      
      musicPlayer.playFullFile("track001.mp3");
#endif      
      Serial.println("Flash!");
      LED[Switch1Light].DoFlash = true;
    }
    else {
      LED[Switch1Light].off();
      LED[Switch1Light].DoFlash = false;
      Serial.println("No Flash!");
    }
  }  
}

As you can see structs and classes in C++ are almost the same ... there is only a very little difference regarding what is public and what is private per default.

There is surely still room for improvement, but just check it out and I am sure you'll find the way to easily add further switches and LEDs!

Good luck and have fun with Arduino!

see also https://wokwi.com/projects/335367021862584914

P.S.: You could e.g. add the name of each component to the struct and initialize it in setup() so that you can print the name via Serial if required ...

Now I believe that

is used instead of

while(1) {

in case the code should contain a "break" statement.

Interesting question!

I have seen explanations of the Arduino setup() and loop() using both either while() or for(). The version I linked here is just the one I found in the library.

Generally both can be left using break.... Let's try it...:wink:

P.S.: I tried it in Wokwi and got a build error if the break would leave loop() ... I recon it is independend from while() or for(). Maybe a question for advanced experts in the forum?!?

Thanks. I did try adding Switch2 into the mix - strangely enough, it works, but only flashes the LED for Switch 2 AFTER the mp3 file plays, and doesnt stop.. Did I pout the new void() in the wrong place?

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

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 = 3;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31,35, 37}; // pins of the switches 
enum {No5, No1, No2};  // Enumeration that keeps track of the human switch numbering

// Definition of LEDs
const byte NoOfLEDs = 4;
LEDType LED[NoOfLEDs];        // Declaration of the LEDs
byte LEDPin[NoOfLEDs] = {28,26,30, 32}; // pins of the LEDs 
enum {CommLight, Switch5Light, Switch1Light, Switch2Light};  // 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
  }
#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();
  LED[Switch1Light].handleFlash();
  LED[Switch2Light].handleFlash();
}


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) {
#ifdef TEST 
    // No musicplayer required
#else      
      musicPlayer.playFullFile("track001.mp3");
#endif      
      Serial.println("Flash!");
      LED[Switch1Light].DoFlash = true;
    }
    else {
      LED[Switch1Light].off();
      LED[Switch1Light].DoFlash = false;
      Serial.println("No Flash!");
    }
  }  
}

void handleSwitchNo2(){
  if (Switch[No2].changed()){
     if (Switch[No2].state) {
#ifdef TEST 
    // No musicplayer required
#else      
      musicPlayer.playFullFile("track002.mp3");
#endif      
      Serial.println("Flash!");
      LED[Switch2Light].DoFlash = true;
    }
    else {
      LED[Switch2Light].off();
      LED[Switch2Light].DoFlash = false;
      Serial.println("No Flash!");
    }
  }  
}

That's a nice phrasing "put the void() in the wrong place" :wink:

"void" just declares that a function does not return anything or (if used inside the brackets after a function name) that it does not use any parameters. The question should read "Did I put the function xyz() in the wrong place?" ...

I will have a look.

P.S.: Think I found it. (Sorry, I did not investigate into the Adafruit Player before :wink: ):

Your file track001.mp3 is quite likely much shorter that track002.mp3.

If you read here

https://adafruit.github.io/Adafruit_VS1053_Library/html/class_adafruit___v_s1053___file_player.html

it says:

boolean playFullFile(const char *trackname)
Play the complete file. This function will not return until the playback is complete.|

So playing a mp3 using this command will block loop() until the playback is complete.

To be able to do other things while mp3 are played you have to use this one instead of playFullFile()

boolean startPlayingFile(const char *trackname)
Begin playing the specified file from the SD card using interrupt-drive playback.

There is an example how to use interrupt driven playing:

https://github.com/adafruit/Adafruit_VS1053_Library/blob/master/examples/player_interrupts/player_interrupts.ino

PPS: You can test this one:

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

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 = 3;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31,35, 37}; // pins of the switches 
enum {No5, No1, No2};  // Enumeration that keeps track of the human switch numbering

// Definition of LEDs
const byte NoOfLEDs = 4;
LEDType LED[NoOfLEDs];        // Declaration of the LEDs
byte LEDPin[NoOfLEDs] = {28,26,30, 32}; // pins of the LEDs 
enum {CommLight, Switch5Light, Switch1Light, Switch2Light};  // 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
  }
#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();
  LED[Switch1Light].handleFlash();
  LED[Switch2Light].handleFlash();
}


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;
      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;
      Serial.println("No Flash!");
    }
  }  
}

void StartMP3(char *aFilename){
#ifdef TEST 
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
   if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
   musicPlayer.startPlayingFile(aFilename);
#endif   
}

Very close! Flashing does work, but cuts off the MP3 playback now on any file after about a quarter of a second (it starts then stops.) Track 1 is about 1 second long, track 2 is about 10 seconds long...)

Oops, my fault ... I missed to notify the musicPlayer to use the interrupt pin:

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

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 = 3;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31,35, 37}; // pins of the switches 
enum {No5, No1, No2};  // Enumeration that keeps track of the human switch numbering

// Definition of LEDs
const byte NoOfLEDs = 4;
LEDType LED[NoOfLEDs];        // Declaration of the LEDs
byte LEDPin[NoOfLEDs] = {28,26,30, 32}; // pins of the LEDs 
enum {CommLight, Switch5Light, Switch1Light, Switch2Light};  // 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();
  LED[Switch1Light].handleFlash();
  LED[Switch2Light].handleFlash();
}


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;
      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;
      Serial.println("No Flash!");
    }
  }  
}

void StartMP3(char *aFilename){
#ifdef TEST 
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
   if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
   musicPlayer.startPlayingFile(aFilename);
#endif   
}

Hope this does it (see the line "musicPlayer.useInterrupt()" in setup() ) ... Unfortunately I can not test it at home ...

THANKS!!! That did it!

Going to add something else, but want to try it and post results. Adding another pushbutton switch (Switch 4) and a reed switch) - single LED for switch 4, no flashing. When reed switch is activated, play the next MP3 in a playlist, but when reed switch is open, play the next MP3 in a different playlist, but activated whenever the state of Switch4 changes.

Glad it works...

If you are adding other types of switches/buttons you may have to add further functions to SwitchType like SwitchType::released() and SwitchType:: pressed() and possibly SwitchType::recentstate() depending on the use...

Give it a try.

Im getting there. Just not sure on the right path.

Someone else showed me case to basically play the next MP3 in a playlist.

Ive added Switch3 (not and issue) - added switch4 (works to just play a single file) - Ideally, what I'd like SwitchN04 to do:

Play the next MP3 in the playlist commtrack whenever the sate changes
Add a reed switch to this (pin41) - and if pin41 is closed, rather than switchNo4 play commTrack, have it play ComputerTrack.

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

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 = 5;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {31,35,37,39,41}; // pins of the switches 
enum {No5, No1, No2, No3, No4};  // 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();
  playCommTrack();
  playComputerTrack();
  LED[Switch1Light].handleFlash();
  LED[Switch2Light].handleFlash();
  LED[Switch3Light].handleFlash();
  LED[Switch4Light].handleFlash();
}


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) {
      playCommTrack();
      Serial.println("Flash!");
      LED[Switch4Light].DoFlash = true;
    }
    else {
      LED[Switch4Light].off();
      LED[Switch4Light].DoFlash = false;
      StartMP3("track008.mp3");
      Serial.println("No Flash!");
    }
  }  
}

void playCommTrack() {
  int commOutput = 1;
  if ( commOutput <= 17) {
    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 ++;
  } else {
    commOutput = 1;

  }
}

void playComputerTrack() {
  int computerOutput = 1;
  if (computerOutput <= 12) {
    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 ++;
  } else {
    computerOutput = 1;

  }
}

void StartMP3(char *aFilename){
#ifdef TEST 
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
   if (musicPlayer.playingMusic) musicPlayer.stopPlaying();
   musicPlayer.startPlayingFile(aFilename);
#endif   
}

If you want changes to local variables to stay you have to declare these variables as "static":

void playCommTrack() {
  int commOutput = 1;  // This will become 1 everytime you call the function
   //...
  commOutput = 99; // Will have no effect after you leave the function!!!
}

void playCommTrack() {
 static int commOutput = 1;  // This will be set to 1 at the first call of this function
   //...
  commOutput = 99; // commOutput will be 99 when the function is called the next time!
}

That applies also to variables in playComputerTrack() and the like ...

And if you call the functions in the loop() they will play independently of the switches:

void loop() {
  handleSwitchNo5();
  handleSwitchNo1();
  handleSwitchNo2();
  handleSwitchNo3();
  handleSwitchNo4();
  playCommTrack();    // Will always play 
  playComputerTrack(); // Will always play 
  LED[Switch1Light].handleFlash();
  LED[Switch2Light].handleFlash();
  LED[Switch3Light].handleFlash();
  LED[Switch4Light].handleFlash();
}

Yes, sir! Thanks so much! That did it!!

At least a little bit :wink:

This sketch should even do what you want regarding the reed switch (provided you connect it to pin 43 and GND):

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

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   
}

As it is now - if you change the reed state it will go on with the next mp3 to play in the chosen list.

If you want to restart from file no.1 in each list everytime the reed state changes, you have to reset the static variable inside the respective function ... As you cannot do this from outside the function, I have added a parameter to the calls and you may uncomment the two lines in handleReed() :

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

For testing see https://wokwi.com/projects/335816194744386132

I rearranged the switches:

image

  • Left to right switch No 1 to No 5
  • Bottom switch = Reed Switch

Thanks. It definitely does - however, how can I tweakl the reed switch function to play a start and end MP3 when it changes?

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()) {
    StartMP3("track012.mp3");
   else {
    StartMP3("track013.mp3");
      
    }
    // Uncomment the following lines
    // if you want the playlists to be reset
    // by a change of Reed state
    //playCommTrack(true);
    //playComputerTrack(true); 
  };
}

First off, your parenthesis are messed up. That code will play track 12 if the switch has changed and play track 13 if it has NOT changed. I think you want to play the track only on change but different tracks depending on if the change was HIGH->LOW vs. LOW->HIGH?

if (Switch[Reed].changed()) {
if ( Switch[Reed].state ) StartMP3("track012.mp3");
else StartMP3("track013.mp3");
}

Well spotted!

Thank you all! Sometimes, you cant see the nesting for the trees, so to speak.