Two Animations At The Same Time
New sample code showing how to run two different animations on different segments of one LED strip at the same time. A number of people have asked about this, and while there are many, many ways to do it, the technique shown in this sample code is pretty simple and keeps the code organized and clean.
The example here shows “confetti” running on 1/4th of the LED strip, and “rainbow” running on the other 3/4ths – with two blank pixels between them for spacing. You can use this same technique with lots of different animations, and you can divide and position the animations anywhere you like on the strip. In fact, you can even move them around… but that’s another topic for another day.
Key concepts:
Move each animation into it’s own ‘draw’ function that draws just one frame of the animation – and then returns without calling FastLED.show() or delay().
Have each ‘draw’ function take an initial LED position and length as arguments – and use them to make sure each draw function draws into the right place.
ONLY call FastLED.show() and delay() from inside the main loop() function!
If you have multiple LED strips, each draw function should actually take not just a starting LED number and length, but perhaps also which LED strip you want to animation on (e.g. a pointer to the leds[] array for that strip). And yes, the starting LED number and the LED array pointer can be combined into one argument, but this example code is all about simplicity, and now you’re getting complicated!
Anyway, this division of “drawing into the leds[] array” away from the loop that “calls show and/or delay” is the central idea here. Move your drawing code out into it’s own function, and have that function take arguments as to where and how much to draw, and the rest is simpler after that.
I’m just getting started with FastLED, so forgive the noob question, but I get an error on compile due to the EVERY_N_MILLISECOND( 10 )... line. Is that a built-in function FastLED should understand, or something I need to define elsewhere?
Ah… That makes sense. I double checked my local git clone last night but it was up to date. Now I’m realizing I didn’t check the copy of it in my library folder. In any case, if it’s a version thing it has to be on my end so I know where to look now. Thanks for the quick correction!
Ahhh. I finally understand. I re-pulled in my Arduino library dir copy itself… and also got that it was up to date (but v 3.0.1). Then it clicked – it’s a separate branch! That seems so obvious now, but I just kept associating “it’s in the new blah blah” with “new version,” not separate branch. git checkout FastLED3.1 was the ticket. Just posting this for the benefit of anyone else who might stumble on it.
Hi I am wondering if you can help with something. I am working on POV and ran into your code which helps a ton. I want to have two images or more continued down the line of the leds. In this code it displays a heart on the bottom half of a 32 pixel strip. On the top it displays a mushroom. Then it cycles to the mushroom on top and the heart should display but it is a mess, meaning the led counts are wrong. So essentially I want to have two sections of 16 leds displaying pov images.
No problem. Indeed, there are some things that I think are only really in those code-style auto generated docs vs the wiki. Others have commented similarly. Hope that gets you in the right direction!