I have been interested in non-blocking code on microcontrollers for some time, and most people start off with the “Blink Without Delay” example that can be found in the Arduino IDE. The problems, if you can call them that, with some of these examples is they generate a handful of variables to track the state, and they are usually inflexible as they only allow a rhythmic ‘on for x time’, ‘off for y time’ pattern. Using this library means you don’t have to worry about how many and which variables are which if you want to use multiple LEDS. This library also allows the user to instantiate multiple different flash sequences on a single LED pin (and multiple pins if desired). Each sequence can be called when a different condition is met. How about a condition where the LED gives one quick flash every 10 seconds to show that all is well and remains off in the interim conserving power. Later, if some error condition occurs, the same LED could then give 3 longer flashes every 5 seconds to indicate the type of error. And so on.
The main technique behind this library is a class, which makes sense when you want to instantiate multiple similar objects. While looking into how people write their non-blocking code, I stumbled across this tutorial on Adafruit’s website written by Bill Earl. This library extends Bill’s example class by turning it into a library, as the alternative was pasting a large chunk of code into your sketch. It also adds the ‘status’ capability, as well as being able to designate if the LED pin uses HIGH or LOW to turn on. This gives some flexibility with your wiring, as well as portability across different development boards. For example, the Arduino UNO uses HIGH to turn on the built-in LED, whereas the Wemos D1 Mini uses LOW.
The file below includes the library files, a readme.txt with instructions, and an Arduino example sketch to demonstrate its use. Please feel free to use it as you see fit. If you decide to distribute it, it would be nice to receive some attribution or an email to say thanks, but I don’t require it. Enjoy!