Using millis() instead of delay

millis() function returns the number of milliseconds the current sketch has been running since the last reset.

millis() returns an unsigned long.

#define MEASURE_INTERVAL 1000   // 
#define SEND_INTERVAL 16000   // 

unsigned long previous_millis_measure = 0;
unsigned long previous_millis_send = 0;

boolean measure_state = false;
boolean send_state = false;
void loop(void) 
  unsigned long current_millis = millis();
  if ((unsigned long)(current_millis - previous_millis_measure) >= MEASURE_INTERVAL)
    measure_state = true;
    previous_millis_measure = current_millis;

  if ((unsigned long)(current_millis - previous_millis_send) >= SEND_INTERVAL)
    send_state = true;
    previous_millis_send = current_millis;

  if (measure_state){ // == if true
    // measure something
    measure_state = false;

  if (send_state){ // == if true
    // send something
    send_state = false;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

close-alt close collapse comment ellipsis expand gallery heart lock menu next pinned previous reply search share star