FadeLed
A simple Arduino library to fade leds on hardware PWM
FadeLed Class Reference

Main class of the FadeLed-library. More...

#include <FadeLed.h>

Public Member Functions

 FadeLed (byte pin)
 Constructor of a FadeLed object. More...
 
void begin (byte val)
 Set a direct begin value to start at without fade. More...
 
void set (byte val)
 Set the brightness to fade to. More...
 
byte get ()
 Returns the last set brightness. More...
 
bool done ()
 Returns if the led is done fading. More...
 
void on ()
 Fade to max brightness. More...
 
void off ()
 Fade to off. More...
 
void beginOn ()
 Sets the start brightness directly to full. More...
 
void setTime (unsigned long time, bool constTime=false)
 Set the time a (full) fade will take. More...
 
bool rising ()
 Returns if the led is still fading up. More...
 
bool falling ()
 Returns if the led is still fading down. More...
 

Static Public Member Functions

static void update ()
 Updates all FadeLed objects. More...
 
static void setInterval (unsigned int interval)
 Sets the interval at which to update the fading. More...
 

Protected Member Functions

void updateThis ()
 Updates fading of this object only. More...
 

Protected Attributes

byte _pin
 PWM pin to control.
 
byte _setVal
 The brightness to which last set to fade to.
 
byte _startVal
 The brightness at which the new fade needs to start.
 
byte _curVal
 Current brightness.
 
bool _constTime
 Constant time fade or just constant speed fade.
 
unsigned long _countMax
 The number of _interval's a fade should take.
 
unsigned long _count
 The number of _interval's passed.
 

Static Protected Attributes

static FadeLed_ledList [FADE_LED_MAX_LED]
 array of pointers to all FadeLed objects
 
static byte _ledCount = 0
 Next number of FadeLed object.
 
static unsigned int _interval = 50
 Interval (in ms) between updates.
 
static unsigned int _millisLast = 0
 Last time all FadeLed objects where updated.
 

Detailed Description

Main class of the FadeLed-library.

The FadeLed class includes all the functions to easily fade a led including a static function to update all constructed FadeLed objects.

See also
set(), update()

Constructor & Destructor Documentation

FadeLed::FadeLed ( byte  pin)

Constructor of a FadeLed object.

Makes a object to fade a specific pin. You can make a object for each PWM pin you want to fade. Updating all objects is done by simply calling update()

When created the default brightness is 0. You can start at a different brightness by calling begin().

Note
Make all objects global (or static if you really must) because a destruct will lead to errors!
Warning
Don't make two objects for the same pin, they will conflict!
Parameters
[in]pinThe PWM pin to fade with this object
See also
update(), set(), on(), off()

Member Function Documentation

void FadeLed::begin ( byte  val)

Set a direct begin value to start at without fade.

If you want to directly start the led at a certain brightness you can set it with this. This will directly set the brightness without fading.

Note
You can also use this to set a brightness without fading
See also
beginOn()
Parameters
[in]valThe brightness to start at.
void FadeLed::beginOn ( )

Sets the start brightness directly to full.

Short for calling begin(255)

See also
begin()
bool FadeLed::done ( )

Returns if the led is done fading.

true if the led is done fading and reached the set() value.

false if it's still fading

Returns
If the led is fading or not
bool FadeLed::falling ( )

Returns if the led is still fading down.

true if fading down.

false otherwise

Returns
if the led is fading down
byte FadeLed::get ( )

Returns the last set brightness.

Returns the last set brightness via set() (or begin())

See also
set(), begin()
Returns
The last set brightness
void FadeLed::off ( )

Fade to off.

Sets the led to fade to off. Same as calling set(0)

See also
set()
void FadeLed::on ( )

Fade to max brightness.

Sets the led to fade to max brightness. Same as calling set(255)

See also
set()
bool FadeLed::rising ( )

Returns if the led is still fading up.

true if fading up.

false otherwise

Returns
if the led is fading up
void FadeLed::set ( byte  val)

Set the brightness to fade to.

Set the brightness to which the led should fade to. If the led is still fading that fade is ended directly and the fade to the new value is started directly. To start fading after the current fade check if it's done via done()

In constant fade time the fade time is also reset. Even when the led was not done fading yet. So in constant fade time it will again take 'time' to reach the desired brightness.

Note
To make all the fading work you need to call FadeLed::update() often in the loop()!
See also
done(), setTime()
Parameters
[in]valThe brightness to fade to.
void FadeLed::setInterval ( unsigned int  interval)
static

Sets the interval at which to update the fading.

Only every interval when calling update() it's checked to see if the brightness of the leds needs to change (fade) to leave time for other stuff.

default: 50ms

Warning
Call this before setting a fading time (via setTime()). Changing the interval will change the fading time or each FadeLed object.
See also
setTime()
Parameters
[in]intervalInterval in ms
void FadeLed::setTime ( unsigned long  time,
bool  constTime = false 
)

Set the time a (full) fade will take.

This will set how much time a fade will take.

The real fade time will be a whole multiple of the set interval (rounded down). For example, if interval is set to 50ms and you specify a fade time of 1025ms the fading time is actually set to 1000ms.

Constant fade speed (default)
In constant fade speed this is the time a fade from off to full brightness (or vice versa) will take. A fade of less will go with the same speed but will take less time

Constant fade time
In constant fade time this is the time each fade will take. No matter how much change in brightness. Each call to set() will reset the fading time and it will fade from it's current brightness to the new brightness in this time. Useful for example if you want to fade a RGB led from one color to another in a set time. No matter the level of the individual RGB colors.

Note
If you want to change the update interval (setInterval()) do that before calling setTime(). The fade time is calculated using the interval.
See also
setInterval()
Parameters
[in]timeThe time (ms) a fade will take
[in]constTime[optional] true to use constant fade time. Default constant fading speed
void FadeLed::update ( )
static

Updates all FadeLed objects.

This is the core function of FadeLed. Calling this function will check each object of FadeLed to see if the brightness needs changing (fade).

It's a static function, you only need to call is once for all objects of FadeLed. You can call it using the class name like:

{C++}
loop(){
}
Note
Call this function often in order not to skip steps. Make the code non-blocking aka don't use delay() anywhere! See Blink Without Delay()
void FadeLed::updateThis ( )
protected

Updates fading of this object only.

Can't be called directly (it's protected). Instead call update() to update all FadeLed objects.

See also
update()

The documentation for this class was generated from the following files: