Personally, I take the easy way out and rather than wrestling with multiple lambda timers I use the Ticker library to flash an LED with the various frequencies that I’m looking for.
The Ticker library is non-blocking, so won’t impact your other code execution.
In this example I use it to flash an LED at 0.1 second frequency (on for 0.1 seconds, off for 0.1 seconds. This is the setup code which includes the library, declares the LED pin, initialises the Ticker object and sets the LED pin to output.
It also adds the flashblue
function which toggles the state of the LED pin whenever it is called (which we will do later with the ticker object we’ve initialised here)…
#include <Ticker.h> // Install from Arduino Library Manager
#define blue_led 2 // NodeMCU onboard LED attached to GPIO2 (D4)
Ticker blueticker; // Initialise the Ticker object called blueticker
void setup()
{
pinMode(blue_led, OUTPUT);
}
void flashblue() // Non-blocking ticker for Blue LED
{
int state = digitalRead(blue_led); // get the current state of the blue_led pin
digitalWrite(blue_led, !state); // set pin to the opposite state
}
To start the LED flashing at the required speed you just add this line of code…
blueticker.attach(0.1, flashblue); // start blueticker with 0.1 second flash rate
When you want to stop the LED from flashing you just detach the ticker object. At this point the LED might be on or off (50% chance of either), so we want to force it to a particular state - in this case off (HIGH)…
blueticker.detach(); // stop the blueticker
digitalWrite(blue_led, HIGH); // LED is active LOW, so force it to be off
If you wanted the LED to flash at 0.1 second intervals for 60 seconds then you could use a combination of your existing lambda timer and the ticker code, attaching the tickler at the start of the 60 second timer and detaching it (and setting your LED to the required state) at the end of the timer.
Pete.