Blinking a LED in an Arduino Sketch
May 25, 2018

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

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 mdBlinky object.

#ifndef mdBlinky_h #define mdBlinky_h #include "Arduino.h" enum ledActive_t { ACTIVE_LOW, ACTIVE_HIGH }; enum blinkMode_t { OFF, ON, BLINK }; class mdBlinky { public: // Create the object specifying the pin connected to the LED and // if the LED is activated (turned on) with a LOW or HIGH value mdBlinky(uint8_t pin, ledActive_t ledActive); //constructor // Set attributes // A value of OFF turns the LED off, of ON turns the LED on and // BLINK starts the blink mode. void setMode(blinkMode_t value); // Number of blinks (on periods) in each cycle void setBlinks(uint16_t value); // Time in microseconds (ms) the led is on when blinked void setBlinkOffTime(uint16_t value); // Time (ms) the led is off between two blinks void setBlinkOnTime(uint16_t value); // Time (ms) the led is off when not blinking void setOffTime(uint16_t value); // Number of cycles to run until the LED is turned off and the mode is then set off. // If value == 0, the blinking goes on indefinitely (this is the default). void setCycles(uint16_t value); // Set all blink parameters at once. This changes the mode to BLINK. void setParams(uint16_t blinks, uint16_t blinkOffTime, uint16_t blinkOnTime, uint16_t offTime, uint16_t cycles = 0); // Select a blink pattern in a linear array of uint16_t logically arranged // into 4 columns and as many rows as there are sets of parameters. // Ex. // uint16_t params_array[] = { 2, 200, 30, 1740, // heartbeat // 1, 0, 50, 50, // panic // 0, 0, 0, 1, // always on // 0, 0, 0, 0, // always off // Call with blinky.setParams(params_array, 1) to get the panic blinking // pattern. The last two entries are visually equivalent to setMode(ON) // and setMode(OFF) but make it easier to create an array of all // LED flashing patterns to be used a sketch to select the current // pattern on the fly with an index. // // This changes the mode to BLINK. void setParams(uint16_t params_array[], uint8_t index = 0, uint16_t cycles = 0); // Get attribute blinkMode_t getMode(void); // Pump to update LED, return current state of LED // false = LED is off, true = LED is on. // MUST BE CALLED on a regular basis (once in every // loop() iteration for example). boolean blink(); };

The methods that set the individual timing attributes do only that. The overloaded setParams methods that set all timing attributes at once have a side effect; they change the mode to BLINK. 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 offTime. That way, the mode will always be BLINK.

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 explicitly changed.

Here is an example. The LED will blink rapidly for 50 cycles whenever the sketch is started and then stay on.

#include <mdBlinky.h> mdBlinky blinky(LED_BUILTIN, ACTIVE_HIGH); void setup() { blinky.setParams(1, 0, 25, 25, 50); } void loop() { blinky.blink(); if (blinky.getMode() == OFF) { blinky.setMode(ON); }; }

If preferred, remove the last line in the loop() routine 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.