Halloween Box Help

This is very basic but I hope does demonstrate my limited objective described earlier.

It's heavily commented but in summary:

  1. Starts silently on power-up. But see options.

  2. On pressing the white button it plays whatever track you want associated with a visitor entering the first zone. You'd replace the button for example with a low-going sensor generated input. A brief flash on the red LED was used during debugging, and remains in the skletch.

  3. On pressing the yellow button it plays whatever track you want associated with a visitor entering the inner zone. You'd replace the button for example with a low-going sensor generated input, such as your ultrasonic distance detector. (I think my preference would be a carefully positioned PIR, or IR Tx/RX pair.) A brief flash on the yellow LED was used during debugging, and remains in the sketch.

  4. The brief (300 ms?) gap after pressing a button before the other sound takes over doesn't seem a problem IMO.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

const byte redLedPin =  10;
const byte yelLedPin = A2;
const byte whtButtonPin = 6;
const byte yelButtonPin = 7;

bool whtButtonState = HIGH;
bool yelButtonState = HIGH;

SoftwareSerial mySoftwareSerial(8, 9); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
/////////////////////////////////////////////////////////////////////
void setup()
{
  pinMode(whtButtonPin, INPUT_PULLUP);
  pinMode(yelButtonPin, INPUT_PULLUP);
  pinMode(redLedPin, OUTPUT);
  pinMode(yelLedPin, OUTPUT);

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  delay(200);
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial, true, false)) //Use softwareSerial
  {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));
  myDFPlayer.setTimeOut(1500); //Set serial communicataion time out
  myDFPlayer.volume(20);  //Set volume value (0~30).

  // NOTE: Even if not required, it's usually necessary to play a single file
  // to ensure module is reliably started. Illogical, but seems needed.
  // Of course, it could be silent, or unrecognisably brief.
  myDFPlayer.play(28); // Initial brief signature sound
  // Or a 'background' sound, until interrupted permanently by later logic.
  Serial.print("setup ended");
  Serial.println();
} // End setup
/////////////////////////////////////////////////////////////////////
void loop()
{

  blinkBothLEDsRepeatedly(); // Waiting for further action

  yelButtonState = digitalRead(yelButtonPin); // Controls yYellow LED

  whtButtonState = digitalRead(whtButtonPin); // Controls red LED

  playRelevantSoundFromYelButton();

  playRelevantSoundFromWhtButton();

  delay(100);
} // End loop

/////////////////////////////////////////////////////////////////////
// FUNCTIONS
void  playRelevantSoundFromYelButton()
{
  // state of yellow button was read in loop; (pressed delivers LOW voltage)
  // yellow button not held down emulates out of range
  // flash yellow LED & play appropriate sound, e.g. saber hum
  if (yelButtonState == 0) // held down
  {
    Serial.print("yellow button is held down");
    Serial.println();
    flashYelLED();
    myDFPlayer.play(26); // 58 s, deep bass hum, 25-saber-humming.mp3
  }
} // end playRelevantSoundFromYelButton
/////////////////////////////////////////////
void playRelevantSoundFromWhtButton()
{
  // state of white button was read in loop; (pressed delivers LOW voltage)
  // white button held down emulates out of range/PIR inactive/etc)
  // flash the red LED once and play appropriate sound, e.g. laser sword
  if (whtButtonState == 0) // held down, so high 5V voltage
  {
    Serial.print("white button is held down");
    Serial.println();

    flashRedLED();
    myDFPlayer.play(25); // 17 s, deep bass hum, laser-sword-SF.mp3
  }
} // end playRelevantSoundFromWhtButton
/////////////////////////////////////////////
void flashRedLED()
{
  digitalWrite(redLedPin, HIGH); // flash the red LED once as an indicator
  delay(1000);
  digitalWrite(redLedPin, LOW);
}
/////////////////////////////////////////////
void flashYelLED()
{
  digitalWrite(yelLedPin, HIGH); // flash the yellow LED once as an indicator
  delay(100);
  digitalWrite(yelLedPin, LOW);
}
/////////////////////////////////////////////
void blinkBothLEDsRepeatedly() // Redundant/optional; e.g. use for:
// simply show the sketch is powered and basically working
// Indicate waiting for further button action;
// Debugging indicator for a sensor
{
  digitalWrite(yelLedPin, millis() % 500 > 250); // Choose, trial/error
  digitalWrite(redLedPin, millis() % 400 > 100);
}
/////////////////////////////////////////////

NOTES:

  • Buttons do not need to be 'held down'. The switch takes place on first press.
  • Obviously, duration of tracks must be considered. My two are 58 amd 17 seconds respectively.
  • I did not use the othet two MP3s I uploaded earlier.
  • I left a few lines that were no longer needed; now corrected.