Plural use of BLYNK_WRITE

Hello, I’m very beginner in C++, I added Terminal widget to V2 pin
Instead of using Switch for choosing options I’ve used if to send a word to Terminal in Blynk app. Because I don’t have any idea for reading input from Terminal in Blynk app hehe. and also using If function is more flexible I think.

Here is my code :

    `  BLYNK_WRITE(V2){
    `  if (String("clr") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"clr");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}

    `  BLYNK_WRITE_2(V2){
     ` if (String("A"),("a") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"");
     ` Blynk.virtualWrite(V2,"TEST A");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}`

I’ve successfully compiled the code without error, but when I want to add some if function.

    `  BLYNK_WRITE(V2){
     ` if (String("clr") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"clr");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}

     ` BLYNK_WRITE_2(V2){
     ` if (String("A"),("a") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"");
     ` Blynk.virtualWrite(V2,"TEST A");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}

     ` BLYNK_WRITE_3(V2){
     ` if (String("B"),("b") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"");
     ` Blynk.virtualWrite(V2,"TEST A");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}

     ` BLYNK_WRITE_4(V2){
     ` if (String("C"),("c") == param.asStr()) {
     ` Blynk.virtualWrite(V2,"");
     ` Blynk.virtualWrite(V2,"TEST A");} 
     ` else 
     ` {Blynk.virtualWrite(V2," Please type correct input !!! , Moving to homepage in 3s");
     ` delay(3500);
     ` {return;}}}`

I compiled it and get this error :

Preformatted text expected constructor, destructor, or type conversion before '(' token

What should I do? or anyone have a better suggest for my code?

What??? Are you inventing new Blynk functions or something? :stuck_out_tongue: I think you need to reread the Documentation and look at some Examples before reinventing the wheel :smile:

You don’t add any underscore & number to a function command, each one is already identified by the vPin used.

BLYNK_WRITE(V2){
// do stuff here when widget attached to V2 is processed
}

BLYNK_WRITE(V3){
// do stuff here when widget attached to V3 is processed
}

BLYNK_WRITE(V4){
// do stuff here when widget attached to V4 is processed
}

PS - the approved and better looking way to format posted code in this forum is also in the Documentation

Blynk%20-%20FTFC

I dont invent it. I read my errors code and found that instruction to use that way. and I just used one vPin for Terminal. not many.

BLYNK_WRITE(V1) for if 1st
BLYNK_WRITE_2(V1) for if 2nd … and so on.

the problem is when I compiled till 2 line compiled perfectly. but when I add line 3 and four ( BLYNK_WRITE_4(V1) ) I got error as I said above. I dont ask how to use Blynk_Write

Umm, are you sure? I don’t think you really understand how all the other thousands of Blynk users are doing it :stuck_out_tongue_winking_eye:

Each BLYNK_WRITE(vPin) function needs it’s own (distinct) virtual pin… why you even need to use multiple functions for same pin?? Just use one function and do all the things :smiley:

As for the functions… as stated before…when whatever widget is associated with that vPin changes state or gets data (AKA entering text in the terminal and pressing ENTER) then that associated function is called, the state/value of the vPin is transferred to a variable (and if necessary converted - E.G param.asInt() for integer) via the param.asStr() command and processed by whatever code you have.

Instead of playing code sleuth and trying to dig into the library internals (well, until you understand the basics at least)… just use the function the way it is intended for proper coding use :wink:

PS don’t rely on delay() with Blynk… or even with ESP8266… or you are just asking for disconnection issues. Use timers instead… also shown in the Docs and Examples.

1 Like