More Blink Without Delay

I often struggle with LED patterns/buzzer patterns once I have finished a project. Say I have a timer that counts down then at the end of the timer 2,3,or4 LEDS will flash in a certain pattern and a buzzer will go off. All of these On/Off Tone/NoTone items require some sort of timing. Delay is not an option in any of my programs so I thought i’d try and catalogue all the different ways I know for timing 1,2 or more items.

Lets start with the original

Blink without delay

 

What if we need a different on and off time – usually the case

 

Lets go a little further – Say we wnated to flash 2 LEDs at 2 different rates. Something that would be almost impossible using Delay() alone.

 

Looking at the code there are multiple duplicates, lets make a class and make the code more flexible.

First we declare a flasher class

Next we add a constructor.  The constructor has the same name as the class and its job is to initialise all the variables.

Finally we take our loop and turn it into a member function called “Update()”.  Note that this is identical to our original void loop().  Only the name has changed.

Now lets use it:

Now, for every LED that we want to flash, we create an instance of the Flasher class by calling the constructor.  And on every pass through the loop we just need to call Update() for each instance of Flasher.

There is no need to replicate the entire state machine code anymore.  We just need to ask for another instance of the Flasher class!