New NeoPixel Painter Library

Hi Everyone

I finally finished my library called NeoPixel Painter that makes it much easier to do simple and colorful animations on WS281x LED strips (aka NeoPixels).

Library description:
The basic principle is to use a brush that can be moved to any pixel. The brush holds a color using the HSV color space rather than RGB. This allows for making colorful animations with color fading as well as brightness fading. To make the brush move on its own, give it a speed and it will move a little on each function call. It only paints each pixel once, even if it stays at the same location for a longer time.

The brush paints on a canvas, which holds one HSV color for each pixel as well as a value to fade to, the fade speed and some fading properties (flags). You should only use one fading type (hue, saturation or value) at a time. Although multiple fadings are allowed, each pixel only holds one 'fade-to' value to not use excessive amounts of RAM. Also, all fading is implemented without the use of floats. This makes it fast and uses a lot less of the precious RAM. The downside is, that color fadings are slightly less accurate but almost indistinguishable to the naked eye because I implemented the fadings using 16bit variables and bit-shifts (aka fixed point arithmetic), making it almost as accurate as when using floats.

It is available since today in the library manager. I did some testing on an Arduino as well as an ESP8266 (NodeMCU actually) and it works just fine on both microcontrollers.
Please let me know if you have any suggestions, questions, remarks either here in the forum or on Github

Oh, and just for fun, I shot a quick video of what the example code animations look like:
Video

I hope the code is useful and if anyone makes some new animations you would like to add as example code, please post it as an issue on Github and I may integrate it into the code.

Enjoy :slight_smile:

Wow! I can't wait to test this. Thanks for the write up!

You are very welcome. Let me know if you encounter any issues with the Library.

Very cool! I especially liked Bouncing Balls and Sparkler.

I notice you are using the Adafruit_NeoPixel library. Have you tried out the FastLED library at all? If so, why do you prefer Adafruit_NeoPixel? I've been using the former with ESP8266 but have been meaning to give Adafruit_NeoPixel a try to see which I like better.

I actually have not tried FastLED yet but I definitely need to test it some day.
For the Painter library it would be easy to switch to this library but I usually use the Adafruit library, just because I am used to it and I know it works well on different Platforms.

Great work, @DedeHai! I forwarded it to the FastLED community.

just because I am used to it and I know it works well on different Platforms.

So does FastLED. At the moment it is state of the art - faster than anything else, supporting A LOT of different led chipsets and supporting many different platforms. Really worth to check it out.

Cheers,

Helmuth

In addition, FastLED supports the use of Palettes, which can give your animations a much wider range of colours using the same display routines. Imagine using an 8 bit value to perform a palette lookup rather than assigning a hue value. That palette could be a rainbow, or it could be cloud, fire or forest colours or even your own creation, and you can transition to another smoothly it on the fly.

Then there's perlin noise, fast math, blending and so much more. . .

Here's a cool example I made with minimal code:

Anyways, would love to see your painter ported over to FastLED. It can then support many other chipsets.

Great Job, I tested it with "Arduino Nano", Working perfect.

DedeHai:
You are very welcome. Let me know if you encounter any issues with the Library.

Ok, I'm a newbie! I have the demo working on my Arduino Nano, but how do I create my own? Is there an application install I need to get?

SwinBreaux:
Ok, I'm a newbie!

this is a perfect starting point, I made the library to make beautiful animations easier for beginners

SwinBreaux:
I have the demo working on my Arduino Nano

great!

SwinBreaux:
but how do I create my own? Is there an application install I need to get?

It is actually not THAT easy. You need to write your own code to make new animations. First, you must understand how to tell the library what you want to do.
A good starting point is to read about HSV colorspace, if you do not know what that is. Read about it on Wikipedia.
Now try to understand how the code in the ledstrip_animations.ino creates the animations. You can start by picking one, the sparkling animation for example, isolate it by deleting the other animations and then start modifying the code and see how the changes affect the animation.

To be more specific:
delete everything from the loop() and insert the following modified sparkler code. The small modification makes the sparkles less whiteish and more colorful by using a high color saturation with just a little randomness added to it. Let me know if you want me to explain how this code creates a sparkling animation in more detail.

	initialized = false; //reset the variable


	//SPARKLER: a brush seeding sparkles
	for(loopcounter = 0; loopcounter<duration; loopcounter++) 
	{

		HSV brushcolor;

		if (initialized == false)
		{
			initialized = true;
			pixelbrush.setSpeed(600);
			pixelbrush.setFadeout(true); //sparkles fade in
			pixelbrush.setFadein(true);  //and fade out immediately after reaching the set brightness
		}

		//set a new brush color in each loop
		brushcolor.h = random(255); //random color
		brushcolor.s = 220+random(35); //CHANGED THIS LINE ONLY FROM ORIGINAL CODE
		brushcolor.v = random(200); //random (peak) brighness

		pixelbrush.setColor(brushcolor);
		pixelbrush.setFadeSpeed(random(100) + 150); //set a new fadespeed with some randomness

		neopixels.clear();

		pixelbrush.paint(); //paint the brush to the canvas (and update the brush, i.e. move it a little)
		pixelcanvas.transfer(); //transfer (add) the canvas to the neopixels

		neopixels.show();
	}

DedeHai,

I tried you library and code and it worked very nicely! Thanks for sharing!

DedeHai:
It is actually not THAT easy.

Wow! My mind is blown!!

DedeHai:
To be more specific:

I will start here and play around with it.

My main item is to create color pattens for a lighting system I am installing my our kitchen. My wife likes to decorate the table and kitchen area for each season and holiday. I need different lighting patterns to match. (Colors for Fall, Halloween, Christmas, New Years/4th of July/Flag Days, Easter, etc...)

Cleared out loop () and it only shows a strip of white lights! (I'm testing on a strip of 10 pixels.)

Any reason why "neopixels.clear ();" doesn't work?

SwinBreaux:
Cleared out loop () and it only shows a strip of white lights! (I'm testing on a strip of 10 pixels.)

Any reason why "neopixels.clear ();" doesn't work?

Not in particular. As you can see I also use this command in the example. What it does is just clearing the buffer in RAM but not actually clearing the LEDs themselves. In order to do that you also need to transfer the buffer data to the LEDs by calling neopixels.show().
The painter library creates yet another buffer in RAM that it paints colors on to (called the 'canvas') which is in HSV colors as opposed to RGB as mentioned above. After painting to the canvas (by calling pixelbrush.paint()) you first need to transfer the colors to the neopixel buffer by calling pixelcanvas.transfer(). It translates the HSV colors from the canvas to RGB colors on the neopixel buffer. To now transfer the colors to the LEDs you call neopixels.show().
You can see that happening in the last four command lines of the example code above.