Warm, soft twinkles First, here's a new sample code showing some nice warm,

Warm, soft twinkles
First, here’s a new sample code showing some nice warm, soft twinkles-- perhaps suited for simple holiday decorations, when a full color RGB onslaught might be too much.

After all, if our expensive, fancy lights can’t simulate the cheap, dumb ones, what good are they?

Second- the actual implementation for this animation is only about twelve lines of code, BUT there are a couple of seriously subtle and non-obvious things going on, so I’ve commented it to about a hundred lines total.

Subtle thing number one: this code “hides” a single bit of status information for each pixel (is this pixel currently brightening or dimming?) in the low bit of the Red color component. Read that again: the lowest bit of the Red component for each pixel is actually used as a meaningful status indicator of something other than color.

Subtle thing number two: saturating pixel math is used to automatically handle switching from brightening to dimming. The code has more comments.

So: enjoy the holiday lights, and have fun playing around with the code and the ideas. And if it seems fiercely confusing how it works, that’s OK. It’s not you. The code is sneaky. Normally I wouldn’t post such oddball code as an example, but every now and then it’s good to stretch.

1ea4e9e1d2139187acb3c81e26a4f6c7.gif

OK, video all loaded now.

Just for ease of discussion, here’s the heart of it all:

const CRGB lightcolor(8,7,1);

void softtwinkles() {
for( int i = 0; i < NUM_LEDS; i++) {
if( !leds[i]) continue; // skip black pixels
if( leds[i].r & 1) { // is red odd?
leds[i] -= lightcolor; // darken if red is odd
} else {
leds[i] += lightcolor; // brighten if red is even
}
}

if( random8() < DENSITY) {
int j = random16(NUM_LEDS);
if( !leds[j] ) leds[j] = lightcolor;
}
}

That’s it, code-wise. Whole sketch compiles to 3,852 bytes.

Brilliant! (Both the pattern and the memory-friendly implementation). :slight_smile:

Neat! I love it when a well experienced programmer shares some tricks. Thanks for the inspiration!

Nice, I will add it to my list of planned 2014 Xmas tree animations !!

Very Nice, I bookmarked it for future reference. I may look into getting it to twinkle different colors.

Twinkling different colors is an interesting level-up challenge from here: since Red will not always be the highest-valued component (it may even be zero), you can no longer “hide” the direction bit just in the Red component. I’ll leave it as an exercise to the reader to figure out what to do then… with the caveat that it’s a bit (or two or three?) more complicated.

If you don’t mind the pixels coming on full brightness and then fading out, here’s the two-liner “confetti” code that will give you lots of colors:

void confetti() {
fadeToBlackBy( leds, NUM_LEDS, 20);
leds[ random16(NUM_LEDS) ] += CHSV( random8(), 255, 255);
}

That’s pretty slick. Thanks for the details.

“simple holiday decorations, when a full color RGB onslaught might be too much” - NEVER!!! You’ll have to drag my full RGB onslaught from my cold dead hands!!! :wink:

I had an idea of putting lights around my front window frame, and just having something very simple and traditional looking like this for 99% of the time - and every so often have it do something totally mental. (ideal world - with a motion sensor that detects only a single person walking past) This is the perfect 99%er

@Mark_Ortiz : I LOVE this idea! :smiley:

Your git repo is definately a gold mine :wink: I also downloaded your sunshade matrix code that might help me to design my xmas tree topper star.

@Mark_Ortiz I just did this with my four front windows (minus the motion sensing). I will be posting my results to the show off area later. However, I want to start with an array of colors already populated, then randomly dim and relight individual pixels. Ideallly, it would relight on a different color. @Mark_Kriegsman , I would love help here. Your example gets me closer, but I still can’t quite figure out how to make it work.

I got it going with the colorPalette. http://paste.ofcode.org/4t6xQA7DDwbaHaGFtd934j
For some reason, some colors from the “TotallyRandomPalette” fades down much slower than others, not totally sure why, but i like the effect of the LEDs fading down differently from the other. At last i went away from the odd or even numbers, because i couldn’t get rid of all the flashing LEDs and i liked the effect.

Nice implementation. I was putting together a similar example, but (of course!) slightly different, but this one is nice!

Im exited to see how you do it. I got a little stock when trying to do it with the odd numbers, but got fascinated by the simple effect and worked arround it.

Right! Your approach is simpler (thank goodness!) but uses a bit more memory. As I’m sure you saw, that codes uses about seven bytes of RAM per pixel: 3 for the ‘led’, 3 for the lightcolor, and one for the Boolean direction flag (which could be just a single bit, but the compiler won’t do that by itself).

The one I think I’ll post used four bytes (32 bits) per pixel: three bytes (24 bits) for the led, and one for the direction flag. It could be relatively easily compacted to 25 bits: 24 for the led and one for the flag.

I do have one version that does it in just 24 bits-- using the “hiding in the low bit” trick, but it’s way more complicated: academically/curiously interesting, but a mess of an implementation.

I’ll see if I can post the 4-byte version tonight.

I posted both the any-color palette version here https://plus.google.com/112916219338292742137/posts/8yh7fi9Sb5i
Two implementations are provided: one that takes a total of 32 (24+8) bits per pixel, and one that takes 25 (24+1) bits.

@Mark_Kriegsman Thanks for the confetti code, I used it to make a 29 LED setup. It is 29 because the circuit board for the strip had the data pin break off :frowning: I need to find a way to make sure that connection point does not flex…

Thanks! nice code :slight_smile: