Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Navigation


Introduction

These special variable types automatically increase as time elapses. It's easy to check if a certain time has elapsed, while your program performs other work or checks for user input. It is also very to handle multiple tasks requiring different delays.


Usage

#include <elapsedMillis.h>

elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs

// Pin 13 has an LED connected on most Arduino boards.
int led = 13;

// delay in milliseconds between blinks of the LED
unsigned int interval = 1000;

// state of the LED = LOW is off, HIGH is on
boolean ledState = LOW;

void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

void loop()
{
	if (timeElapsed > interval) 
	{				
		ledState = !ledState;		 // toggle the state from HIGH to LOW to HIGH to LOW ... 
		digitalWrite(led, ledState);
		timeElapsed = 0;			 // reset the counter to 0 so the counting starts over...
	}
}


Source Code

The latest version of the source code is always available for download here (this is a zip file containing the current version of the code). Just download the file, and unpack the contents to your Sketchbook\Libraries folder in a folder named 'elapsedMillis'. (For more information, have a look at the Arduino Guide to Installing Libraries).

For users with familiar with Git, you can simply clone the git repository into 'elapsedMilllis' under your Sketchbook\libraries folder.


Feedback and bugs

As this is not an official Arduino library nor is it supported by the Arduino team, please direct all feedback, bugs and issues to the issue tracker for this library which is located on Github.


For more information

Although it is thought that this library is already well documented and is accompanied by two example sketches (at the time of writing), more information and examples are located on the wiki.


Acknowledgements

Original source code was written by Paul Stoffregen as an aid to users of his Teensy Arduino compatible USB development board. Visit the Teensy USB Development Board page for more information.

Initial examples and Arduino library package was developed by John Plocher.