7

One of the FastLED examples that I've seen was using code that looks like this:

EVERY_N_MILLISECONDS( 300 ) { transition_step(); }

I've copied it and successfully used it in the same context - but I have no idea what it is or how it works.

A Google search finds no reference anywhere at all.

I'm still a beginner with C but this looks like a Define to me - all being in uppercase - is it defined somewhere in the FastLED library?

Lefty
  • 347
  • 2
  • 4
  • 12

3 Answers3

6

Yes, it is defined in the FastLED library. It is a preprocessor macro which is replaced by a block of code when compiled:

It is a very convoluted bit of preprocessing code that is difficult (even for me) to follow through with lots of concatenations and multiple layers of definition, so my advice is to just not worry about it and just use it.

If you want to try and decypher it all it's in the file lib8tion.h in the FastLED library.

Majenko
  • 105,851
  • 5
  • 82
  • 139
3

As @Majenko has said it is defined in the FastLED library http://fastled.io/. There is also EVERY_N_SECONDS.

It is not necessary to call any other FastLed function in the run loop (such as FastLED.delay()) in order to use these functions.

include <FastLED.h>

void loop() {
   ....
   EVERY_N_MILLISECONDS( <mseconds> ) { 
     <your timed code here>; 
   }

   EVERY_N_SECONDS( <seconds> ) { 
     <your timed code here>; 
   }
}
meesern
  • 131
  • 1
3

Its a macro that wraps the code you put inside of it to only be run based on the time interval you pass to it. The purpose of it is to allow for timer based code to be ran without having to block the main loop(). When the code is compiled it gets wrapped by other code that governs how the code inside it should run.

Lets say you want to run a method every 100ms. A simple approach would be the following:

// loop() runs the code within it over and over at a rate based on the 
// how fast the microprocessor runs - specifically how long it takes 
// for the processor to get through the code inside of it.
void loop(){

doMethod(); delay(100);

checkForButtonInput();

}

The issue here is that during the delay(), the delay() function, by its design, hogs all the processor time for 100ms, and so the checkForButtonInput() is not able to check for input, as the program is currently stuck looping inside of delay() until 100ms has passed.

EVERY_N_MILLISECONDS( <mseconds> ){} is the answer here, and you can think of it working to add code to you code like this (although this isn't exactly accurate):

void loop() {
   EVERY_N_MILLISECONDS( 100 ){
  //pseudocode
  //if ( it has been 100ms since last time we did doMethod() ) {
      doMethod();
      // reset time since we did doMethod() to 0
  //}
  //else {
  //  continue...
  //}
  }
  checkForButtonInput();
} 

so if it hasn't been 100ms since the last time we were in the EVERY_N_MILLISECONDS wrapped code then we just move on, and so the checkForButtonInput() is called as much as possible and inputs wont be missed.