Arduino Programming – Interval

How to write a program that fires actions with different intervals without using the delay() function. Configuration of the serial port and printing values is also explained.

I explain you how to write a program that fires actions with different intervals without using the delay() function. Configuration of the serial port and printing values is also explained.

Level: beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables ).

Interval_basic.ino:

/*
  Print serial text with an interval.
  Basic example.   
  
  created 01-12-2009 by kasperkamperman.com
*/

unsigned long previousMillis  = 0;
unsigned long currentMillis   = 0;

int interval = 1000;

void setup() {
  // opens serial port, sets data rate to 57600 bits per second
  Serial.begin(57600);   
}

void loop()
{ // save number of milliseconds since the program started
  currentMillis = millis();
  
  // check to see if the interval time is passed. 
  if (currentMillis - previousMillis >= interval == true ) {
    
      Serial.print("this program runs now for : ");
      Serial.print(currentMillis);
      Serial.println(" milliseconds");
      
      // save the time when we displayed the string for the last time
      previousMillis = currentMillis;
  }
}

Interval_for_loop.ino:

/*
  Print serial text with different intervals.   
  Check the array with a for-loop.
  
  created 01-12-2009 by kasperkamperman.com
*/

const int amountOfIntervals = 3;

unsigned long previousMillis[amountOfIntervals]  = {0, 0, 0};
unsigned long currentMillis                      = 0;

int intervals[amountOfIntervals]       = {1000, 2500, 5000};

void setup() {
  // opens serial port, sets data rate to 57600 bits per second
  Serial.begin(57600);   
}

void loop()
{ // save number of milliseconds since the program started
  currentMillis = millis();
  
  // check the 3 intervals one by one with a for-loop. 
  for(int i=0; i<amountOfIntervals; i++) { // check to see if the interval time is passed. if (currentMillis - previousMillis[i] >= intervals[i]) {
      
      if(i==0) Serial.println("-   every 1 second");
      if(i==1) Serial.println(" -  every 2.5 second");
      if(i==2) Serial.println("  - every 5 seconds");
      
      // save the time when we displayed the string for the last time
      previousMillis[i] = currentMillis; 
            
    }
  }
}
Buy Me a Coffee at ko-fi.comWas this article useful to you? Buy me a coffee!

2 thoughts on “Arduino Programming – Interval”

  1. How to use this function to run two different independent events. for example cooling and heating of air conditioner. If temperature is greater than a specified value let’s say 30C, than check the time from last stop to now, if time is greater than lets say 5 minutes than start cooling other wise wait for completion of 5 minutes.
    similarly if temperature is less than a specified low temperature lets say 5c. than before starting heating check if time from last stop to now is greater than 2 minutes or not, if yes than start heating otherwise wait for completion of 2 minutes.
    some other functions may also be called within these functions like lcd data blinking.
    The point is the interval of cooling should not effect heating interval and vice a versa. have any idea?

Leave a Reply

Your email address will not be published. Required fields are marked *

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