#25 - Das Timed Blinkin’ LED… Without any delay()
- AKA Timerception
This is simply just two nested Lambda functions using BlynkTimer and two different timer types to flash a Virtual LED ON for 1 second and Off for 1 second… If this is placed in the void setup()
it just repeats in the background… marquee style.
Here it is integrated into a bare bones Blynk Blink Sketch… There should be enough commenting to figure it out from here. LED Widget set to V0
#include <ESP8266WiFi.h> // for ESP8266
#include <BlynkSimpleEsp8266.h> // for ESP8266
#define BLYNK_MSG_LIMIT 0 // In theory this disables the "anti-flood governor"... might only work on Local Server
BlynkTimer timer;
char auth[] = "xxxxxxxxxx"; // Auth Token
char ssid[] = "xxxxxxxxxx"; // Your WiFi network name
char pass[] = "xxxxxxxxxx"; // Your WiFi network password
char server[] = "blynk-cloud.com"; // Cloud Server address
int port = 8080; // MCU Hardware Device port
void setup() {
pinMode(2, OUTPUT); // Setup GPIO2 (D4 the ESP On-Board LED) as an output pin
// Connect to your WiFi network, then to the Blynk Server
WiFi.begin(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect();
// This dual nested timer routine will constantly blink both a Virtual LED and the onboard LED of an ESP8266 Dev Board
timer.setInterval(2000L, []() { // Start 1st Timed Lambda Function - Turn ON LED every 2 seconds (repeating)
timer.setTimeout(1000L, []() { // Start 2nd Timed Lambda Function - Turn OFF LED 1 second later (once per loop)
Blynk.virtualWrite(V0, 0); // Turn Virtual LED OFF
digitalWrite(2, HIGH); // Turn ESP On-Board LED OFF
}); // END 2nd Timer Function
Blynk.virtualWrite(V0, 255); // Turn Virtual LED ON
digitalWrite(2, LOW); // Turn ESP On-Board LED ON
}); // END 1st Timer Function
}
void loop() {
Blynk.run();
timer.run();
}
So… why…?
Well I have heard that it is too complicated to make or understand the code to blink an LED without using delay()
You be the judge
Also, because it is a nice, non-blocking (payload depending), simple package of code that can be used to repeatedly do just about anything you want, as many times as you need (E.g. replace the first setInterval()
timer with a setTimer()
and designate your total loops to run. See - timer.setTimer()
Challenge - I am nesting only two functions here… How many nested lambda timed functions can you make use of? Either for practical or just visual purposes.