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

Break a timed loop

Hi, I’m trying to read a virtual pin, and if active, activate a physical pin for a certain number of seconds, and then always turn it off for a certain number of seconds.
I tried the following code and it works, only when the virtual pin becomes 0, the loop doesn’t stop.
How can I solve?
Thank you.

BLYNK_WRITE(V4)  // Virtual button on Vx to activate action
{
  int BTN = param.asInt();
  if (BTN == 1) {
    digitalWrite(ledpin, HIGH);  // Run ActionON function
    timer.setTimeout(1000L, ActionON);
  }
}

void ActionON()
{
  digitalWrite(ledpin, HIGH);  // Set pin high
  timer.setTimeout(2000L, ActionOFF);  // Run ActionOFF function in 5 seconds
}

void ActionOFF()
{
  digitalWrite(ledpin, LOW);  // Set pin Low
  timer.setTimeout(1000L, ActionON);
}

@salvatoredc33 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.

You have an if statement telling the sketch what to do if the button widget send a 1 (on)…

but there is no corresponding else or if (BTN == 0) command telling it what to do if a 0 (off) is received.

Pete.