Adding a delay to a reed switch activation

Hello, I'm fairly new and am more accustomed to tinkering than I am writing code completely from scratch. The code I currently need to modify currently functions like this:

  1. 4 reed switches will be activated by magnets
  2. This triggers stopping an audio file and starting another audio file
  3. After the audio file plays, there is a delay that would then switch the status to "complete" which triggers LED lights to light up in sequence.

My goal is to add in a delay in the beginning, so that if 4 reed switches are activated by the magnet, the "trigger" doesn't activate unless the magnets contact the reed switches for a specific period of time (let's say 2 seconds). Do you have any suggestions on how that can be added to the code easily without rewriting the entire code?

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

// Arduino pins allocation
const int triggers = 2;
const int blacklight = 4; 
const int speaker_rx = 6;
const int speaker_tx = 7;

const int LED0 = 8;
const int LED1 = LED0 + 1;
const int LED2 = LED0 + 2;
const int LED3 = LED0 + 3;
const int LED4 = LED0 + 4;

// Flashing LED sequence
const int numDigits = 8;
const int puzzleSequence[numDigits] = {0,1,2,1,3,3,4,0};

// Speaker
SoftwareSerial mySoftwareSerial(speaker_rx, speaker_tx); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

// State 
int sequenceIter = 0;
bool puzzleCompleted = false;
unsigned long ledLightTime = 1000;
unsigned long ledFlashTimer = millis();
unsigned long ledTransitiontimer = millis();

void setup()
{
  pinMode(triggers, INPUT);
  pinMode(blacklight, OUTPUT);
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);


  // Setup speaker
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    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.volume(30);  //Set volume value. From 0 to 30*/
  myDFPlayer.loop(1);
}

void loop()
{
  if (!puzzleCompleted)
  {
    if (digitalRead(triggers) == HIGH)
    {
      myDFPlayer.disableLoop();
      myDFPlayer.stop();
	  delay(1000);
	  
      myDFPlayer.play(2);
      myDFPlayer.loop(2);
      myDFPlayer.enableLoop();
      
      delay(3000);
      puzzleCompleted = true;
    }
  }
  

  if (puzzleCompleted)
  {
    digitalWrite(blacklight, HIGH); 

    // Light up the LED sequence
    int ledStrip = puzzleSequence[sequenceIter] + LED0;
    char data[100];
    sprintf(data, "Lighting LED on pin [%d].", ledStrip);
    Serial.println(data);

    unsigned long currentTime = millis();
    for (int i = LED0; i <= LED4; i++)
    {
      if (i == ledStrip)
      {
        if (currentTime - ledFlashTimer < ledLightTime)
        {
          digitalWrite(i, HIGH);
        }
        else
        {
          digitalWrite(i, LOW);
        }
      }
      else
      {
        digitalWrite(i, LOW);  
      }
    }
    if (currentTime - ledTransitiontimer > (ledLightTime + 1000))
    {
      ledTransitiontimer = currentTime;
      ledFlashTimer = currentTime;
      sequenceIter++;
      if (sequenceIter >= numDigits)
      {
        sequenceIter = 0;
      }
    }
  }
  else
  {
    digitalWrite(blacklight, LOW); 
    for (int i = LED0; i <= LED4; i++)
    {
      digitalWrite(i, LOW);
    }
  }
}

You have to keep track of when the switches go HIGH and see how long it has been. Something like this:

//#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

// Arduino pins allocation
const int triggers = 2;
const int blacklight = 4;
const int speaker_rx = 6;
const int speaker_tx = 7;

const int LED0 = 8;
const int LED1 = LED0 + 1;
const int LED2 = LED0 + 2;
const int LED3 = LED0 + 3;
const int LED4 = LED0 + 4;

// Flashing LED sequence
const int numDigits = 8;
const int puzzleSequence[numDigits] = {0, 1, 2, 1, 3, 3, 4, 0};

// Speaker
SoftwareSerial mySoftwareSerial(speaker_rx, speaker_tx); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

// State
int sequenceIter = 0;
bool puzzleCompleted = false;
const unsigned long ledLightTime = 1000;
unsigned long ledFlashTimer = millis();
unsigned long ledTransitiontimer = millis();
unsigned long triggerStart;
const unsigned long triggerDelay = 2000;
int prevTrig;

void setup()
{
  pinMode(triggers, INPUT);
  pinMode(blacklight, OUTPUT);
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);


  // Setup speaker
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    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.volume(30);  //Set volume value. From 0 to 30*/
  myDFPlayer.loop(1);
}

void loop()
{
  int trig = digitalRead(triggers);
  if ( trig != oldTrig )
  {
    // new value so reset timer
    trigStart = millis();
  }

  if (!puzzleCompleted)
  {
    if (trig == HIGH)
    {
      if ( millis() - triggerStart >= triggerDelay )
      {
        myDFPlayer.disableLoop();
        myDFPlayer.stop();
        delay(1000);

        myDFPlayer.play(2);
        myDFPlayer.loop(2);
        myDFPlayer.enableLoop();

        delay(3000);
        puzzleCompleted = true;
      }
    }
  }

  if (puzzleCompleted)
  {
    digitalWrite(blacklight, HIGH);

    // Light up the LED sequence
    int ledStrip = puzzleSequence[sequenceIter] + LED0;
    char data[100];
    sprintf(data, "Lighting LED on pin [%d].", ledStrip);
    Serial.println(data);

    unsigned long currentTime = millis();
    for (int i = LED0; i <= LED4; i++)
    {
      if (i == ledStrip)
      {
        if (currentTime - ledFlashTimer < ledLightTime)
        {
          digitalWrite(i, HIGH);
        }
        else
        {
          digitalWrite(i, LOW);
        }
      }
      else
      {
        digitalWrite(i, LOW);
      }
    }
    if (currentTime - ledTransitiontimer > (ledLightTime + 1000))
    {
      ledTransitiontimer = currentTime;
      ledFlashTimer = currentTime;
      sequenceIter++;
      if (sequenceIter >= numDigits)
      {
        sequenceIter = 0;
      }
    }
  }
  else
  {
    digitalWrite(blacklight, LOW);
    for (int i = LED0; i <= LED4; i++)
    {
      digitalWrite(i, LOW);
    }
  }
}

Learn about programming state machines - this is how to implement real-world behaviours
like this - once you have figured out the state-transition diagram you'll understand your system
better too (ie figure out the awkward edge cases).

Thank you to both of you for the advice

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