Arduino Programming – Interval

In this video, I explain to you how to write an Arduino interval timer. A program that fires actions with different intervals without using the delay() function. The problem with using delay is that it stops the “loop” for a specific amount of time. This makes that you won’t be able to respond to changes in your sensors. By programming an interval based on millis(), you can keep running the Arduino loop as quickly as possible.

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.

Arduino Programming - Interval Tutorial
Watch this video on YouTube.

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

This example shows how to utilise an array to provide multiple intervals.

/*
  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; 
            
    }
  }
}


5 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?

  2. Ik wil vragen of het mogelijk is met één van deze voorbeelden en aanpassingen een digitale input te meten met een tijdinterval van 25 seconden en afhankelijk van de 2 waardes een actie te ondernemen.
    Wil namelijk een afgeleide (5v dc) van de netspanning meten op een inputpin en na 25 seconden nog een keer om zeker te weten dat het geen spike was, en dan een generator starten via de Arduino en zodra de netspanning weer terug is de generator stoppen.

  3. Using timer0, write a program (without using interrupts) that prints the time every 0.015 seconds. Use timer0 to ensure the 0.015 second interval as closely as possible. Use micros() to record and print out the time.
    Please send solution
    thank you

Leave a Comment

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