This is my second Arduino library that takes care of the minutia of flashing a LED. It's a blinky! Obviously, there is nothing much to it, I just wanted a reusable module that had some flexibility yet was simple to use.
The blinky can be in one of three modes: ON, OFF and BLINK which should be self explanatory. When in BLINK mode, the LED is operated in cycles. Each cycle begins with one or more flashes of the LED. And then the cycle ends with the LED off. The number of times the LED is blinked at the beginning of each cycle, the time of each blink, the time the LED is off between blinks, and the time the LED is off at the end of the cycle can all be set.
+-------------1 cycle----------+ | | +------blinks---+ | | ||<--offTime-->| | || | X----XXX---------------XXX----XXX----XXX---------------XXX---XXX---XXX | | | | | | -->| |<--blinkOffTime | | -->| |<- blinkOnTime
Times are specified in milliseconds (ms).
- Total time the LED is on in a cycle = blinks*blinkOnTime
- Total time of the blink phase = blinks*blinkOnTime + (blinks-1)*blinkOffTime
- Period of a cycle = blinks*blinkOnTime + (blinks-1)*blinkOffTime + offTime
Because there can be more than one LED connected to a device and since it would likely be that the are on and off at different times, it made sense to implement the blinky as an object.
Here is the header file defining the
The methods that set the individual timing attributes do only that. The
setParams methods that set all timing attributes
at once have a side effect; they change the mode to
I know this is not supposed to do that, but it makes sense to me. So far
I have used this object to control a LED that gives feedback to the user
about the state of the firmware. Do that, I set up all the blink patterns
needed in a linear array and then just select the needed pattern using the
indexed version of
setParams. To make things simpler, the
object recognizes two special patterns that begin with zero blinks. The
LED will be turned on or off according to the value of the
That way, the mode will always be
The example sketch supplied with the library, shows how this is done.
There is another timing attribute called
cycles. If positive,
then the object will be put in
OFF mode after performing the
specified number of cycles after being put in
BLINK mode. If
the attribute is zero, then
BLINK mode does not end until
Here is an example. The LED will blink rapidly for 50 cycles whenever the sketch is started and then stay on.
If preferred, remove the last line in the
and the LED will go off after blinking.
This is getting repetitive, but this is only a second attempt at creating an Arduino library and I remain a C/C++ neophyte. Perhaps the library could be useful as a starting point for someone wanting to do something better. In any case here it is: mdBlinky.zip.