BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Loop for blinking led with virtual pin

Hello!
I want to make a led to blink when I “press” the button on app Bynk.
I tried this:

BLYNK_WRITE(5) 
{
  if (param.asInt()) {
    digitalWrite(LED_SEMNAL, HIGH);
    delay(500);
    digitalWrite(LED_SEMNAL, LOW);
    delay(500);
    // 1 semnal
    digitalWrite(LED_SEMNAL, HIGH);
    delay(500);
    digitalWrite(LED_SEMNAL, LOW);
    delay(500);
    // 2 semnal
    digitalWrite(LED_SEMNAL, HIGH);
    delay(500);
    digitalWrite(LED_SEMNAL, LOW);
    delay(1000);
    // 3 semnal
  } else {
    digitalWrite(LED_SEMNAL, LOW);
  }
}

I’ve tried this, but it only blinks 3 times. I wish it was a loop.
When the button is pressed to operate in the loop.

@Dolly please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

You can’t use delays like that in Blynk.

Maybe reading this will help…

Pete.

I apologize, but my knowledge is not very rich in programming. I can’t handle it, can you give me another example?
I tried this, but nothing happens.
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#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


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "_2DX-EyR20G_KRKk9vD";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "1";
char pass[] = "t20";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  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
  blueticker.attach(0.5, flashblue);
}

void loop()
{
 Blynk.run();
}

You haven’t included any code to attach and detach the ticker.
With your original code you’d do this in the BLYNK_WRITE(V5) callback function.

Pete.

1 Like

Superb!
I made him blink. But I would like it to flash 3 times at one second intervals, then 2 seconds to be stopped, and to start again.

That’s what’s known as Project Scope Creep!

Looks like you’re probably back to using lambda timers or a combination of a timer and a ticker, but I don’t have the time or the inclination to explain the detail of how you might implement that.

Pete.