Posts with «breathing led» label

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.

Hack a Day 14 Jun 00:00