FadeLed
A simple Arduino library to fade leds on hardware PWM
|
Makes fading LEDs on an Arduino easy. By Timo Engelgeer (Septillion)
***New in version v1.4.0:* Gamma correction.** As of version v1.4.0 the library fades with gamma correction. This is now the default behavior. This also sets the default range to 0 to 100 (instead of the old 0 to 255 for 8-bit). So if you upgrade from an older version change the range in your sketch or disable gamma correction (noGammaTable()
).
Fading is easy right? But it can quickly become quite some code to do so. FadeLed does all the heavy lifting for you. It can fade every LED on a hardware PWM pin (capable of analogWrite()
) with gamma correction. Just make a FadeLed object for it, set the fade time and just set the brightness to fade to. You can let it fade to and from each brightness you like!
FadeLed can fade in two modes, constant fade speed (default) and constant fade time. The mode can be selected for each LED.
This is the default mode. A LED will fade with a constant speed. The time you set is the time of a full fade from off to full brightness or vice versa. If the fade is not full scale, for example to/from half brightness, it will take less time. In the case of half brightness it will take half the time.
Each fade will now take the same amount of time. No matter if you fade the full scale or just by 10 steps, it will take the same time.
The human eye doesn't respond linearly to light. It's way more sensitive for small changes in dim light than it is to small changes in bright light. To compensate for that FadeLed uses gamma correction tables. This gives a very natural feel to the dimming and fading. Even if you're not interested in fading you can use FadeLed to drive LEDs with gamma correction.
If desired, the gamma correction can be disabled per LED. It's also possible to use a different gamma table than default (with a gamma of 2,3), even per LED!
FadeLed is available via Arduino IDE Library Manager.
Latest release: v1.5.0
libraries
folder inside your Sketchbook. Default is [user]\Arduino\libraries
.FadeLed
(remove version number).You can update to the latest version of the library in the Library Manager as well.
Alternatively you can download it from GitHub and simply unpack it over the current version (or remove the old version first).
Using FadeLed is simple. Just make a FadeLed object for each LED you want to fade like
It's also possible to make an array of multiple FadeLed-objects which can make it easy to loop over each.
In order to update the fading of all FadeLed objects you just have to call FadeLed::update()
frequently. So it's best to have a non-blocking loop and call it like
Calling this function will set the time a fade needs to take. By default it will set the time a full fade should take (constant fade speed). But by entering true
as second parameter you can change that to the time each fade should take (constant fade time). You can change the fade time anytime you like! The time is set in milliseconds
The most common function of the library. Simply sets the brightness to fade to.
There are also the shortcuts .on()
and .off()
to simply fade to full on or full off respectively.
Other useful methods of the library include .on()
, .off()
, .done()
, .get()
, .rising()
, .falling()
and FadeLed::setInterval()
. For documentation of all the methods, see the full documentation.
Full documentation of all the methods of this library can be found inside the library located in FadeLed\doc
. Just open FadeLed\doc\index.html
to see all methods of FadeLed.
You can also view the documentation via GitHub HTML Preview.
This documentation is powered by Doxygen and thus fully extracted from the source files. This README.md is also used as Main Page.
Check to see if FadeLed::update()
is called regularly. So you should not use blocking code! The loop()
should run freely. The biggest example of blocking code is the delay()
function. (See Blink without delay how to fix that.) But also other functions can block like Serial.readBytesUntil()
, Serial.parseInt()
or other functions that just wait until something happens.
Are you calling FadeLed::update()
frequently? Have you used a PWM (capable of analogWrite()
) pin?
If you do, are you using another library (or code) that uses a timer? For example Servo
. This will block the PWM of some pins. Check if you can do a plain analogWrite in the same code.
By default the library can only fade 6 LEDs. Why 6? That's the number of PWM pins on an Uno (/Pro Mini/Nano). If you use a board with more than 6 hardware capable PWM pins you can increase this number by changing src\FadeLed.h
. Change
Set the fade time of each color to the same time and to constant fade time. Now always set the brightness of all the three colors together (or at least all before you call FadeLed::update()
).
Simply change the macro FADE_LED_PWM_BITS in FadeLed.h to the number of bits your device has.
For example for a with 10-bit PWM:
Selection is only automated for ESP8266. Would like to automate this in the future for more devices.
Calling FadeLed.set() is ignored while the LED is still fading in constant fade time (not in constant fade speed). Wait until it's done (check FadeLed.done() ) or call FadeLed.stop() to stop at the current brightness after which you can set a new brightness to fade to.