OK, I am a bit bored… and loose focus too often sometimes, thus the movie is on pause
Here is a nice little sketch with three simultaneously running BLYNK_WRITE()
functions, each doing their own thing…
-
Increment and display a number every 100ms on the Terminal if the White button is ON, reset when OFF.
-
Flashing one of Blynk’s new large LED’s (BETA App at time of this post) depending on the frequency set by the Slider Widget (AKA my variable timer) 100ms-5000ms range.
-
Increment/Decrement a number, based on the Step Widget… as fast as you can press.
And as I kept recommending to avoid, wherever possible due to their blocking nature… not a single delay()
, for()
or while()
is to be found… and only one Blynk.run()
command required… thus no function is blocking the others.
Small Disclaimer… I am messing heavily with Blynktimers here, and not really paying attention to giving each timed action enough ‘time’ to complete, so there can be some slight degradation (AKA slowdown) when running the variable timed function at fast rates.
However, the BLYNK_WRITE()
function containing the simple Step Widget and indicating Display widget is completely unaffected by these slight timer degradations.
Yep, with smarter coding practice, multiple BLYNK_WRITE()
functions working concurrently as normal
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//Blynk setup
char ssid[] = "SSID";
char pass[] = "PASS";
char auth[] = "AUTH";
long variableTime;
int number;
int varTimer; // Setup Timer ID
int numTimer; // Setup Timer ID
int flashTimer; // Setup Timer ID
// WidgetTerminal blynkTerm(V1); // Not required as I am using direct Blynk.virtualWrite() commands to the terminal
BlynkTimer timer;
void setup() {
Blynk.begin(auth, ssid, pass); // Blynk.begin() is a blocking function until connected, so no need to have extra connection check delays
}
void loop() {
Blynk.run();
timer.run();
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V3);
}
BLYNK_WRITE(V0) { // Switch Widget for incremental counting to terminal
if (param.asInt() == 1) { //if button pressed down
Blynk.virtualWrite(V1, 0); // Send number to Terminal
// Timed Lambda Function - LED Flash
numTimer = timer.setInterval(100L, []() {
number++; // Increment number
Blynk.virtualWrite(V1, "-"); // Send number to Terminal
Blynk.virtualWrite(V1, number); // Send number to Terminal
}); // END Timer Function
} else {
timer.deleteTimer(numTimer); // Cancel previous Timeout Timer
number = 0; // Reset Number
}
}
BLYNK_WRITE(V3) { // Slider Widget for interval timing
timer.deleteTimer(varTimer); // Cancel previous Timeout Timer
Blynk.virtualWrite(V2, 0); // Turn OFF LED
variableTime = param.asInt(); // Get new interval time
if (variableTime <= 100) { // prevents a timer from becoming 100 or less
variableTime = 100;
}
Blynk.virtualWrite(V6, variableTime);
timerLoop(); // Call your timed loop
}
BLYNK_WRITE(V4) { // Increment/Decrement a number, based on the Step Widget
Blynk.virtualWrite(V5, param.asInt());
}
void timerLoop() { // This is my, adjustable, variable timer setup flashing an LED
timer.deleteTimer(varTimer); // Cancel existing Timeout Timer
Blynk.virtualWrite(V2, 255); // Turn ON LED
// Timed Lambda Function - LED Flash OFF
flashTimer = timer.setTimeout(50L, []() {
Blynk.virtualWrite(V2, 0); // Turn OFF LED
}); // END Timer Function
varTimer = timer.setTimeout(variableTime, timerLoop); // Set new Timeout timer and call this function again in X seconds
}
And for others to check out… I have also used various timer methods in many code examples - C++ Blynk (Legacy) - Code Examples for Basic Tasks