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

How can I pass parameters to lamba timer expression


#1

Hello Guys,
This is really useful post.
Many time use this code for triggering solenoid.

           timer.setTimeout(2000L, [](){  digitalWrite(pin1, HIGH); } );

how can I pass parameters to this lamba expression?

void TurnOn(int pinX){
           timer.setTimeout(2000L, [](){ 
            //digitalWrite(pin1, HIGH); 
              Serial.print("Pin No");
              Serial.println(pinX);
        } ); 
}

But this Gives this error

 note: the lambda has no capture-default
            timer.setTimeout(2000L, [](){ 
                                     ^
/Users/saurabh/Documents/Arduino/harplyn-rtu/harplyn-rtu.ino:16:17: note: 'int pinX' declared here
 void TurnOn(int pinX){
                 ^
exit status 1
'pinX' is not captured

SimpleTimer and Delay() question
#2

Take a look at this:

Not sure if it will help in your case, but worth a try.

If not, then it just may be easier to use the timer.seTimout as it was designed.

https://playground.arduino.cc/Code/SimpleTimer#F_setTimeout


#3

thanks,
it didn’t work.

void sloenoidTrig(int pinX){
  Serial.print("Pin No");
 Serial.println(pinX);
}


void TurnOn(){
int pinX =1;
//           timer.setTimeout(2000L, [&]()  { 
//            //digitalWrite(pin1, HIGH); 
//              Serial.print("Pin No");
//             // Serial.println(pinX);
//        } ); 

        timer.setTimeout(200L,sloenoidTrig);
}

How can i pass parmater to that sloenoidTrig function from the timer.setTimeout. ?


#4

By declaring pinX as Global.


int pinX =0;

void sloenoidTrig(){
  Serial.print("Pin No");
 Serial.println(pinX);
}


void TurnOn(){
pinX =1;
//           timer.setTimeout(2000L, [&]()  { 
//            //digitalWrite(pin1, HIGH); 
//              Serial.print("Pin No");
//             // Serial.println(pinX);
//        } ); 

        timer.setTimeout(200L,sloenoidTrig);
}

#5

When you put “int” in front of a variable name it re-declares that variable, setting its value to zero.
When you do this within a function, any values you assign to that variable are only available within that function.

You need to use global variables, and not keep re-declaring them locally, if you want the values assigned to those variables to be available through your code.

Pete.


#6

Thanks for the Reply,
you may be miss interpreted my question.
I know that declaring the Global variable works. but i want to pass the parameter to that lamba expression.so that I can turn ON & OFF the different pins from the same function.without right separate function for each pin.
like this

void triggerPin(int pin){
timer.setTimeout(200L,[](){
digitalWrite(pin,HIGH);
});

#7

I Google this error and found something that might be related?


#8

There’s two things I can’t get my head around here:

  1. Why not put the digitalWrite immediately after the lambda function, so it executes after the timeout timer has completed?

  2. What’s the point in having a function that waits 0.2 seconds then writes a specified pin HIGH? What’s the reason for the 0.2 second delay?

Pete.


#9

That IS what it does already…

@saurabh47 Of course with snippets, we don’t get the whole picture as to the whys :slight_smile: I am also wondering what pulls the pin LOW and why it is not included in the function, but prior to the Lambda timer for even more compact code.

Anyhow, this whole passing parameters is new to me… well… I have seen it, but never used it… so I am learning as I watch this topic :slight_smile:


#10

it will not work like that.
It first digitalRight then run the code inside the Lambda function at specified time.

In my project, I am using latching solenoid valves that need triggering pulse of 2ms for turning ON.


#11

Here is some code snippet. i want to make code more compact and with less repetition.

switch(atoi(data[1])){
      case 1: //Solenoid 1
          if(atoi(data[2])==1){ //Turn ON the Solenoid
            Serial.println("Solenoid 1 ON");  
               digitalWrite(OP1,HIGH);
               digitalWrite(OP2,LOW);  
               timer.setTimeout(150L,[](){
                Serial.println("Solenoid 1 ALL pin LOW");
               digitalWrite(OP1,LOW);
               digitalWrite(OP2,LOW);
            });
          }else{ //Turn OFF Solenoid
            Serial.println("Solenoid 1 OFF");  
               digitalWrite(OP1,LOW);
               digitalWrite(OP2,HIGH);          
               timer.setTimeout(150L,[](){
               digitalWrite(OP1,LOW);
               digitalWrite(OP2,LOW);
            });
          }
        break;
      case 2: //Solenoid 2
          if(atoi(data[2])==1){ //Turn ON the Solenoid
            Serial.println("Solenoid 2 ON");  
               digitalWrite(OP3,HIGH);
               digitalWrite(OP4,LOW);  
               timer.setTimeout(150L,[](){
                Serial.println("Solenoid 2 ALL pin LOW");
               digitalWrite(OP3,LOW);
               digitalWrite(OP4,LOW);
            });
          }else{
            Serial.println("Solenoid 2 OFF");  
               digitalWrite(OP3,LOW);
               digitalWrite(OP4,HIGH);          
               timer.setTimeout(150L,[](){
               digitalWrite(OP3,LOW);
               digitalWrite(OP4,LOW);
            });
          }
        break;

#12

Okay, doesn’t make sense to me, but I’ll take your word for it.

Thankfully, the way I use Blynk doesn’t require me to do stuff like this, so I’ll leave you to work out the solution.

Pete.


#13

Lambda functions are obviously not Blynk specific… do you not need any custom code with Node-Red?

The way a Lambda Timer works is instead of the timer calling a separate function after timeout, it runs the equivalent code embedded in the Lambda.

I don’t think it makes much difference in the compiled end, memory? processing?.. perhaps just a more compact way of visualising?


#14

I get what a lambda function does, it’s just that in this case I’m seeing it as being used as an equivalent of this code:

void triggerPin(int pin)
{
  delay(200);
  digitalWrite(pin,HIGH);
}

If that’s the case then I’d just put an empty lambda timer function in there, which solves the problem of passing the pin variable value into the lambda function.

If that’s not the case them I’m clearly missing the point.

Personally, I much more of a longhand coder, including putting curly brackets on their own line, because I find that easier to visualise.

Pete.


#15

yes you are right.

if you use empty Lambda function.in that case, it will not digitalwrite after 200ms delay.it executes the below code(digitalwrite) immediately.
I am avoiding delays.so it will not block other code.


#16

Good point… use of such a short delay is unlikely to cause any “Blynk” issues… unless being called frequently?

I can see that… except I can also get lost in the code… so keeping everything related (grouped) seems to help for me. I think that is why I like the lambda… that and is sounds like shorthand for my favorite car, the Lambo :racing_car: At least I can say I can use one of them :wink: