FadeLed-library

Makes fading leds on an Arduino easy.

By Timo Engelgeer (Septillion)

GitHub: septillion-git/FadeLed

What does it do?

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()). 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.

Constant fade speed

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 halve brightness, it will take less time. In the case of halve brightness it will take halve the time.

Constant fade time

Each fade will now take the same amount of time. No matter if you fade the full scale or just just by 10 steps, it will take the same time.

Download and install

  1. Download the latest release from GitHub.
  2. Extract it to the libraries folder inside your Sketchbook. Default is [user]\Arduino\libraries.
  3. Rename the folder to FadeLed (remove version number).
  4. Restart the Arduino IDE if you had it open.
  5. Done!

Usage

FadeLed-object

Using FadeLed is simple. Just make a FadeLed object for each led you want to fade like

FadeLed statusLed(5); //Fading status led on pin 5
FadeLed anotherLed(6); //Other fading led on pin 6

It’s also possible to make an array of multiple FadeLed-objects which can make it easy to loop over each.

FadeLed myLeds[] = {5, 6}; //creates two FadeLed objects: leds[0] and leds[2]

void FadeLed::update()

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

void loop(){
  FadeLed::update(); //updates all FadeLed objects

  /*
  Do all the other things you want to do
  */

}

void .setTime(unsigned long time)

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

statusLed.setTime(5000); //makes the statusLed fade with constant fade speed. Full fade will take 5 seconds
anotherLed.setTime(1000, true); //makes this led fade with constant fade time. Each fade now takes 1 second

void .set(byte value)

The most common function of the library. Simply sets the brightness to fade to.

statusLed.set(127); //fades the statusLed to halve brightness

There are also the shortcuts .on() and .off() to simply fade to full on or full off respectively.

More methods

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 / more info

For more information see the GitHub page: FadeLed on GitHub

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.

Share