IF Statement Inside a Timed Lambda Function?

I noticed while reading one of @Gunner post he used a clever bit of coding with a Timed Lambda Function.

  // Timed Lambda Function - UpTime
  upTime = timer.setInterval(500L, []() {
    Blynk.virtualWrite(V12, millis() / 1000);
  });  // END Timer Function

I’m trying to use @Gunner timer example with an encased “If Statement”.

BLYNK_WRITE(V13) {
  int autoOff = param.asInt();
  if ((autoOff == HIGH) && (lightsOn > 0) && (X1 == 1) || (X2 == 1) ) {
    timer1 = tripWire.setTimeout(lightsOn * 3600000L, []() {
    if (autoOff == HIGH) {
    digitalWrite(Exterior, LOW);
    }
    });

The purpose of the second “if (autoOFF == HIGH)” is to re-verify after the timers time has past that the condition is still HIGH (Blynk Virtual Button is still HIGH) before the next command possible hours later. As soon as I put the IF statement within the timer1’s statement, I get an error that “… autoOff is not captured”.

Everything compiles fine until I throw in the second IF Statement no matter how simple or nested.

Can you not use “IF Statements” within a Timed Lambda environment within Blynk?

Having been a student of @Gunner work, comments and code, I’ve searched high and low and only seen a limited number of comments or code similar to this and none with IF Statements nested inside.

Thanks in advance

No problem… if properly placed. I am just polishing up another simple LED flasher using if() statements inside two nested Lambdas to control the flow. I will post a link here when ready.

Thanks @Gunner.

I’ll watch for it.

OK, here it is…

Some Googling (here is one example) show that this error can be “eliminated” by adding in this [&] ampersand (don’t ask me why :stuck_out_tongue_winking_eye: ), but that brings another error…

no matching function for call to 'BlynkTimer::setTimeout(long int, BlynkWidgetWrite13(BlynkReq&, const BlynkParam&)::__lambda3)'

But, this WILL compile,

tripWire = timer.setTimeout(lightsOn * 3600000L, [&]() {
      if (1 == 1) {
        digitalWrite(1, LOW);
      }
    });

So it appears to do with how the variables autoOff and Exterior need to be addressed assigned, filled, whatever, when referenced inside the lambda… a bit beyond my understanding right now.

Thanks for your effort @Gunner, I ended up with this workaround.

BLYNK_WRITE(V11) {
  int GARAGEVirtualButton = param.asInt();
  if (GARAGEVirtualButton == HIGH) { // virtual pin V11 goes HIGH when pressed in the app.  SWITCH
    digitalWrite(GarageLights, HIGH);
    X2 = 1; // state of light ON
    Blynk.virtualWrite(V16, String("Garage Lights ON"));
  }
  else if (GARAGEVirtualButton == LOW) {
    digitalWrite(GarageLights, LOW);
    X2 = 2;  // state of light OFF
    Blynk.virtualWrite(V16, String("Garage Lights OFF"));
  }
}

Added a Slider to adjust how long I feel like having my lights on that night.

BLYNK_WRITE(V12) { // Slider for Ext Light Hours
  int lightsOn = param.asInt();
}

Then the section to check what’s on and what to turn off. This is where the need to “re-verify” came in.

BLYNK_WRITE(V13) {
  int autoOff = param.asInt();
  if ((autoOff == HIGH) && (lightsOn > 0) && (X1 == 1) || (X2 == 1) ) {
    timer1 = tripWire.setTimeout(lightsOn * 3600000L, []() {
      Blynk.syncVirtual(V13);
      lightsOff();
    });
  }
  else if (autoOff == LOW) {
    tripWire.deleteTimer(timer1);
  }
}

And sends it off to this Void ()

Void lightsOff()
{
  if (autoOff == HIGH) {
    if ((X1 == 1) && (X2 == 1)) {
      digitalWrite(PorchLights, LOW);
      digitalWrite(GarageLights, LOW);
      Blynk.virtualWrite(V13, LOW); // sets the Blynk Button to OFF
    }
    else if ((X2 == 2) && (X1 == 1)) {
      digitalWrite(PorchLights, LOW);
      Blynk.virtualWrite(V13, LOW);  // sets the Blynk Button to OFF
    }
    else if ((X2 == 1) && (X1 == 2)) {
      digitalWrite(GarageLights, LOW);
      Blynk.virtualWrite(V13, LOW);  // sets the Blynk Button to OFF
    }
  }
}

This really isn’t that big of a deal. I’m just trying to rain in control of my exterior lights and turn them off, whatever combination may be on. I’m sure this will take some Debugging.

If anyones feeling generous, I’m dying to figure out how to sample my HOT Lead (Mains) to the actual light fixture so I can confirm juice is flowing to which lights and when. Just an ask. :wink:

Something like this… whatever type can be best adapted to a MCU.

https://www.sparkfun.com/products/11005