Start if statement after 10 seconds

Dear Community

I’d like to start the first if-statement in the checkImpact methode 10 seconds after the variable impact gets true. I want to make it with millis(), but I am not sure how to implement it.

Thank you in advance. :slight_smile:

void checkImpact(){

  const int maxDelta = 150;
  const int minDelta = - 150;
  int count1;
  int count2;
  
  for(int i = 0; i < 5; i++){
    if((impact == true  && deltaX <= maxDelta && deltaX >= minDelta && deltaY <= maxDelta && deltaY >= minDelta && deltaZ <= maxDelta && deltaZ >= minDelta) && momSpeed <= 1){
      delay(1000);
      count1 ++;
      if (count1 == 5){
        emergency = true;
        count1 = 0;
      }
      }else{
        impact = false;
    }
    for(int j = 0; j < 5; j++){
      if((emergency == true  && deltaX <= maxDelta && deltaX >= minDelta && deltaY <= maxDelta && deltaY >= minDelta && deltaZ <= maxDelta && deltaZ >= minDelta) && momSpeed <= 1){
        delay(1000);
        count2 ++;
        if (count2 == 5){
          codeRed = true;
          count2 = 0;
        }
      }else{
        emergency = false;
        impact = false;
      }
    }
  }    
}

Your requirement doesn't make sense. You can record WHEN impact is set to true. But, your whole function makes no sense. You have a variable, count1, with no known initial value. You increment that value, and then test whether the garbage that is in the variable is now 5. That makes no sense.

Local variables are NOT given meaningful initial values unless YOU give them meaningful initial values, which you don't.

You don't "start an if statement". You evaluate an if statement.

Have a look at how millis() is used to manage timing in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.

At the top of your IF block you could have

if (millis() - impactTime >= 10000) {

which assumes that impactTime is set when impact becomes true

...R