BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

BlynkTimer and Correct Function Use

Hi Guys, could I confirm the below is valid in regards to timer use and delays within the function? or does this cause the same issues as having delays in the loop which isnt recommend?

If so, how do you control an LED blink which requires some sort of delay or do you need to use a timer for on and then off if timing intervals to make it look like it is blinking?


#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_ADS1015.h>
#include <Wire.h>

#define GREEN_LED 2
#define RED_LED 17

char auth[] = "removed";
char ssid[] = "removed";
char pass[] = "removed";

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);

  timer.setInterval(1500L, greenledblink);
  timer.setInterval(1500L, redledblink);
}

void loop()
{
  Blynk.run(); // all the Blynk magic happens here
  timer.run(); // BlynkTimer is working...
}

void greenledblink() {
  digitalWrite(GREEN_LED, HIGH);
  delay(1000);
  digitalWrite(GREEN_LED, LOW);
  delay(1000);
}

void redledblink() {
  digitalWrite(RED_LED, HIGH);
  delay(1000);
  digitalWrite(RED_LED, LOW);
  delay(1000);
}

Basically there should not be any delay in your whole sketch. It just stops the whole code and it will cause connection problems or just crash the code. Search “Blink without delay” on google.

Yes, it does, and not only isn’t it recommended, it will make your sketch unresponsive to commands from the Blynk app and ultimately cause disconnections from the Blynk server.

My favourite way of Blinking an LED is to use the Ticker library (although if you do this, beware that Ticker uses seconds, or decimal fractions of a second as the time parameter, not milliseconds as with delay or BlynkTimer parameters.

You can achieve the same result with a BlynkTimer in timeout mode and preferably using a Lambda function, but it requires a bit more thought to be applied (I prefer the easy option).

Pete.

awesome, thanks heaps for that. Dont have many delays so could just turn them into functions with Blynktimers for simplicity sake, easy logic. Will Read up as you suggested

thanks for the info, what is BlynkTimer in timeout mode?

Search the forum for “timeout” and “lambda”

Pete.

Follow this link. This is a great post by @Gunner.
You will how to blink an virtual lead on post 25.

You can understand on how to use the timeout function as mentioned by @PeteKnight.