Handles are staying on

OK. Got a weird one in my sketch.

Everything works EXACTLY like its support to - EXCEPT the following three:

handleSwitchPhasers()
handleSwitchSensors()
handleSwitchShuttle()

For some reason - one just these three ONLY - the digital pins never go back to OUTPUT LOW after the MP3 files play - they stay on as OUTPUT HIGH. All the others turn the output off to those pins - but these do not. I can't seem to see the forest for the threes on these three.

// LedFlasher Library by Nick Gammon, 23 Dec 2012
#include <LedFlasher.h>

// include SPI, MP3 and SD libraries
#undef TEST

#ifdef TEST
// Do not use the swpcific hardware to play sound
// use Serial.println() instead
#else
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"
// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
   // Set volume for left, right channels. lower numbers == louder volume!
   //  musicPlayer.setVolume(1,1);
#endif

const byte PRESSED = LOW;

// 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 = 10;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {22,40,42,44,46,48,49,47,45,43}; // pins of the switches
enum {Cruise,Phasers,Photons,RedAlert,Impulse,Sensors,Warp,Shuttle,Tribbles,Comp1};  // Enumeration that keeps track of the human switch naming


void setup() {
 // put your setup code here, to run once:
pinMode (26, OUTPUT); //Bridge LEDs
pinMode (28, OUTPUT); //Main Saucer strips
pinMode (30, OUTPUT); //Support column strips
pinMode (32, OUTPUT); //Secondary hull strips and shuttle LEDs
pinMode (34, OUTPUT); //5vDC out to power Navs strobes and 6 lights under screen
pinMode (36, OUTPUT); //5vDC out to power bussards
pinMode (38, OUTPUT); //LED strips for Nacelles
pinMode (25, OUTPUT); //Phaser 1 LED
pinMode (27, OUTPUT); //Photon 1 LED
pinMode (29, OUTPUT); //Red Alert LED
pinMode (31, OUTPUT); //Impulse LED
pinMode (33, OUTPUT); //Sensors LED
pinMode (35, OUTPUT); //Shuttle LED

// setup Pin Modes
  for (int i = 0; i < NoOfSwitches; i++) {
    Switch[i].No = i + 1;
    Switch[i].setPin(SwitchPin[i]);
    Switch[i].state = digitalRead(SwitchPin[i]) == !PRESSED;  // probably not!
    Switch[i].lastState = Switch[i].state;
  }
   
Serial.begin(115200);
  Serial.println("Adafruit VS1053 Simple Test");
#ifdef TEST
  // No musicplayer, no SD card required
#else
  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("VS1053 found"));

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }

  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));
  // Set volume for left, right channels. lower numbers == louder volume!
  // Set volume for left, right channels. lower numbers == louder volume! 
  musicPlayer.setVolume(1,1
  );
#endif
}

//declare variables
bool startup = false;



void loop() {
  // put your main code here, to run repeatedly:

// Play the Startup sound one time on power up
if (startup == false) {
      Serial.println(F("Playing track 001"));
      musicPlayer.playFullFile("/track001.mp3");
      startup = true;
}
handleSwitchCruise();
handleSwitchPhasers();
handleSwitchPhotons();
handleSwitchRedAlert();
handleSwitchImpulse();
handleSwitchSensors();
handleSwitchWarp();
handleSwitchShuttle();
handleSwitchTribbles();
handleSwitchComp1();
ResumeBridgeTrack();
}

void handleSwitchCruise(){
  if (Switch[Cruise].changed()) {
   if ( Switch[Cruise].state == PRESSED ) {
      StartMP3("track002.mp3");
      delay(800);
      digitalWrite(26, HIGH);
      delay(1300);
      digitalWrite(28, HIGH);
      delay(1700);
      digitalWrite(30, HIGH);
      delay(1300);
      digitalWrite(32, HIGH);
      delay(3300);
      digitalWrite(34, HIGH);
      delay(8300);
      digitalWrite(36, HIGH);
      delay(12000);
      digitalWrite(38, HIGH);
      delay(28000);
      digitalWrite(38, LOW);
      }
   else 
      {
      StartMP3("track022.mp3");
      delay(5000);
      digitalWrite(26, LOW);
      digitalWrite(28, LOW);
      digitalWrite(30, LOW);
      digitalWrite(32, LOW);
      digitalWrite(34, LOW);
      digitalWrite(36, LOW);
      digitalWrite(38, LOW);
      digitalWrite(31, LOW);
      digitalWrite(33, LOW);
      }
   } 
 {
  };
}

void handleSwitchPhasers(){
  if (Switch[Phasers].changed()) {
   if ( Switch[Phasers].state == PRESSED ) {
      StartMP3("track006.mp3");
      digitalWrite(25, HIGH);
      delay(5000);
      }
   else 
      {
      digitalWrite(25, LOW);
      }
   } 
 {
  };
}

void handleSwitchPhotons(){
  if (Switch[Photons].changed()) {
   if ( Switch[Photons].state == PRESSED ) {
      StartMP3("track007.mp3");
      delay(300);
      digitalWrite(27, HIGH);
      delay(250);
      digitalWrite(27, LOW);
      delay(2200);
      digitalWrite(27, HIGH);
      delay(250);
      digitalWrite(27, LOW);
      delay(2000);
      }
   else 
      {
      digitalWrite(27, LOW);
      }
   } 
 {
  };
}

void handleSwitchRedAlert(){
  if (Switch[RedAlert].changed()) {
   if ( Switch[RedAlert].state == PRESSED ) {
      StartMP3("track008.mp3");
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(700);
      }
   else 
      {
      digitalWrite(29, LOW);
      }
   } 
 {
  };
}

void handleSwitchImpulse(){
  if (Switch[Impulse].changed()) {
   if ( Switch[Impulse].state == PRESSED ) {
      StartMP3("track004.mp3");
      delay(2000);
      digitalWrite(38, LOW);
      digitalWrite(31, HIGH);
      }
   else 
      {

      }
   } 
 {
  };
}

void handleSwitchSensors(){
  if (Switch[Sensors].changed()) {
   if ( Switch[Sensors].state == PRESSED ) {
      StartMP3("track011.mp3");
      delay(1000);
      digitalWrite(33, HIGH);
      delay(14000);
      }
   else 
      {
      digitalWrite(33, LOW);
      }
   } 
 {
  };
}

void handleSwitchWarp(){
  if (Switch[Warp].changed()) {
   if ( Switch[Warp].state == PRESSED ) {
      musicPlayer.playFullFile("track018.mp3");
      delay(250);
      StartMP3("track005.mp3");
      delay(2000);
      digitalWrite(31, LOW);
      digitalWrite(38, HIGH);
      }
   else 
      {

      }
   } 
 {
  };
}

void handleSwitchShuttle(){
  if (Switch[Shuttle].changed()) {
   if ( Switch[Shuttle].state == PRESSED ) {
      musicPlayer.playFullFile("track098.mp3");
      delay(250);
      StartMP3("track099.mp3");
      digitalWrite(35, HIGH);
      delay(16000);
      }
   else 
      {
      digitalWrite(35, LOW);
      }
   } 
 {
  };
}

void handleSwitchTribbles(){
  if (Switch[Tribbles].changed()) {
   if ( Switch[Tribbles].state == PRESSED ) {
      StartMP3("track009.mp3");
      digitalWrite(28, LOW);
      delay(900);
      digitalWrite(28, HIGH);
      delay(3000);
      digitalWrite(30, LOW);
      delay(1500);
      digitalWrite(30, HIGH);
      delay(4000);
      digitalWrite(32, LOW);
      delay(800);
      digitalWrite(32, HIGH);
      delay(2000);
      digitalWrite(26, LOW);
      delay(1500);
      digitalWrite(26, HIGH);
      delay(3000);
      digitalWrite(28, LOW);
      delay(500);
      digitalWrite(28, HIGH);
      delay(500);
      }
   else 
      {
      
      }
   } 
 {
  };
}

void handleSwitchComp1(){
  if (Switch[Comp1].changed()){
     if (Switch[Comp1].state) {
      playComputerTrack();      
    } 
    else {

      }
  }  
}


void playComputerTrack() {
  static int computerOutput = 1;
  if (computerOutput <= 25) {
    switch (computerOutput) {
      case (1):
        StartMP3("track012.mp3");
        break;
      case (2):
        StartMP3("track013.mp3");
        break;
      case (3):
        StartMP3("track014.mp3");
        break;
      case (4):
        StartMP3("track015.mp3");
        break;
      case (5):
        StartMP3("track016.mp3");
        break;
      case (6):
        StartMP3("track017.mp3");
        break;
      case (7):
        StartMP3("track019.mp3");
        break;
      case (8):
        StartMP3("track020.mp3");
        break;
      case (9):
        StartMP3("track023.mp3");
        break;
      case (10):
        StartMP3("track024.mp3");
        break;
      case (11):
        StartMP3("track025.mp3");
        break;
      case (12):
        StartMP3("track026.mp3");
        break;
      case (13):
        StartMP3("track027.mp3");
        break;
      case (14):
        StartMP3("track028.mp3");
        break;
      case (15):
        StartMP3("track029.mp3");
        break;
      case (16):
        StartMP3("track030.mp3");
        break;
      case (17):
        StartMP3("track031.mp3");
        break;
      case (18):
        StartMP3("track032.mp3");
        break;
      case (19):
        StartMP3("track033.mp3");
        break;
      case (20):
        StartMP3("track034.mp3");
        break;
      case (21):
        StartMP3("track035.mp3");
        break;
      case (22):
        StartMP3("track036.mp3");
        break;
      case (23):
        StartMP3("track037.mp3");
        break;  
      case (24):
        StartMP3("track038.mp3");
        break; 
      case (25):
        StartMP3("track039.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);
   musicPlayer.setVolume(1,1);
#endif   
}

void ResumeBridgeTrack(){
   if (musicPlayer.stopped() && Switch[Cruise].state == PRESSED )  StartMP3("track003.mp3");
}

One more thing I noticed... the way you have this "if..condition" closes the condition immediately due to a semicolon. See comments below...

void StartMP3(char *aFilename) {
#ifdef TEST
  Serial.print("Playing ");
  Serial.println(aFilename);
#else
  if (musicPlayer.playingMusic) musicPlayer.stopPlaying(); // this semicolon means the condition ends after "stopPlaying()"
  musicPlayer.startPlayingFile(aFilename); // not in the "if..condition" braces, so always executes
  musicPlayer.setVolume(1, 1); // not in the "if..condition" braces, so always executes
#endif
}

I may be the only one who just finds the formatting of your code totally impenetrable.

You might get more attention if you were to apply the IDE Autoformat tool or adopted one of several commonly used formatting styles and wrote your code using it.

It might even help you see errors.

And here, all I can make of it is that you have sprintzled meaningless empty statements about:

 {
  };

Also, if you haven't, go to the IDE preferences and turn up the compiler warnings. It will flag things that just might not be what you meant, like the useless if statements and maybe even the stray braces and semicolons, which while harmless just make noise.

a7

1 Like

+1 from me

1 Like

Who made the code?

Perhaps let me clarify a bit.

I'm still an Arduino neophyte. In still learning. Please bear with that, as everyone was a beginner at one time.

The code I wrote mostly myself, using from some bits and pieces from previous sketches. I come from a BASIC, Visual Basic and T-SQL programming background, so this is still a bit of a new environment and language for me.

Since I come from Visual Basic, I'm used to writing an object that works, cutting and pasting a piece at a time, and then changing values. That may help explain what may seem disjointed to you. I'm used to cause and effect-logical step by step progression.

Since I'm also used to Visual Basic IF/THEN/ELSE structures, if my ELSE doesn't do anything at all - ELSE = "" - that is why I ended up with empty values.

I'm sure there's 3000 ways to do this better than I am doing - I'm trying to help a friend light a 3ft model of the Star Trek Enterprise with LEDs, and turn LEDS on an off with straight DC power, MOSFETs for higher voltage, play sounds with a push of a button, etc etc. He doesn't know anything about Arduino, so I'm trying to help him.

Thanks for explaining. I've just topped up my cutting some slack pile :slight_smile:
You could sort a lot of the inconsistencies out by doing a select all and then using the autoformat tool in the IDE.

Maybe some of what is odd are "empty values", but

  { 
};

is not part of any other statement. It is two statements, one an empty compound statement, the other essentially a stray harmless semicolon.

Do what you need to to succeed or progress, I'm just saying the the Autoformat tool can help speed that progress if you are at all interested in doing as the Romans do, now you in Rome.

And I repeat it will be more likely that your code gets the attention you ask us to give it.

If I was in the lab and thought I was going to help, I'd just throw it into an empty sketch and autoformat it, which would take care of some or even most ot the weirdness.

But it wouldn't throw out empty statements.

a7

Here is the code after running through AutoFormat:

// LedFlasher Library by Nick Gammon, 23 Dec 2012
#include <LedFlasher.h>

// include SPI, MP3 and SD libraries
#undef TEST

#ifdef TEST
// Do not use the swpcific hardware to play sound
// use Serial.println() instead
#else
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"
// These are the pins used for the breakout example
#define BREAKOUT_RESET 9  // VS1053 reset pin (output)
#define BREAKOUT_CS 10    // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8    // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1   // VS1053 reset pin (unused!)
#define SHIELD_CS 7       // VS1053 chip select pin (output)
#define SHIELD_DCS 6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4          // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3            // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// Set volume for left, right channels. lower numbers == louder volume!
//  musicPlayer.setVolume(1,1);
#endif

const byte PRESSED = LOW;

// 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 = 10;
SwitchType Switch[NoOfSwitches];                                            // Declaration of the switches
byte SwitchPin[NoOfSwitches] = { 22, 40, 42, 44, 46, 48, 49, 47, 45, 43 };  // pins of the switches
enum { Cruise,
       Phasers,
       Photons,
       RedAlert,
       Impulse,
       Sensors,
       Warp,
       Shuttle,
       Tribbles,
       Comp1 };  // Enumeration that keeps track of the human switch naming


void setup() {
  // put your setup code here, to run once:
  pinMode(26, OUTPUT);  //Bridge LEDs
  pinMode(28, OUTPUT);  //Main Saucer strips
  pinMode(30, OUTPUT);  //Support column strips
  pinMode(32, OUTPUT);  //Secondary hull strips and shuttle LEDs
  pinMode(34, OUTPUT);  //5vDC out to power Navs strobes and 6 lights under screen
  pinMode(36, OUTPUT);  //5vDC out to power bussards
  pinMode(38, OUTPUT);  //LED strips for Nacelles
  pinMode(25, OUTPUT);  //Phaser 1 LED
  pinMode(27, OUTPUT);  //Photon 1 LED
  pinMode(29, OUTPUT);  //Red Alert LED
  pinMode(31, OUTPUT);  //Impulse LED
  pinMode(33, OUTPUT);  //Sensors LED
  pinMode(35, OUTPUT);  //Shuttle LED

  // setup Pin Modes
  for (int i = 0; i < NoOfSwitches; i++) {
    Switch[i].No = i + 1;
    Switch[i].setPin(SwitchPin[i]);
    Switch[i].state = digitalRead(SwitchPin[i]) == !PRESSED;  // probably not!
    Switch[i].lastState = Switch[i].state;
  }

  Serial.begin(115200);
  Serial.println("Adafruit VS1053 Simple Test");
#ifdef TEST
  // No musicplayer, no SD card required
#else
  if (!musicPlayer.begin()) {  // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1)
      ;
  }
  Serial.println(F("VS1053 found"));

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1)
      ;  // don't do anything more
  }

  if (!musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));
  // Set volume for left, right channels. lower numbers == louder volume!
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(1, 1);
#endif
}

//declare variables
bool startup = false;



void loop() {
  // put your main code here, to run repeatedly:

  // Play the Startup sound one time on power up
  if (startup == false) {
    Serial.println(F("Playing track 001"));
    musicPlayer.playFullFile("/track001.mp3");
    startup = true;
  }
  handleSwitchCruise();
  handleSwitchPhasers();
  handleSwitchPhotons();
  handleSwitchRedAlert();
  handleSwitchImpulse();
  handleSwitchSensors();
  handleSwitchWarp();
  handleSwitchShuttle();
  handleSwitchTribbles();
  handleSwitchComp1();
  ResumeBridgeTrack();
}

void handleSwitchCruise() {
  if (Switch[Cruise].changed()) {
    if (Switch[Cruise].state == PRESSED) {
      StartMP3("track002.mp3");
      delay(800);
      digitalWrite(26, HIGH);
      delay(1300);
      digitalWrite(28, HIGH);
      delay(1700);
      digitalWrite(30, HIGH);
      delay(1300);
      digitalWrite(32, HIGH);
      delay(3300);
      digitalWrite(34, HIGH);
      delay(8300);
      digitalWrite(36, HIGH);
      delay(12000);
      digitalWrite(38, HIGH);
      delay(28000);
      digitalWrite(38, LOW);
    } else {
      StartMP3("track022.mp3");
      delay(5000);
      digitalWrite(26, LOW);
      digitalWrite(28, LOW);
      digitalWrite(30, LOW);
      digitalWrite(32, LOW);
      digitalWrite(34, LOW);
      digitalWrite(36, LOW);
      digitalWrite(38, LOW);
      digitalWrite(31, LOW);
      digitalWrite(33, LOW);
    }
  }
  {};
}

void handleSwitchPhasers() {
  if (Switch[Phasers].changed()) {
    if (Switch[Phasers].state == PRESSED) {
      StartMP3("track006.mp3");
      digitalWrite(25, HIGH);
      delay(5000);
    } else {
      digitalWrite(25, LOW);
    }
  }
  {};
}

void handleSwitchPhotons() {
  if (Switch[Photons].changed()) {
    if (Switch[Photons].state == PRESSED) {
      StartMP3("track007.mp3");
      delay(300);
      digitalWrite(27, HIGH);
      delay(250);
      digitalWrite(27, LOW);
      delay(2200);
      digitalWrite(27, HIGH);
      delay(250);
      digitalWrite(27, LOW);
      delay(2000);
    } else {
      digitalWrite(27, LOW);
    }
  }
  {};
}

void handleSwitchRedAlert() {
  if (Switch[RedAlert].changed()) {
    if (Switch[RedAlert].state == PRESSED) {
      StartMP3("track008.mp3");
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(800);
      digitalWrite(29, HIGH);
      delay(1000);
      digitalWrite(29, LOW);
      delay(700);
    } else {
      digitalWrite(29, LOW);
    }
  }
  {};
}

void handleSwitchImpulse() {
  if (Switch[Impulse].changed()) {
    if (Switch[Impulse].state == PRESSED) {
      StartMP3("track004.mp3");
      delay(2000);
      digitalWrite(38, LOW);
      digitalWrite(31, HIGH);
    } else {
    }
  }
  {};
}

void handleSwitchSensors() {
  if (Switch[Sensors].changed()) {
    if (Switch[Sensors].state == PRESSED) {
      StartMP3("track011.mp3");
      delay(1000);
      digitalWrite(33, HIGH);
      delay(14000);
    } else {
      digitalWrite(33, LOW);
    }
  }
  {};
}

void handleSwitchWarp() {
  if (Switch[Warp].changed()) {
    if (Switch[Warp].state == PRESSED) {
      musicPlayer.playFullFile("track018.mp3");
      delay(250);
      StartMP3("track005.mp3");
      delay(2000);
      digitalWrite(31, LOW);
      digitalWrite(38, HIGH);
    } else {
    }
  }
  {};
}

void handleSwitchShuttle() {
  if (Switch[Shuttle].changed()) {
    if (Switch[Shuttle].state == PRESSED) {
      musicPlayer.playFullFile("track098.mp3");
      delay(250);
      StartMP3("track099.mp3");
      digitalWrite(35, HIGH);
      delay(16000);
    } else {
      digitalWrite(35, LOW);
    }
  }
  {};
}

void handleSwitchTribbles() {
  if (Switch[Tribbles].changed()) {
    if (Switch[Tribbles].state == PRESSED) {
      StartMP3("track009.mp3");
      digitalWrite(28, LOW);
      delay(900);
      digitalWrite(28, HIGH);
      delay(3000);
      digitalWrite(30, LOW);
      delay(1500);
      digitalWrite(30, HIGH);
      delay(4000);
      digitalWrite(32, LOW);
      delay(800);
      digitalWrite(32, HIGH);
      delay(2000);
      digitalWrite(26, LOW);
      delay(1500);
      digitalWrite(26, HIGH);
      delay(3000);
      digitalWrite(28, LOW);
      delay(500);
      digitalWrite(28, HIGH);
      delay(500);
    } else {
    }
  }
  {};
}

void handleSwitchComp1() {
  if (Switch[Comp1].changed()) {
    if (Switch[Comp1].state) {
      playComputerTrack();
    } else {
    }
  }
}


void playComputerTrack() {
  static int computerOutput = 1;
  if (computerOutput <= 25) {
    switch (computerOutput) {
      case (1):
        StartMP3("track012.mp3");
        break;
      case (2):
        StartMP3("track013.mp3");
        break;
      case (3):
        StartMP3("track014.mp3");
        break;
      case (4):
        StartMP3("track015.mp3");
        break;
      case (5):
        StartMP3("track016.mp3");
        break;
      case (6):
        StartMP3("track017.mp3");
        break;
      case (7):
        StartMP3("track019.mp3");
        break;
      case (8):
        StartMP3("track020.mp3");
        break;
      case (9):
        StartMP3("track023.mp3");
        break;
      case (10):
        StartMP3("track024.mp3");
        break;
      case (11):
        StartMP3("track025.mp3");
        break;
      case (12):
        StartMP3("track026.mp3");
        break;
      case (13):
        StartMP3("track027.mp3");
        break;
      case (14):
        StartMP3("track028.mp3");
        break;
      case (15):
        StartMP3("track029.mp3");
        break;
      case (16):
        StartMP3("track030.mp3");
        break;
      case (17):
        StartMP3("track031.mp3");
        break;
      case (18):
        StartMP3("track032.mp3");
        break;
      case (19):
        StartMP3("track033.mp3");
        break;
      case (20):
        StartMP3("track034.mp3");
        break;
      case (21):
        StartMP3("track035.mp3");
        break;
      case (22):
        StartMP3("track036.mp3");
        break;
      case (23):
        StartMP3("track037.mp3");
        break;
      case (24):
        StartMP3("track038.mp3");
        break;
      case (25):
        StartMP3("track039.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);
  musicPlayer.setVolume(1, 1);
#endif
}

void ResumeBridgeTrack() {
  if (musicPlayer.stopped() && Switch[Cruise].state == PRESSED) StartMP3("track003.mp3");
}

Those are the only functions in which you use the "else" portion of the "if" statement to set the output LOW.

Does the handleSwitchCruise() function ever play "track022.mp3" in the "else" portion of the "if" statement?

< edit >
How do you have the switches wired?

SwitchCruise is a rocker switch - ON/OFF - and yes, it does play track022.mp3 successfully on change.

All other switches are momentary buttons.

Oh, wait .... I thinki I see the part Im missing - see the comment below - I never turn the LED back off after the delay, and then it never circles back to the ELSE part of the statement?

void handleSwitchPhasers() {
  if (Switch[Phasers].changed()) {
    if (Switch[Phasers].state == PRESSED) {
      StartMP3("track006.mp3");
      digitalWrite(25, HIGH);
      delay(6000);
      digitalWrite(25, LOW);  // THIS would be the part Im missing, to turn them off? Because it's never circling back to the ELSE part of the statement?
    } else {
      digitalWrite(25, LOW);
    }
  }
  {};
}

I don't think that's a or the problem. Or wait, let's see:

  if the state has changed it either

    becomes pressed: mplay the track & write something HIGH
  
   becomes not pressed: write something LOW

So originally the writing LOW is not happening until the state changes. But it should happen, maybe even while you are sitting in a 6000 ms delay(). It should not be missed. Eventually that else clause would get execute.

But since you are timing this with delay() I think I can agree with your move.

Note: Here's where serial printing and making your sketches verbose can help. Most of mine are quite chatty and the flow in the monitor eventually becomes amusing after having been exploited to print the values of key variables and confirm that they are properly informing flow through the code.

I don't usually bother to remove them; if the serial output is not being used, it just spills out into the so-called bit bucket, no harm.

Or you could use techniques from crude to needlessly clever to turn on and off all such messaging.

BTW thanks for formatting! As I predicted, it did not remove…

  }
  {};  // ... these curious statements.
}

Unless you want to use that as a quirky personal signature of some kind, lose them wherever they are.

a7

You could get into a problem of one switch being released and another pressed while in a delay. That would cause both to go into the debounce at the same time, given the first switch checked priority and going into more delays before reaching the check for the released key.

1 Like

Adding in those lines, autoformatting and taking out the other brackets did it. Thanks all!

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