Blocking vs Non-Blocking?

Since the Spark requires the user loop() to give up control every now and then, I know there is a big requirement on making code non-blocking. What is the best way to go about doing this? Do you set a target millis() and wait for the actual millis() to reach that target before you execute a block of code? Are there any simple libraries or classes that might help handle this? I can’t help but to think a JavaScript-style setTimeout(function() { ... }, 1000) would be pretty awesome in this situation. Is a callback-style function-as-argument possible in C/C++?

I used to use Interrupts and Timers for the code that is not required to be continuously running in the loop()

That’s how stuff are done on the :spark: as well. Like the mode button to debounce and check the duration pressed without affecting the loop().

Never worked with java before but setTimeout sounds like a good to have :smiley:

Can’t wait to hear from the exprienced guys!

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

Nice! I think I’ll use this instead of delay(50) for my LED fading in my current project. Thanks!

elapsedMillis is implemented and working beautifully! Thanks @peekay123!

1 Like

I’ve been using this library: http://playground.arduino.cc/Code/Timer https://github.com/JChristensen/Timer

I just had to make a small change to the .cpp files to remove this:

// For Arduino 1.0 and earlier
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

And replace it with

#include "application.h"
2 Likes

jonathan, great find! I will fork this and make the Spark changes so it is available on my github. When the Spark Team implements libraries, this is definitely a good one. :smile:

UPDATE: The modified library and examples are posted on my github

1 Like

This is a very good lib thanks.
Can you publish it on the spark community libs ?
Regards,
Julien

@jburdy, completely forgot about this library! I’ll get on it :smile: