A LED dimmer library for Arduino
LightDimmer is designed to drive lights of railroad signals. One or many LED can be driven simultaneously. For each LED the time needed to switch on the light and the time needed to switch it off can be specified. The light can also blink with a specified period.
Using the library
As usual, you have to include it at the beginning of your sketch:
Then you have to instantiate as many object as lights you want to drive. The fist argument is the pin to which the LED is connected and the second one can be HIGH or LOW. It is the value to apply to the pin to switch the LED on. Of course for LightDimmer objects, fading is only possible on PWM pins.
With version 1.1, a LightDimmerSoft soft PWM class has been added. The API described below stays the same. Instead of having 256 levels, there are only 32 which is enough for this kind of application. Performances measured on an Arduino Uno show a PWM frequency of 263 Hz for 12 LED driven simultaneously.
By default the period is set to 900ms, the fading and brightening time is set to 250ms and the on time is set to 200ms. This correspond to blinking lights of french railway lights.
setup, you shall call
begin to start each LightDimmer and LightDimmerSoft objects. You can also set the fading and brightening time, the on time and the period for blinking process. See below.
loop, you shall call
LightDimmer::update() to call the library so that it update the state of each declared object.
Period, on, brightening and fading times are 16 bits integer. So the maximum values of the time settings are 65.535s.
Functions of LightDimmer
The following functions are available:
LightDimmer::update shall be called in loop to allow the library to update the state of each LED. If the call frequency is not high enough you will get discontinuous update in the fading and brightening process for LightDimmer objects (those using hardware PWM). LightDimmerSoft object (those using software PWM) will flicker. So you shall never use the delay function in your sketch.
As an option, update can take as argument the number of objects to update. This allows you to more finely interlace LightDimmer's work and the rest of your sketch. Passing a number of objects greater than the number of objects declared has no particular effect other than updating several times and unnecessarily the same object.
begin has to be called in setup for each LED.
pin is the pin to which the
LED is connected.
level is the logical level, HIGH or LOW, to light up the LED.
max is a 8 bits integer (
setMax sets the duty cycle to set the LED on. setMax allows to adjust the brightness of each LED of your signal without changing the resistor.
Set the time it take to fade the LED from the maximum value to the minimum value. time is in milliseconds and ranges from 1 to 65535.
Set the time it take to fade the LED from the minimum value to the maximum value. time is in milliseconds and ranges from 1 to 65535.
Set the time the LED stays on when blinking. time is in milliseconds and ranges from 0 to 65535.
Set the blinking period. the period should be greater or equal to the sum of the brightening time, the on time and the fading time. Otherwise, blinking cannot be done. time is in milliseconds and ranges from 2 to 65535.
max is a 8 bits integer (
setupMax turns the output on, sets the current value on and sets the duty cycle to set the LED on. setupMax allows to live adjust the brightness of each LED of your signal without changing the resistor.
Switch the LED on using the brightening time.
Switch the LED off using the fading time.
Starts the blinking provided the condition given in the setPeriod paragraph is fulfilled.
Stop the blinking.
Returns true if the LED is currently on
Returns true if the LED is currently off
Returns true if the LED is currently brightening
Returns true if the LED is currently fading
Returns true if the LED is currently blinking
uint8_t which is the maximum duty cycle value as set by
uint16_t which is the fading time of the LED
uint16_t which is the brightening time of the LED
uint16_t which is the on time of the LED
uint16_t which is the period of the LED
uint8_t which is the pin on which the LED is connected
uint8_t which is the current value of the duty cycle
Note about Travis CI
The LightDimmer Library examples are built on Travis CI for the following boards:
- Arduino Leonardo
- Arduino Uno
- Arduino Mega 2560
- Arduino Zero
- Arduino Due