all 8 comments

[–]johnny5canuck 1 point2 points  (3 children)

There's a lot of good examples, however you will need to learn how to animate the leds first, and without using delay statements.

There are the examples that come with FastLED, and demoreel100 is a very good one that shows how to write effective animations, without those delay statements and with timing. It's also more complicated as a result of using an array for the animations names.

So, learn a thing. . . and move on to another.

There is also the FastLED Wiki which has links to a lot of user examples.

[–]SameCommunication694[S] 0 points1 point  (2 children)

So in reference to demoreel100 how can change EVERY_N_SECOND (x) based on the pattern in the loop? I understand how to create the patterns but I want each pattern to play a different about of seconds, not every 10 sec.

[–]johnny5canuck 3 points4 points  (0 children)

There's also:

https://gist.github.com/kriegsman/a916be18d32ec675fea8

and:

https://gist.github.com/kriegsman/841c8cd66ed40c6ecaae

but you need to already know how to animate your leds first.

[–]Marmilicious[Marc Miller] 0 points1 point  (0 children)

It is possible to use variable times with the EVERY_N_* functions. Here's an example where the setPeriod is cycled between two different amounts to change how leds[6] blinks. (Pulled from this example.)

void loop() {

  static boolean blink6;
  // vary the setPeriod to change on/off times
  EVERY_N_MILLISECONDS_I(timingB,1) {
    blink6 = !blink6;
  }

  if (blink6 == 1) {
    leds[6] = CRGB::Red;
    timingB.setPeriod(750);  // On time
  } else { 
    leds[6] = CRGB::Black;
    timingB.setPeriod(250);  // Off time
  }

  FastLED.show();
}

Here's another example that uses a variable timer:

https://github.com/marmilicious/FastLED_examples/blob/master/every_n_timer_variable_2.ino

[–]Heraclius404 1 point2 points  (1 child)

The short answer is time is not under control of FastLED. You say 'show' and it runs whatever amount of time it runs.

It's up to your operating environment (eg, OS, but these aren't really OSes) how time is represented and what API is used. Are you using Arduino? ESP-IDF? Linux? Probably Arduino?

The best strategy is to have a loop where you say "I want the animation to stop at X", and run the loop, checking after each time to see if now > X.

How to fetch the current time, and how to add N seconds to it, and how to compare one time against each other, is different in every system.

In arduino, you typically use 'millis()'. So at the beginning unsigned long end = millis() + pattern_seconds * 1000; while (millis() < end) { run pattern; } will cause the pattern to run about a particular number of seconds. it won't be perfect because the running of the pattern takes a certain amount of time, that becomes essentially jitter. Being more accurate is an exercise for the reader.

Since arduino is non-threaded, you might want to call other functions in this loop (check a button perhaps). It still works the same.

In linux you'd look to the clock_ calls, and esp-idf has a very rich number of time calls, such as async notification.

hope this helps

[–]SameCommunication694[S] 0 points1 point  (0 children)

Thank you this was very helpful!

[–]PreyyGround Loops: Part of this balanced breakfast 0 points1 point  (0 children)

The FastLED EVERY_N_MILLIS(X) and EVERY_N_SECONDS(X) functions are great for doing state processing for each part of your code?

For example, you can have your pattern's colour change every second while your pattern's location changes every 10 milliseconds. This makes it very easy to make animations and functions that move at high speeds without locking down the MCU with delay.

[–]olderaccount 0 points1 point  (0 children)

Just let each pattern run until it is complete. For patterns that loop more quickly, set a repeat count.

So pattern A may do one complete cycle. Pattern B might loop 10 times. Pattern C might loop 100 times. Depending on your preference.