arduino millis

millis() is a function in the Arduino programming language (based on C++) that returns the number of milliseconds that have passed since the Arduino board started running. It is commonly used for timing and scheduling tasks in Arduino projects. Here is an example of how millis() can be used:

void setup() {
  // setup code goes here
}

void loop() {
  unsigned long currentMillis = millis();

  // code to be executed repeatedly goes here

  if (currentMillis >= 1000) {
    // code to be executed after 1 second (1000 milliseconds) goes here

    // reset the timer
    currentMillis = 0;
  }
}

In this example, the setup() function is called once when the Arduino board starts running, and the loop() function is called repeatedly. Inside the loop() function, the millis() function is used to get the current number of milliseconds. This value is then compared to a threshold (in this case, 1000 milliseconds) to determine if a certain amount of time has passed. If the condition is true, a specific code block is executed.

By using millis() to keep track of time instead of relying on delay() function, you can create non-blocking code that can perform multiple tasks simultaneously. This is especially useful in projects that require precise timing or need to respond to external events while executing other code.