Handling issue with IF and ELSE

Hello, sometimes you cant see the forest for the trees.

Working on a 1/350 scale model of the Enterprise with a friend. He wants lights and sound effects. Everything will be lit with 5v LEDs, and using a Music Maker Shield to play MP3's. Here's the sequence:

On power up, play a startup noise ONE TIME (track001, works good)

Rocker switch, two positions, starts in the OFF position.

When you close the rocker, it plays a startup sequence and audio track002 (handleSwitchCruise), and lights up the LEDs in sequence with a few second delays in between them. (should be the IF portion of handleSwitchCruise). After that, it should continiusly play a bridge sound effect as a loop (which it does).

When you open the rocker, it should play track022, and then after a second or two, shut off all the LEDs (the ELSE portion of handleSwitchCruise). The background bridge noise should NOT play here (which it does).

Funny, everything works - but my IF and ELSEs are backwards. It does the ELSE section when you close the rocker, and then the IF portion when you open it again. Im sure its something stupid and minor, I just cant see it.

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

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

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

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

// Begin of Struct to handle the switches
struct SwitchType {
  byte No;
  byte pin;
  boolean state = false;
  void setPin(byte aPin);
  boolean changed();
  unsigned long lastChange = 0;
  byte lastState;
  bool changeOk = false;
};

//define the Switch Type
void SwitchType::setPin(byte aPin) {
  pin = aPin;
  pinMode(pin, INPUT_PULLUP);
  lastState = digitalRead(pin);
}

//Define the SwitchType changed
boolean SwitchType::changed() {
  byte actState = digitalRead(pin);
  if (lastState != actState) {
    lastState = actState;
    lastChange = millis();
    changeOk = true;
  }
  if (millis() - lastChange > 30 && changeOk) {
    changeOk = false;
    state = lastState;
    return true;
  } else return false;
}
// End of Struct to handle the switches

// Definition of Switches
const byte NoOfSwitches = 1;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {22}; // pins of the switches
enum {Cruise};  // Enumeration that keeps track of the human switch naming


void setup() {
 // put your setup code here, to run once:
pinMode (26, OUTPUT);
pinMode (28, OUTPUT);
pinMode (30, OUTPUT);
pinMode (32, OUTPUT);
pinMode (34, OUTPUT);
pinMode (36, OUTPUT);
pinMode (38, OUTPUT);

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

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

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

//decalre variables
bool startup = false;




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

// Play the Startup sound one time on power up
if (startup == false) {
      Serial.println(F("Playing track 001"));
      musicPlayer.playFullFile("/track001.mp3");
      startup = true;
}
handleSwitchCruise();
ResumeBridgeTrack();
}

void handleSwitchCruise(){
  if (Switch[Cruise].changed()) {
   if ( Switch[Cruise].state ) {
      StartMP3("track002.mp3");
      delay(1000);
      digitalWrite(26, HIGH);
      delay(1750);
      digitalWrite(28, HIGH);
      delay(2200);
      digitalWrite(30, HIGH);
      delay(4000);
      digitalWrite(32, HIGH);
      delay(4500);
      digitalWrite(34, HIGH);
      delay(10000);
      digitalWrite(36, HIGH);
      delay(11000);
      digitalWrite(38, HIGH);
    }
   else 
      {
      StartMP3("track022.mp3");
      delay(5000);
      digitalWrite(26, LOW);
      digitalWrite(28, LOW);
      digitalWrite(30, LOW);
      digitalWrite(32, LOW);
      digitalWrite(34, LOW);
      digitalWrite(36, LOW);
      digitalWrite(38, LOW);
      }
   } 
 {
  };
}


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

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

your if triggers on the rocker changing position and is declared as INPUT_PULLUP so will read LOW when active (whatever that means for a rocker)

instead of doing

  if ( Switch[Cruise].state ) {
    ...
  } else {
    ...
  }

test like this

  if ( Switch[Cruise].state == LOW ) { 
    // active
    ...
  } else {
    // not active
    ...
  }

Yes, upside down switches.

You can go a step further. Make a constant at the top of the sketch, viz:

const byte PRESSED = LOW;

then use it in your code everywhere:


  if ( Switch[Cruise].state == PRESSED ) { 
    // active
    ...
  } else {
    // not active
    ...
  }

So when/if you have buttons wired differently, you have one place to make one change to accommodate that.

Or build in the concept of how the switch is wired into the switch object.

a7

Thanks y'all. That definitely fixed the rocker IF ELSE problem - however running into issues with the bridgetrack now not working correctly:

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

Leaving blank like the original code - plays when turning rocker off
== PRESSED - loops after the startup sound plays, but does loop correctly when the rocker is turned on , turning the rocker off stops the bridgetrack from looping

So if I can just stop it from playing when it first gets power, that should do it. What part am I missing here?

<before food>

The initial value of Switch[Cruise].state is… false: LOW, 0 and PRESSED.

Set the initial state to ( !PRESSED) . The '!' is the logical negation operator, or you can say (not PRESSED).

<after food>

I now see you do store a lastState by reading the pin at the very beginning. Good!

So you may just be able to set state at the same time. That is, after all, not a lie.

Maybe just do it exeplicilty for obviousness and clarity:


  // setup Pin Modes
  for (int i = 0; i < NoOfSwitches; i++) {
    Switch[i].No = i + 1;

    Switch[i].setPin(SwitchPin[i]);

    Switch[i].state = digitialRead(SwitchPin[i])) == PRESSED;  // probably not!
    Switch[i].lastState = Switch[i].state;
  }
 

a7

Thanks! I hadn't even rembered I was storing that - is still throwing a small error on that new line though:

exit status 1
expected ';' before ')' token

It looks like the semi-colons are there though....

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

Sry sry sry, I edited after testing, as far as I could, my fix.

There's two )) in that line I quote, one would do:

Switch[i].state = digitalRead(SwitchPin[i]) == PRESSED;

I'm under the umbrella, but that is def wrong, and the essence of my idea should be correct.

Small syntax errors can give the most misleading error messages, even when the error messages aren't. Misleading.

I tried to make a runnable tested version, and used your TEST… to compensate for missing hardware. Nice idea, but doesn't go quite far enough, and might be hard to make it do.

So I did have a chopped down version that worked fine. TBH I am not sure where the extra parenthesis came from. There was a giant bag full of them right on the workbench when I was making the tests, musta spilled some.

a7

No worries...I think its almost there.

Below is the code so far - everything does what its supposed to do - only wrinkly is that it plays the ELSE section right after the Power up MP3 on startup - after that, everything does what its supposed to.


// 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 = 1;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {22}; // pins of the switches
enum {Cruise};  // Enumeration that keeps track of the human switch naming


void setup() {
 // put your setup code here, to run once:
pinMode (26, OUTPUT);
pinMode (28, OUTPUT);
pinMode (30, OUTPUT);
pinMode (32, OUTPUT);
pinMode (34, OUTPUT);
pinMode (36, OUTPUT);
pinMode (38, OUTPUT);

// setup Pin Modes
  for (int i = 0; i < NoOfSwitches; i++) {
    Switch[i].No = i + 1;
    Switch[i].setPin(SwitchPin[i]);
    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
}

//decalre variables
bool startup = false;



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

// Play the Startup sound one time on power up
if (startup == false) {
      Serial.println(F("Playing track 001"));
      musicPlayer.playFullFile("/track001.mp3");
      startup = true;
}
handleSwitchCruise();
ResumeBridgeTrack();
}

void handleSwitchCruise(){
  if (Switch[Cruise].changed()) {
   if ( Switch[Cruise].state == PRESSED ) {
      StartMP3("track002.mp3");
      delay(1000);
      digitalWrite(26, HIGH);
      delay(1750);
      digitalWrite(28, HIGH);
      delay(2200);
      digitalWrite(30, HIGH);
      delay(3000);
      digitalWrite(32, HIGH);
      delay(3900);
      digitalWrite(34, HIGH);
      delay(9000);
      digitalWrite(36, HIGH);
      delay(10000);
      digitalWrite(38, HIGH);
      }
   else 
      {
      StartMP3("track022.mp3");
      delay(5000);
      digitalWrite(26, LOW);
      digitalWrite(28, LOW);
      digitalWrite(30, LOW);
      digitalWrite(32, LOW);
      digitalWrite(34, LOW);
      digitalWrite(36, LOW);
      digitalWrite(38, LOW);
      }
   } 
 {
  };
}


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

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

OK, little bit sun-dangled here.

I'll not be where I can look closer at the code for awhile. For now, could you try

    Switch[i].state = !PRESSED;  // note excalmation point!
    Switch[i].lastState = Switch[i].state;

until I figure out where I've gone upside down myself. Your button object is intriguing.

In case it isn't obvious, keep you finger off the button until the thing is running. All the digitalRead() was doing went for being able to start up with the button held down, so for now don't do that and the above code should do something.

Maybe something different and desirable. :expressionless:

When we fail to fix this, I will suggest a giant kludge or kluge of a hack. Hate to, but.

a7

That did it!!! Thanks so much!

Now, onto adding buttons for Phasers, Photons, sound effects, tribbles to make the lights randomly flash, Arduino is such fun stuff to play with!!!!

Phew!

Please post a current complete sketch that works, before you start your planned additions.

Could you also just post a list of the tracks and their approximate durations?

I note that some of the timing and sequencing is just happening automagically. I think with too much more placed on this vehicle there will be some unintended consequences and it may become increasing difficult to add features and keep,it functional.

I have looked at the code. In doing that, I recast the button code a bit, I'd like to see if my changes to it would still work. and I've managed to simulate most of the hardware. The sketch and other info I request will be useful in that effort.

TIA

a7

Sure, Ive attached the code. Here's some clarification:

track001 - startup sound, plays on time at startup
track002 - plays the Cruise sequence (rocker on)
track003 - bridge background noise (rocker on
track022 - power off sequence (rocker off)

When the rocker turns on, it starts the Cruise MP3 (the opening music from Start Trek) and then turns on the LEDs in sequence (hence the delays, to work in time with the music)
LED1 - lights the bridge
LED2 - lights the saucer
LED3 - lights the support column for the saucer
LED4 - lights the bottom hull
LED5 - lights the nav lights and strobes
LED6 - lights the bussard collectors
LED7 - lights the warp lights on the side of the nacelles

Then after the theme music s done, it starts looping the bridge background noise (track003.)

Hope that makes sense what I'm doing - I can always post a video of my breadboard in action.

Next steps are:

Phaser button - momentary push button, flashes two LEDs quickly, and plays track006
Photon button - momentary push button, flashes one LED and plays track007
Red alert button - momentary push button, flashes one LED and plays track008
Tribble button - momentary push button, flashes the first 4 LEDs randomly, and plays track009
Shuttle button - momentary push button, lights one LED and plays track010
Sound effects - momentary push button, flashes one LED and plays a playlist of a sequence of tracks.

Have the code done for most everything above, but thats my long term plan.

// 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 = 1;
SwitchType Switch[NoOfSwitches];        // Declaration of the switches
byte SwitchPin[NoOfSwitches] = {22}; // pins of the switches
enum {Cruise};  // Enumeration that keeps track of the human switch naming


void setup() {
 // put your setup code here, to run once:
pinMode (26, OUTPUT);
pinMode (28, OUTPUT);
pinMode (30, OUTPUT);
pinMode (32, OUTPUT);
pinMode (34, OUTPUT);
pinMode (36, OUTPUT);
pinMode (38, OUTPUT);

// setup Pin Modes
  for (int i = 0; i < NoOfSwitches; i++) {
    Switch[i].No = i + 1;
    Switch[i].setPin(SwitchPin[i]);
    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
}

//decalre variables
bool startup = false;



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

// Play the Startup sound one time on power up
if (startup == false) {
      Serial.println(F("Playing track 001"));
      musicPlayer.playFullFile("/track001.mp3");
      startup = true;
}
handleSwitchCruise();
ResumeBridgeTrack();
}

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


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

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

Then turning the rocker off plays the shutoff noise, and then turns off all 7 LEDs (simulating a blackout).

THX. Good beach reading I hope.

One thing you can do is move that once and only once thing into setup(), which is designed for once and only once stuff, viz:

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

      Serial.println(F("Playing track 001"));
      musicPlayer.playFullFile("/track001.mp3");

#endif
}

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

Another thing you can now do is remove the comment

  // put your main code here, to run repeatedly:

You are no longer a noob. :wink:

a7

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