Millis() VS. Delay() - what's the difference

In programming, millis() and delay() have the following differences:

millis():

  • millis() is a function that returns the number of milliseconds since the program started running.
  • It provides a way to measure time intervals without blocking the execution of the program. You can use it to keep track of elapsed time or implement time-based operations without pausing the program flow.
  • For example, you can use millis() to check if a certain amount of time has passed and then perform an action. This allows other parts of the program to continue running in the background.
  • It is useful for applications where you need to perform tasks at specific time intervals or implement non-blocking timers.

delay():

  • delay() is a function that pauses the program execution for a specified number of milliseconds.
  • When delay() is called, the program stops and waits for the specified time period before continuing.
  • For instance, if you call delay(1000), the program will pause for one second.
  • While delay() is easy to use for simple tasks that require a fixed delay, it can block the execution of the program and prevent other parts of the code from running during the delay period.
  • This can be a limitation in applications where you need to perform other tasks simultaneously or respond to external events while waiting for the delay to complete.

In summary, millis() provides a non-blocking way to measure time and implement time-based operations, while delay() pauses the program execution for a specified time period. The choice between them depends on the specific requirements of your program and whether you need to keep the program running continuously or can afford to block its execution.