The use of millis

hello, I want that if a condition when it is correct wait 5 seconds and then it is executed, with the delay function I do it well, but it is blocking all the work in the loop, I want to do it with millis, can you help me

Welcome to the forum

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

As to your question

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

you can use a timer that compares the current time to a timestamp captured at some event to see if their difference is 5000 msec

I can't get to coding the formulas

unsigned long startTime;
const unsigned long delayTime = 5000;

void loop() {
  if ( <condition> ) {
    startTime = millis();
  }
  // if startTime is 0, then condition has not been met yet
  // if it is non-zero, then test for enough elapsed time
  if ( startTime && millis() - startTime >= delayTime ) {
    doSpecialThing();
    startTime = 0;  // timer off
  }
  ...
  // rest of loop
}

This code will continously reset your startTime as long as your condition is met. If you don't want this, you can only test for the condition if startTime is zero just like the other part of the code.

1 Like

Posting an image of code is never a good idea.
How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

if(webmesage1 == true || webmesage2 == true || webmesage3 == true || webmesage4 == true || webmesage5 == true || webmesage6 == true || webmesage7 == true ){

sirena = true;

starttime = millis();

digitalWrite(output4, HIGH);

}

if(output4 == HIGH){

if(millis() < starttime + 6000){

digitalWrite(output4, LOW);

}

}

You have a lot missing... would you post your complete sketch?

I want that if a condition is true, the code inside the brackets waits for 5 seconds and then executes, can you help me how to do it?

#define DISABLED false
#define ENABLED  true

. . .

//when the TIMER is not timing, is all that stuff valid ?
if (timingFLAG == DISABLED && allOtherSuff == true)
{
  //enable TIMER
  timingFLAG = ENABLED;
  
  //restart the TIMER
  starttime = millis();

  digitalWrite(output4, HIGH);
}

//when we are timing, is it time to stop ?
if(timingFLAG == ENABLED  &&  millis() - starttime >= 6000)
{
  //we are finished with this TIMER
  timingFLAG = DISABLED;
 
 digitalWrite(output4, LOW);
}

thanks for your time, but it's not working for me and I can't figure out why it's not working

  • Well, as stated, we cannot see your sketch. :pleading_face:

because it's not in English and it's not very neat, I just couldn't understand the millis function how it works inside the if statement

I want that if a condition is true, the code inside the brackets waits for 5 seconds and then executes

blocking code:

if (condition) {
 delay(5000);
 digitalWrite(output4, LOW);
}

with millis

Everything in computer languages is in English. Device names, function names and comments do not effect the program.

Post your complete sketch.

unsigned long timer, timeout = 1000;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // millis() is NOW, timer is "last" time it happened, timeout = 1000ms
  if (millis() - timer > timeout) { // if difference of LAST time to NOW is > 1000ms...
    timer = millis(); // ...timeout has occurred, and reset LAST TIME timer
    Serial.println("BLINK"); // blink something
  }
}

@piratzaskoku, do not cross-post. Threads merged.