TwinkleFOX: new Color Twinkles for 2015 Video: Code: A year ago,

TwinkleFOX: new Color Twinkles for 2015

A year ago, I offered up ‘twinkle lights’ for the holidays, and I’ve been thinking about them in the back of my mind ever since. I now humbly offer up “TwinkleFOX” : new, improved, and internally MUCH weirder!, color twinkle code.

As before:

  • use at many (or as few) color palettes as you like
  • several ‘holiday’ color palettes are included
  • palette cross-fades
    And now, new/improved:
  • Much easier control over twinkle speed and density
  • Smoother fading in and out; truer, smoother color fades
  • Optional “background color” (other than black)
  • Lower memory usage: zero(!) bytes per-pixel overhead
  • Weird new internal design, which led to the new name: TwinkleFOX

Why is it called TwinkleFOX? Because the entire state of every LED in the array can be generated as a Function Of X, where X is a clock. There’s no need for storing the ‘state’ of each light: everything is a function of time. Much longer comments are in the code itself, here But the basic idea is this: if you had only ONE pixel, and you wanted it to fade in and out, you could say something like:

pixelbrightness = sine( time )

And then given a particular point in time you could easily tell what brightness the pixel should be, right? (In a sense, this is how the ‘noise’ functions work, too.)

Well, so that’s more or less what’s going on here, too, except (1) each pixel has its own time clock, (2) each time clock moves at a slightly different speed, (3) each time clock has a slightly different zero-offset, and (4) none of this data has to be stored in precious RAM… it’s all “stored” in a random number generator. Yeah. Like I said: much weirder internals this time!

Feel free to just run the sketch and look at the pretty lights, or dig in and ponder the code. It took me months and months of background-thinking about this before I implemented it, so if it seems hopelessly abstract, you might be right about that.

Oh, and aesthetically, if you run it on an LED strip, instead of a pixel string, I recommend dropping the TWINKLE_SPEED and TWINKLE_DENSITY both down from “5” (the default) to “4” – but that’s a parameter you can play with yourself! See code for details.

Enjoy, and may your holidays be happy and filled with light!

UPDATE: of course, I’ve found minor typos and things in the code, which I’ve fixed in the posted code now. If it’s not doing quite what you expect, just try re-downloading the latest version.

Thanks for sharing! I can’t wait to try this out on my tree!

Thanks. I’ll be adding this to my window lights code to add some variability.

For a slightly better attack/decay on the light levels, add this function into the sketch, and replace the existing call to “triwave8” with a call to this function, “attackDecayWave8”. I held it back from the published code on github only because there are already enough complicated things going on in there at once!

// This function is like ‘triwave8’,
// (which produces a
// symmetrical up-and-down triangle
// sawtooth waveform), except that this
// function produces a triangle wave with
// a faster attack and a slower decay:
// /
// /
// /
// /

uint8_t attackDecayWave8( uint8_t i)
if( i < 86) {
return i * 3;
} else {
i -= 86;
return 255 - (i + (i/2));

UPDATED This is now already incorporated into the published code.

I see potential server twinkle here!

Just wondering about the “Ramekin.h”.

Oops – just a leftover. Fixed (removed).

@Michael_Sime : definitely some server possibilities here – I was absolutely thinking about that as a strong secondary use here.

Because the ‘brightness wave function’ is no longer related to the cycling itself, you could easily make a wave function that was “always on, except for a few moments of off”, etc.

Fancy idea & code! Inspiring.

Awesome Mark. Love your creativeness.

Thanks @Mark_Kriegsman

If next year’s Christmas-leds-on-the-house project is a success, this is definitely one of the patterns I’ll be running.

I just put it up on a 300-pixel “tree” in our office and I have to say I kinda like it.

I made one other code tweak (other than the attack/decay, above), which is to have the colors lean slightly toward redder as they fade out, slightly simulating the cooling of an incandescent bulb filament. I’ll post that here too; I left it out of the original posted code for the same reason as the attack/decay thing: code already complicated enough!

So: add this function to the sketch:

// This function takes a pixel, and if its in the ‘fading down’
// part of the cycle, it adjusts the color a little bit like the
// way that incandescent bulbs fade toward ‘red’ as they dim.
void coolLikeIncandescent( CRGB& c, uint8_t phase)
if( phase < 128) return;

uint8_t cooling = (phase - 128) >> 4;
c.g = qsub8( c.g, cooling);
c.b = qsub8( c.b, cooling * 2);

Then find the existing line that says
return ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);

And replace that line with this:
CRGB c = ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);
coolLikeIncandescent( c, fastcycle8);
return c;

A totally optional tweak, but I think I like the way it looks overall: a little softer, a little warmer.

UPDATED This is now already incorporated into the published code.

My lady likes the tweak even more.

Thanks. I have a couple more tweaks which make it “better” but also “more complicated”. I’m now thinking about publishing the fully-tweaked version as more of a ready-to-run tweaked-out sketch, and less as a “useful example.”

And of course, it uses Dan’s new pixel iterators…

Any video of the tree?

I’ll try to get some tomorrow. It’s this

The german Arduino forum asked me already to express their appreciation and respect for your creativity. So: Those guys like your crazy code! Me too.

@Mark_Kriegsman , can you take a look at Twinkles.cpp in I tried to port you code to a class to fit in my window lights display more easily, but it’s not working. I don’t see why. I got it sort of working once, but it only lit a few. So I changed the density to 8, and nothing lit up. I suspect I just don’t understand it well enough though, so it’s pry broke in some silly way.

I actually want to do two things. The first is Snow, but it’s not showing up at all. The second would be a twinkling christmas tree effect, but all the lights would be a little better than dimly lit (hence using 8). I have 4 strands of Adafruit 2812’s for this driven by a Photon.

So first, I love “Twinkles::seeTheRainbow()”.

Second, I fixed a couple of minor bugs the afternoon after I posted it… the code you have looks like maybe it still has the old version. Sorry about that!

I’m going to post a revised (fancy) version in the next day or so. I like your class structure, and I’ll try to keep the original source as easy-to-convert-to-a-class as possible.