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.
“Today’s hotness is breathing LEDs. ”
Got a wired Logitech mouse that does that. Thankfully it can be turned off.
“One thought on “Simplifying Basic LED Effects”
Yup, that oughta just about cover it…
Please. No more blue LEDs. Please.
If it makes you feel better, I did the fork with a green LED.
Pink LED is in! Lighting up breast cancer awareness one bulb at a time!
I give it 2 hours before you’re contacted by Susan G Komen’s lawyers.
I like the ice blue ones. HP desktops had ice blue power buttons ca 2008 and I loved them
Tried doing this “breathing” with cold cathode tubes ~15 years ago when they the cool in thing. But couldn’t get my head around doing it with just a few discrete transistors.
I dunno what kind of control signal a cold cathode needs, but you could always use a 555 charging and discharging a big fat capacitor over time.
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.)
You could probably do a breathing LED effect in 10 lines, using a PWM pin, using virtually no processor overhead.
“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.
“Today’s hotness is breathing LEDs”
Eurgh. I wish people would stop calling it ‘breathing’, what’s wrong with just calling it “fading in and out”?
Probably because Apple called it that on their macbooks, and it stuck.
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.
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?
Or just learn how to read them instead of interpreting as “everything under the sun, and the sun is a freebie”.
…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
Breathing is as easy as … breathing. Realistic candle effect is a bit more complicated and much more interesting.
Downright musical in a lot of cases:
http://www.instructables.com/id/Listen-to-a-led-tea-light/
Im not upto par with arduino, but why not stick the update subrouting into a timer interrupt?
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.
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.
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)