Simplifying Basic LED Effects

There was a time when having a blinking blue LED on a project was all you needed to be one of the cool kids. But now you need something more complex. LEDs should not just snap on, they should fade in and out. And blinking? Today’s hotness is breathing LEDs. If that’s the kind of project you want, you should check out [jandelgado’s] jled library.

At first glance, an Arduino library for LED control might seem superfluous, but if you are interested in nice effects, the coding for them can be a bit onerous. If you don’t mind stopping everything while you fade an LED on (or off) then sure, you just write a loop and it is a few lines of code. But if you want to have it happen while other things continue to execute, it is a little different. The library makes it very simple and it is also nicely documented.

Obviously, to create a special effect LED, you need to create a JLed object. Then you can use modifier methods on that object to get certain effects. The only overhead is that you need to call the update method on the LED periodically. Here is one of the examples from the project:

#include <jled.h>

// connect LED to pin 13 (PWM capable). LED will breathe with period of
// 2000ms and a delay of 1000ms after each period.
JLed led = JLed(13).Breathe(2000).DelayAfter(1000).Forever();

void setup() { }

void loop() {
   led.Update();
}

Pretty easy and readable. Just remember that some Arduinos can’t do PWM on pin 13, so you might have to adjust. Our only complaint is that you have to update each LED. It would be nice if the JLed constructor kept a linked list of all LED objects so you could have a class method that updates all of them with one call. In the examples, the author keeps all the LEDs in an array and steps through that. However, that would be easy to fork and add. Oh wait, we did it for you. The library does do a lot of work, including taking advantage of higher PWM resolution available on the ESP8266, for example.

The library can turn an LED on or off (including delays), blink or breathe an LED forever or for a certain number of times, or fade an LED on or off. In addition to the presets, you can provide your own brightness function if you want to do some kind of custom pattern. You can modify most actions by specifying a delay before or after, a repeat count (which can be forever) and you can also tell the library that your LED is active low, so you don’t have to mentally remember to flip everything around in your code.

Rocket science? No. But we like it. Blocking for an LED effect is bad and this makes fancy asynchronous LEDs simple. Why not use it? Recent IDEs can install it from the manage library dialog, so it takes just a second.

Really, libraries are a key to building systems simple. Why not stand on the backs of others? Some of our favorites handle SPI and proportional integral derivative (PID) control.

24 thoughts on “Simplifying Basic LED Effects

  1. People wonder why modern software is so slow and bloated, and then want their LEDs to “breath” in the next sentence…
    (LEDs with built-in “flicker” and “color change” chips are available – is there none with the “breath” effect?)
    (Lots of Arduino “programmers” should find this helpful, though. “We” should go through and write non-blocking libraries for a bunch of the stuff that tends to trip up beginners.)

  2. “Breathing” huh. We always called them “snoring” because of the distracting effect if you were trying to sleep in the same room with that light (may it please not be blue) that keeps changing.

      1. yeah but apple really had “breathing” LEDs. they didn’t simply fade in and out but the timing was modeled on a person breathing. So it wasn’t really linear fading.

        1. According to Apple’s 1999 patent, it’s just sinusoidal. _”… generates a sleep-mode indicator blinking pattern based on a sinusoidal function using PWM …”._ The inspiration and timing may well have been a sleeping person’s breathing rhythm, but nothing more fancy than that. Also, here we go with patents again, does that mean we have to stick with ordinary blinking LEDs?

    1. …need to use JARGON to keep the great unwashed at bay. They might understand, otherwise. Can’t have that, now can we?…

      “The word ‘user’ is the word used by the computer professional when they mean ‘idiot’.‭” — ‬Dave Barry

  3. I typically program PICs in assembly. To do the same breathing effect I just rotate (single instruction RLF) the 8bit PWM register periodically. Once I get to zero I just toggle the carry bit.
    PWM Register:
    00000000
    00000001
    00000011
    00000111

    11111111
    11111110
    11111100

    The actual PWM on/off output is accomplished through the onboard timer peripheral. So it’s roughly four instruction cycles every 0.1s.

  4. As a PSA, I added the UpdateAll to my Git fork of the project but failed to realize that the default copy constructor was not making an appropriate copy of the object (duh) which broke how the examples are written. I have updated to provide a default copy constructor so now that works like it should even if you write your code like the examples are written. I did a pull request upstream so hopefully they’ll fold it in at some point.

    But if you downloaded my fork before this message timestamp, you should go refresh it.

  5. Interesting post and comments, thanks :-)

    It occurs you could make the led or any number under different conditions appear as if it has a faulty electrical connection by modulating the breathing mode (with a general lumen decline then off for some time etc) with a pseudo random sequence. For the amateur (and electronics) observer this triggers the ‘uh oh’ thought somethings wrong and draws attention to the unit – for whatever reason ;-)

    For the aware seasoned developer one could also embed a data sequence as a low level (simple) data encryption, so for most they would just see some sort of failure – whilst those prepared get their mobile phone out, put camera in correct mode with suitable software, acquire the data stream, process out the pseudo random stuff & coding protocol and “get the message”. Of course there are several permutations one could offer – ain’t life fun when you can combine electronics with psychology & data security :-o)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.