1

My current function is supposed to play a sound effect, then after the sound effect is done (14 seconds), perform the next action (digitalWrite(CONTROLLINO_R1, LOW)).

if (digitalRead(CONTROLLINO_A8) == HIGH && !skeleLatch) {
  skeleLatch = 1;
  wTrig.trackPlayPoly(2);
  unsigned long currentMillis = millis();
  if((currentMillis-previousMillis) >= 14000) {
    digitalWrite(CONTROLLINO_R1, LOW);
    previousMillis = millis();
  }
}

As it is, it performs both at the same time. I do not know how to correctly implement the delay so that it waits.

Any help would be appreciated. Thank you.

5
  • If your program shall not do anything during 14 seconds, why not simply use delay()?
    – jfpoilpret
    Jan 28 '17 at 17:00
  • its going into an escape room - meaning, there is a good chance someone else might activate something else while that music is playing. In the off chance, i'd like to be sure it doesn't interfere with any other inputs Jan 28 '17 at 17:02
  • You can get the current time (milliseconds since startup) using millis(). Using that you can tell whether it's been 14 seconds since a particular event. Does that give enough of a clue?
    – Mark Smith
    Jan 28 '17 at 18:46
  • Where do you set up previousMillis (apart from after the decision in the code you posted)?
    – Nick Gammon
    Jan 29 '17 at 2:10
  • previousMillis was set to 0 unsigned long outside loop Jan 29 '17 at 17:13
2
  unsigned long triggerMillis = 0;

  if (digitalRead(CONTROLLINO_A8) == HIGH && !skeleLatch)
  {
    skeleLatch = 1;
    wTrig.trackPlayPoly(2);
    triggerMillis = millis();
  }

  if (millis() - triggerMillis >=14000 && skeleLatch)
  {
    digitalWrite(CONTROLLINO_R1, LOW);
    skeleLatch = 0;
  }

This should work, depending on how long your CONTROLLINO_A8 is staying high you might want to invest some more time to set up an interrupt on that pin, to make sure you don't miss a trigger.

0

If your code does not need to be re-entrant (work within a multitasking environment), the simpler solution is Arduino's delay function, designed specifically for waiting a specific number of milliseconds.

delay(14000)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.