# delay, but without delay.

So I was doing some research online, and I looked at “while”, and I believe that it is a suitable replacement for delay, without pausing everything else on the board

``````while(millis() < millis() + 1000){}
// this while should pause for 1 second, if I'm correct.
``````

So can anyone tell me if I’m using it correctly?

“ can anyone tell me if I'm using it correctly?”

Not correct.

(deleted)

So then whats the best way to delay, but letting other processes happen aswell? I would also like to be able to put it into a function so it's easier to call.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

(deleted)

``````  //save current time
currentMillis = millis();

//********************************
//to help show if there is any blocking code, this LED toggles every 500ms
if (currentMillis - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = currentMillis;

digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));

}

. . .
``````

You’ve designed a delay of around 49.7 days, when the 32-bit unsigned integer used to represent the number of milliseconds overflows back to zero.
There’s no other scenario where “x + 1000” is ever smaller than “x”.
It also shows why should never add two times together, only subtract.

The correct “delay”-equivalent would be:

``````unsigned long start = millis();
while (millis() - start < 1000);
``````

It still pauses everything on the board, though, see the “Blink Without Delay” example for an alternative.

By the way, Arduino is open-source, so you don’t have to guess how delay is implemented:
Source

``````void delay(unsigned long ms)
{
uint32_t start = micros();

while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
``````

Pieter

SkyCrafter:
So I was doing some research online, and I looked at “while”, and I believe that it is a suitable replacement for delay, without pausing everything else on the board

``````while(millis() < millis() + 1000){}
``````

// this while should pause for 1 second, if I’m correct.

``````

So can anyone tell me if I'm using it correctly?
``````

Do you want “delay” or “all functions running”?
See, these are opposite of each other.

The usage of millis function is for a reason.

The delay function blocked the “functions running in background” so that was a problem.

So today’s programmers use the millis function instead of delay, BECAUSE “it doesn’t block the processes running in background”

… Arnav

And there is Really no process running in the background... it’s just the same and one process... so you stop it then it’s stopped...if you don’t want that then don’t stop it.

In simple words, the difference between delay and millis is-

Delay - delays the process (stops the process)

millis - waits for a condition (doesn't stop the process)

..Arnav

ArnavPawarAA:
millis - waits for a condition (doesn't stop the process)

millis in itself does not wait for anything... it just provides an unsigned long value representing the number of milliseconds since booting.

The way you use millis() lets you achieve time management and subtasks scheduling.

delay -- are you saying about any arbitrary amount of timeDelay?

(without) delay -- are you saying about delay(); function?

Please, change/edit the Title of your thread accordingly.

(The Title could be like this: Time Delay without delay() function.)

J-M-L:
millis in itself does not wait for anything... it just provides an unsigned long value representing the number of milliseconds since booting.

The way you use millis() lets you achieve time management and subtasks scheduling.

In the way we use millis(), we use a while() function. You know what the while function does, waits for a condition. So the same way - when we use the millis() function as an alternative of delay, we use a condition in the while() statement that is - wait until the specific time is passed.

Only millis() function does the work as you said, but you know that how people use the millis() as an alternative to delay. I said how it works in that way.

..Arnav

There is no “while function” - it is a while loop.

And a while loop is seldom used in functions that do timing with millis() . See these tutorials.
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

TheMemberFormerlyKnownAsAWOL:
There is no "while function" - it is a while loop.

Yes yes yes yes yes. It's a while loop.......

Millis does not work the way you wrote it. It’s the rest of the code that performs the expected time management. Programming requires precision.

ArnavPawarAA:
You know what the while function does, waits for a condition.

To be precise (and pull a bit your leg ) the while keyword does not wait for anything either... A while keyword introduces an Iteration statement which repeatedly executes a target statement as long as a given condition is true.

In most cases we would usually use a Selection statement (introduced by if keyword), not a while loop though. The repetition is provided by the loop() function.

Using the right terminology helps

J-M-L:
To be precise (and pull a bit your leg ) the while keyword does not wait for anything either... A while keyword introduces an Iteration statement which repeatedly executes a target statement as long as a given condition is true.

As the usage of while loop used with millis, the while loop has a condition but doesn't have to execute anything until the condition is true So in the loop section there is almost no code (instead of the calculations) in the loop section.

So technically the while loop waits for the condition and at the same time, doesn't block the process like a delay. Think again how it works (if only used in that way) : )

Nothe that I am not talking about the original while() loop, I am taking about the way it is used with the millis() function.

..Arnav

Show some real code to illustrate the usage of a while loop used with millis().