Strange behavior when starting a sound-trigger project

I’ve got a simple project that uses a Sparkfun WAV Trigger to initiate sounds when 3 switches are turned on. I have 3 sounds that simulate a helicopter starting up, idling, and then shutting down. The idea is when you flip on all 3 switches, the startup sound begins (which takes 45 seconds to complete), then the idle sound takes over and plays until one of the switches is turned off. At that point, the shutdown sound plays (which is 15 seconds long).

From the code it seems like everything is working OK in almost every test, but every once in a while, when I turn the 3 switches on, it triggers the shutdown sound immediately. When I turn all the switches off and then back on, the startup sound begins as expected.

I’m applying power to this circuit via a key switch because it’s only supposed to be used at certain times, and a friend of mine made the suggestion that I might need one or two capacitors to smooth out the initial state, but I’ve never used a capacitor. Based on the code and breadboard mock-up below, can anyone see what else might be the problem?

int switch1State = 0;
int switch2State = 0;
int switch3State = 0;
unsigned long currentMillis = 0;
unsigned long startMillis = 0;
unsigned long startupLength = 45000;
unsigned long endMillis = 0;
unsigned long endLength = 10600;
int watcher1 = 0;
int watcher2 = 0;
int watcher3 = 0;

void setup() {
  pinMode(5,INPUT);     //Leftmost Switch (This is the running lights ON switch)
  pinMode(4,INPUT);     //Middle Switch   
  pinMode(3,INPUT);     //Right Switch    (This is the helo startup switch)
  pinMode(6,OUTPUT);    //Leftmost LED
  pinMode(7,OUTPUT);    //Middle LED
  pinMode(8,OUTPUT);    //Right LED
  pinMode(9,OUTPUT);    //Helo Startup Sound for WAV Trigger
  pinMode(10,OUTPUT);   //Helo Idle Sound for WAV Trigger
  pinMode(16,OUTPUT);   //Helo Shutdown Sound for WAV Trigger
  Serial.begin(9600);
}

void loop() {
  switch1State = digitalRead(5);
  switch2State = digitalRead(4);
  switch3State = digitalRead(3);
  currentMillis = millis();
  watcher1 = digitalRead(9);
  watcher2 = digitalRead(10);
  watcher3 = digitalRead(16);
  
  if (switch1State == LOW) {
    offSwitch(6);
  }
  // Turn off running lights here
  else {
    digitalWrite(6,HIGH);
    // Turn on running lights here
  }
  if (switch2State == LOW) {
    offSwitch(7);
  }
  else {
    digitalWrite(7,HIGH);
  }
  if (switch3State == LOW) {
    offSwitch(8);
  }
  else {
    digitalWrite(8,HIGH);
  }
  if ((switch1State == HIGH) && (switch2State == HIGH) && (switch3State == HIGH)) {
    // All three switches on is the trigger for the startup sound if it isn't already playing
    if (startMillis == 0) {
      // startMillis at 0 means the startup sound isn't currently playing
      startMillis = currentMillis;
      digitalWrite(9,HIGH);           // Trigger the startup sound
    }
    if (currentMillis - startMillis >= startupLength) { 
      // Counting the seconds until the startup sound is done 
      digitalWrite(9,LOW);            // Turn off the startup sound after 45 seconds
      digitalWrite(10,HIGH);          // Turn on the idle sound (which is set to loop on the WAV trigger until interrupted by another trigger)
    }
  }
  if (currentMillis - endMillis >= endLength) {
    digitalWrite(16,LOW);
    endMillis = 0;
  }
  Serial.print("Pin 9 is: ");
  Serial.print(watcher1);
  Serial.print(" Pin 10 is: ");
  Serial.print(watcher2);
  Serial.print(" Pin 16 is: ");
  Serial.print(watcher3);
  Serial.print(" startMillis is: ");
  Serial.print(startMillis);
  Serial.print(" endMillis is: ");
  Serial.println(endMillis);
}

void offSwitch(int whichSwitch) {
  digitalWrite(whichSwitch,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  if (startMillis != 0) {
    if (endMillis == 0) {
      endMillis = currentMillis;
      digitalWrite(16,HIGH);
    }
    startMillis = 0;
  }
}

Attached is a Fritzing image of the circuit (I’m terrible at creating schematics). Disregard the label that says the sound board is an MP3 Trigger – it’s a WAV Trigger, but I haven’t made the Fritzing object for that yet.

Check your timing code. When startMillis or endMillis becomes very high, the difference may never surmount the playback length. Also take care that these variables never are set to zero by accident (when millis()==0). Even if millis() wraps around rarely, you may encounter just such unexpected conditions.

Also give your pins names, to make the code easier readable.

And try to provide a circuit diagram from Fritzing, that's much more expressive than the spaghetti cabling.

Thanks, DrDiettrich! I'll check the timing code again.

Your code would be much easier to follow if you replace this pinMode(3,INPUT); //Right Switch (This is the helo startup switch) with pinMode(heloStartPin,INPUT); Use meaningful variable (and constant) names.

I think this may be a logical problem if (currentMillis - endMillis >= endLength) {

I would add endMillis = currentMillis; as here

   if (currentMillis - startMillis >= startupLength) {
      // Counting the seconds until the startup sound is done
      digitalWrite(9,LOW);            // Turn off the startup sound after 45 seconds
      digitalWrite(10,HIGH);          // Turn on the idle sound (which is set to loop on the WAV trigger until interrupted by another trigger)
      endMillis = currentMillis;
    }

and adjust the value of endLength to take account.

...R