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

Simple Time Delay

Hello Blynkers,
I am new to Blynk and not yet an Arduino pro either. I am looking for the best way add a delay to a button, similar to the basic example blink sketch. I am connected to an Android Tablet using an HC-05 Bluetooth module, I have read using delay() can cause disconnections. I don’t want to use the Timer widget because I have no need to set a specific time within a 24hr clock. Press the button, LED comes on for x seconds and then turns back off until the next press of the button.

What is the route I should take? Is anyone able to provide a basic example to help me understand the function? Any feedback is appreciated, thanks all.

BlynkTimer (in code, not a widget) and use the timeout timer functionality.

Pete.

Hello Pete,
Thank you for the quick response. I have seen the BlynkTimer used in code before but never the timeout timer function. Are there any examples you have seen that may help me? I am very much a visual learner when it comes to coding, very new still.

https://community.blynk.cc/search?q=timeout%20timer

Pete,

1 Like

There is the Lambda timer that works well. So below would turn a virtual LED on and after 1second turn it back off.

Blynk.virtualWrite(V12, 255);
timer.setInterval(1000L, []() {
    Blynk.virtualWrite(V12, 0);
  });
1 Like

To expand on @daveblynk’s example.

This would only turn on/off once.


// When App button is pushed - Do something
BLYNK_WRITE(V2) {
  buttonState = param.asInt();
  if (buttonState == 1)
   {
     Blynk.virtualWrite(V12, 255);
     digitalWrite(ledPin, HIGH);
     timer.setTimeout(1000L, []() {
     Blynk.virtualWrite(V12, 0);
     digitalWrite(ledPin, LOW);
                });
    }
}

A more drawn out example would be:

// When App button is pushed - Do something
BLYNK_WRITE(V2) {
 buttonState = param.asInt();
  if (buttonState == 1)
   {
     Blynk.virtualWrite(V12, 255);
     digitalWrite(ledPin, HIGH);
     timer.setTimeout(1000L, OFF); 
    }
}

void OFF()
{
     Blynk.virtualWrite(V12, 0);
     digitalWrite(ledPin, LOW);
}

  
2 Likes