Using 2 virtual pins

Hi,
I’m using two push buttons on the app (V1 and V2) with the code below to activate 2 relays however, only the routine with V1 works. I cannot understand why when both of them are identical.
Any help is appreciated.

#include <Bridge.h>
#include <BlynkSimpleYun.h>

//=== set pin numbers ===
const int blindsOpenRelayPin = 5; // (digital out) open main blinds (persiana)
const int centralitaRelayPin = 6;  // (digital out) reset centralita

/* Blynk auth token */
char auth[] = "xxx";
int blinds, cent; // used for Blynk

void setup() {
  pinMode(blindsOpenRelayPin, OUTPUT);
  pinMode(centralitaRelayPin, OUTPUT);
  digitalWrite(blindsOpenRelayPin, HIGH);
  digitalWrite(centralitaRelayPin, HIGH);

  Bridge.begin();
  Console.begin();

  Blynk.begin(auth);
}

void loop() {
  // update Blynk
  Blynk.run(); // has to be the last line
}

/* Blynk controls */
BLYNK_WRITE(V1)
{
  blinds = param.asInt();
  if ( blinds == 1)
  {
    digitalWrite(blindsOpenRelayPin, LOW); // activate relay
    delay(900);
  }
  else if ( blinds == 0) {
    digitalWrite(blindsOpenRelayPin, HIGH); // desactivate relay
  }
}

BLYNK_WRITE(V2)
{
  cent = param.asInt();
  if ( cent == 1)
  {
    digitalWrite(centralitaRelayPin, LOW); // activate relay
    delay(10000);
  }
  else if ( cent == 0) {
    digitalWrite(centralitaRelayPin, HIGH); // desactivate relay
  }
}

@Emilio

delay(900) and delay(10000) are FAR from identical.

basically delay() is NOT used with Blynk, use SimpleTimer i.e. one of the five libraries you manually installed when you set up your system.

If you are lucky 900 will work, 10000 will not work as Blynk has a 5s to 10s hearbeat timeout. Blynk doesn’t see your hearbeating during the 10000 delay so assumes you are dead and puts you 6 feet under (disconnects you).

Study SimpleTimer and lose ALL delays.

Got it. Thank you.

Hi again,
I tried the following code as a delay but it’s not working as expected. The delay is much shorter than requested and, at times, it gets stock in a loop. Meaning the relay stays on forever. Surely I’m doing some wrong.

BLYNK_WRITE(V2)
{
  cent = param.asInt();

  if ( cent == 1)
  {
    digitalWrite(centralitaRelayPin, LOW); // activate relay
    Blynk_Delay(10000);
  }
  else if ( cent == 0) {
    digitalWrite(centralitaRelayPin, HIGH); // desactivate relay
  }
}
void Blynk_Delay(int milli) {
  int end_time = millis() + milli;
  while (millis() < end_time) {
    Blynk.run();
    yield();
  }
}

As @Costas noted, use SimpleTimer.

Look at this example:

#include <SimpleTimer.h>

SimpleTimer timer1;
SimpleTimer timer2;

[your other setup() code]

void loop() {
  timer1.run();
  //your other code in loop here
}

BLYNK_WRITE(V2) {
  cent = param.asInt();
  if ( cent == 1)
  {
    digitalWrite(centralitaRelayPin, LOW); // activate relay
    timer1.setTimeout(10000, deactivateRelayV2);
  }
}
 
void deactivateRelayV2() {
  digitalWrite(centralitaRelayPin, HIGH); // deactivate relay
}

You can see that you use the SimpleTimer library to set a one-shot delay (setTimeout) and define a function name to call once the timer has expired; in this case, deactivateRelayV2().

This method does not get in the way of the other background routines of the ESP8266 or Blynk.

Thank you.