Blocking vs Non-Blocking?

wgbartley, I posted a library called elapsedMillis that essential allows you to create background counters for millis() and micros() that you test in your loop() code. Here is an example:

#include <elapsedMillis.h>

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

// Pin D7 has an LED connected
int led = D7;

// 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...
	}
}

You can have as many of these variables as you want. I typically use them in my loop to create my data sampling (50ms), data processing (500ms) and data display (1000ms) intervals for example. :slight_smile:

1 Like