Looping without breaking the code with the Millis function

Has I also use my blog as a reference for myself, I mainly put this there for further reference. I usually use this lines of code to loop without stopping other functions from being executed or to time certain events.

void myFunc()
{
	unsigned long interval = 60 * 1000; // 60 seconds.
	static unsigned long previousMillis;

	if( millis() - previousMillis > interval ) {
		previousMillis = millis();

		/*
			Timed code here.
		*/
	}
}

This function can be called in the main loop() function. Every time it be called, the function checks if the interval of time is reached. If so, the code within the condition is executed. The Millis() function counts the milliseconds since the Arduino is booted.

This entry was posted on Sunday, March 25th, 2012 at 18:45 and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Internet to Serial Proxy for the USB Arduino and others...