#12 - Timers simplified… perhaps… 
Some have a difficult time understanding timers… and since this is a fundamental building block of Blynk… I buried this reference in the middle of this entire topic
Well anyhow…
This is a “real world” timer example where i am using two timers to alternately turn ON and OFF an LED… yes, there are many ways to do this, but this example is all about timers.
-
The first timer will run it’s assigned function 500ms (1/2 second) after bootup, and every 500ms after.
-
The second timer will run it’s assigned function 1000ms (1 second) after bootup and every 1000ms after
This in effect will cause the LED to blink ON and OFF every 1/2 second.
In this case I used both timers for the same LED action, but normally you would run one timer to run a function that flashes the LED on and off (we will look a various ways to do that later), while the 2nd would run a function that gets sensor data and sends it to the App… and so on.
I did it this way to show that you can have multiple timers running (up to 16 per instance)… in this case the instance is called timer… but could have easily been called blinker, flasher or fred… just be sure to declare the timer name and use it accordingly in the setup… E.g.
BlynkTimer fred;
fred.setInterval(500L, blinkMyLEDon);
But for this example, lets stick with the name timer 
NOTE Because both these timers initialize at the same time (at boot) it is important to stagger timers (more info below) enough to allow their associated functions to complete their task… otherwise you can get some interesting stuttering and lockups in your sketch.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxxxx"; // Token for Device A
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx"; // IP for your Local Server
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
#define DeviceLED 2 // Built in LED for Wemos or NodeMCU v3, Or 16 for NodeMCU v1
void setup() {
pinMode(DeviceLED, OUTPUT);
WiFi.begin(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect();
timer.setInterval(500L, blinkMyLEDon); // This will call the Arduino Function 'void blinkMyLEDon()' every 500ms (1/2 second)
timer.setInterval(1000L, blinkMyLEDoff); // This will call the Arduino Function 'void blinkMyLEDoff()' every 1000ms (1 second)
}
void blinkMyLEDon() {
digitalWrite(DeviceLED, LOW); // Turn ON built in LED (triggered LOW)
}
void blinkMyLEDoff() {
digitalWrite(DeviceLED, HIGH); // Turn OFF built in LED (triggered LOW)
}
void loop() {
Blynk.run();
timer.run(); // This keeps checking the timers to see if then need to be processed
}
-
Latch & indicator Here is another example with the timers that, with a press of a button, you can trigger a relay on for 10 seconds, while flashing an indicator LED the entire time.
NOTE: This “time”
the timers are NOT initialized in the void setup()
since they do not need to run all the time from boot. These timers setTimer()
and setTimout()
just run when called, and for as long as told to, and that is it.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx"; // IP for your Local Server
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
int Latch;
int Flag;
#define DeviceLED 2 // Built in LED for Wemos or NodeMCU v3, Or 16 for NodeMCU v1
#define LatchRelay 12 // GPIO12 or D6
BlynkTimer timer;
void setup() {
pinMode(DeviceLED, OUTPUT);
pinMode(LatchRelay, OUTPUT);
WiFi.begin(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect();
}
//===== LATCH(Relay) & LED PULSE - BLYNK Functions =====
BLYNK_WRITE(V0) // Virtual button on V0 to activate Relay_LED pulses
{
//Serial.println("Latch LED");
Latch = param.asInt();
if (Latch == 1 && Flag == 0) {
Flag = 1; // Keeps from allowing button press more then once while relay activated
digitalWrite(LatchRelay, HIGH); // Activate Relay
timer.setTimer(1000L, blinkMyLEDon, 10); // Pulse LED routine (LedON/LedOFF) 10 times
timer.setTimeout(10000L, RelayOFF); // Deactivare Relay after 10 seconds
} // END if
} // END Blynk Function
void RelayOFF() {
digitalWrite(LatchRelay, LOW);
Flag = 0; // reset flag after relay disables
} // END Function
void blinkMyLEDon() {
digitalWrite(DeviceLED, LOW); // Turn ON built in LED (triggered LOW)
timer.setTimeout(500L, blinkMyLEDoff); // Run LED OFF routine once in 1/2 second
} // END Function
void blinkMyLEDoff() {
digitalWrite(DeviceLED, HIGH); // Turn OFF built in LED (triggered LOW)
} // END Function
void loop() {
Blynk.run();
timer.run(); // This keeps checking the timers to see if then need to be processed
}
Staggering Timers:
Stagger the timers so that each one starts a little later then the prior one… but also giving its function time to complete.
Also try not to have timers match on alternating cycles… like a timer every 1 second another every 5 seconds and a third every 60 seconds… as some and/or all will eventually try to run at the same time every few cycles.
For example… eight timed functions, running all timers within 200 ms from start to finish, and repeating the process every second… assuming 20 ms is enough time for each function to complete its task before the next one runs…
timer.setInterval(1000L, myFunction1);
timer.setInterval(1020L, myFunction2);
timer.setInterval(1040L, myFunction3);
timer.setInterval(1080L, myFunction4);
timer.setInterval(1100L, myFunction5);
timer.setInterval(1120L, myFunction6);
timer.setInterval(1140L, myFunction7);
timer.setInterval(1160L, myFunction8);
NOTE: While each timer starts counting at the same time (as is the way it should be) , they run each of their respective first functions 20ms later then it’s predecessor, and thus each subsequent function call continues to run at a different time hence forth. any eventual overlap (because I am using evenly divisible timers in this example) is few and far between. I have actual projects running like this without issues.
You can test if 20ms is enough by having print statements at the beginning and end of each function… then if one function starts before the other ends, you will see the print statements out of order. Be aware that the print statements themselves will take a small amount of time to process.
If the actual time between individual timers is more important then having them all start at the same time (AKA they all MUST be one second intervals, but still not running at the same time), you can also stagger timers this way…
timer.setInterval(1000L, myFunction1);
delay(20);
timer.setInterval(1000L, myFunction2);
delay(20);
timer.setInterval(1000L, myFunction3);
delay(20);
timer.setInterval(1000L, myFunction4);
delay(20);
timer.setInterval(1000L, myFunction5);
delay(20);
timer.setInterval(1000L, myFunction6);
delay(20);
timer.setInterval(1000L, myFunction7);
delay(20);
timer.setInterval(1000L, myFunction8);