RGB LED Animation

LED AnimationI was over enjoying a NYE drink at Aidan’s place at the weekend and we were chatting about gadgets, as you do and he’s made a little 3d printed enclosure that looks like a round home fire alarm box.. in the middle is a movement sensor (IR) and some slots all around. He was demonstrating it and said that he’d put a string of RGB LEDs on the board for status use and I said how nice it would be to give it some Echo-style animation.

That went off on a tangent and we ended up with some fancy animation using my ESP8266 code (Home control 2016). The software to do this has been around for a while – but not really used other than in my flashing Christmas candle referred to in an earlier blog.. in that code you can refer to a string of RGB lights up to 300 of them and do things like -  make the first 10 LEDs RED and pause for a second, then make the second 20 LEDs GREEN and wait for 5 seconds, clear the lot wait 1 second.  That will then continue to do that forever in a loop.

What has always been missing however is proper loops an loops within loops. Well, I fixed that tonight.

Using the normal topic/payload structure I’ve been testing on serial.

Here’s an example I have running in front of me in which I have a strip of 12 serial RGB LEDs.  4 come on in blue, then they go off, then the last 8 alternate between 4 greens and 4 reds, then 4 reds and 4 greens, several times before clearing and starting all over again.  As you can see there’s a loop in the middle of that.

{rgbstop}
{rgbstart:12,12}
{rgbadd:1,0,4,0,0,255,1000}
{rgbadd:1,0,4,0,0,0,10}
{rgbadd:2,8}
{rgbadd:1,4,4,0,255,0,10}
{rgbadd:1,8,4,255,0,0,90}
{rgbadd:1,4,4,255,0,0,10}
{rgbadd:1,8,4,0,255,0,90}
{rgbadd:3}
{rgbadd:1,0,12,0,0,0,1000}

This is now INCOMPATIBLE with earlier code so if you have something already running you’ll need to add a new first parameter (1) to keep compatibility.

The first command stops anything already running. The second sets up 12 LEDs on GPIO12 (count is the second parameter).

An example of a simple light up is the third instruction – code 1, from LED 0, light 4 LEDs in BLUE (0,0,255) for 1000ms.

Then go onto clear those LEDS and wait 10ms.

The new stuff comes in line 5 – command (2) – the loop. That says – here’s a loop (2) which will run 8 times.  More light changes after that then command (3) – end of loop.  Finally I clear all 12 lights and wait a second until starting all over again. That’s it – you can do loops within loops of course.

There is also a command 4 which means END – you’d only use that if you wanted to perform a series of loops and then stop permanently. That has no parameters.

Assuming you’ve used the latter and have a sequence which does whatever and then stops – it might be nice to store this in FLASH and recall it when needed. There are four non-volatile buffers available to store this information.

{rgbstore:X} where X is 0-3

You can play back any of these as below, again assuming in this case GPIO12 and 12 LEDs. Set X to be 0-3

{rgbrecall:X}
{rgbstart:12,12}

The above will play back a stored sequence immediately.

The code is in the usual place and available via the OTA – don’t forget the minor incompatibility. So this applies to version 1.7.5 of the software and onwards.

Facebooktwittergoogle_pluspinterestlinkedin

One thought on “RGB LED Animation

Leave a Reply

Your email address will not be published. Required fields are marked *