How to use millis() instead of delay() | Arduino FAQs

PHOTO EMBED

Sun Aug 08 2021 16:03:12 GMT+0000 (UTC)

Saved by @mongol #millis #delay

#define EXE_INTERVAL_1 1000
#define EXE_INTERVAL_2 3000

unsigned long lastExecutedMillis_1 = 0; // vairable to save the last executed time for code block 1
unsigned long lastExecutedMillis_2 = 0; // vairable to save the last executed time for code block 2

void setup() {
  /*******************
   *  your setup code
   *******************/
}

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

  if (currentMillis - lastExecutedMillis_1 >= EXE_INTERVAL_1) {
    lastExecutedMillis_1 = currentMillis; // save the last executed time

    /********************
     * your code block 1
     ********************/
  }

  if (currentMillis - lastExecutedMillis_2 >= EXE_INTERVAL_2) {
    lastExecutedMillis_2 = currentMillis; // save the last executed time

    /********************
     * your code block 1
     ********************/
  }
}
content_copyCOPY

https://arduinogetstarted.com/faq/how-to-use-millis-instead-of-delay