[SOLVED] Local and global Variables/ BLYNK_WRITE

How to use local variables global

I have read a lot of contributions but didnt found a solution yet.

I have difined variables in the BLYNK_WRITE operation but i am not able to use them global for Blynk.virtualWrite().
I like to compare 2 or more variables and show them on a value display.

How is it possible to get the variable out of the BLYNK_WRiTE() and use it on an other point. When i define the variable global the value display alwasy shows 0.

Thanks for your help.

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>

char auth[] = "d1d...............................fcf22e";

int pinValue;
int finalValue;

BLYNK_WRITE(V1)
{
pinValue = param.asInt();
}

BLYNK_WRITE(V2) {
int finalValue = param.asInt();
Blynk.virtualWrite(V3 ,int(finalValue));
}

void setup()
{ 
pinMode(8,OUTPUT);
     
Serial1.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
Blynk.virtualWrite(V4, int(pinValue));
      
}
            


    void loop()
    {
      Blynk.run();

    }

V3 works fine inside th blynk_write but i want to do it like V4.
I want to use the parameters global.
Thank you.

1 Like

Global variable should work. Show us the code.

You use sync for this as you want the variable in setup().

Thank you for the quick response.
how can i use it. As you see i am a beginner.

global definition:

int V4variable;

in setup() after Blynk.begin()

Blynk.syncVirtual(V4);

V4 like your V1 but swap pinValue for V4variable (or whatever name you declare it as).

If this doesn’t help provide details of what you really want to do.

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>

char auth[] = "d1d..............................fcf22e";

int V3variable;
int V4variable;
int V3plusV4 = V3variable + V4variable; // add the two values
BLYNK_WRITE(V1)
{
V4variable = param.asInt();
}

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

}

void setup()
{ 
Serial1.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
Blynk.syncVirtual(V4);
Blynk.virtualWrite(V4, int(V4variable));
Blynk.syncVirtual(V3);
Blynk.virtualWrite(V3, int(V3variable));
Blynk.syncVirtual(V7);
Blynk.virtualWrite(V7, int(V3plusV4)); //show the added values in a 3rd value display
}
            


void loop()
{
Blynk.run();

}

I try to get data from two sliders with blynk_write. Than show those two values in a value display and add them and show the result in a 3rd display.
the problem is that i am only able to use the variables inside the BLYNK_WRITE() funktion. Outside the displays are always 0 because the global variables doesnt change.

hope u can help me. Thanks :slight_smile:

Presumably you want to add slider 1 value to slider 2 value every time the slider changes and not just on bootup, right?

Exactly.at the moment it is not even adding when it boots up.

This is working on a WeMos, change the settings for your hardware:

// SumSliders.ino https://community.blynk.cc/t/local-and-global-variables-blynk-write/15720

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[]   = "xxxx";
char pass[]   = "xxxx";
char auth[]   = "xxxx";
char server[] = "xxxx";

int Slider1;
int Slider2;
int Display; // sum of slider 1 and slider 2

void setup()
{ 
  Serial.begin(115200);
  Serial.println();
  Blynk.begin(auth, ssid, pass, server);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
}

void loop()
{
  Blynk.run();
}


BLYNK_WRITE(V1)
{
  Slider1 = param.asInt();
  Display = Slider1 + Slider2;
  if(Slider2 != 0){                   // just a hack for initial boot when 2nd slider doesn't yet have a value
    Blynk.virtualWrite(V3, Display);
    Serial.println(Display);
  }  
}

BLYNK_WRITE(V2) {
  Slider2 = param.asInt();
  Display = Slider1 + Slider2;
  Blynk.virtualWrite(V3, Display);
  Serial.println(Display);
}

@Dmitriy interestingly when you “dab” the slider rather than slide the slider it doesn’t appear to update the value. By design?

Edit: if you are a “dabber” rather than a slider then setting the SEND ON RELEASE to OFF will update the values. But don’t slide the slider with it set to OFF as you are sure to flood the server and get disconnected.

1 Like

Thank you very much so far :smiley:
the next problem is that the result in the display only refreshes the first time I connect to the app. When i change the values of the sliders by dabbing the value in the display doesnt change.

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>

char auth[] = "d1d76.............................cfcf22e";

int Slider1;
int Slider2;
int Display; // sum of slider 1 and slider 2

void setup()
{ 
Serial1.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
}

void loop()
{
Blynk.run();
}


BLYNK_WRITE(V1)
{
Slider1 = param.asInt();
Display = Slider1 + Slider2;
if(Slider2 != 0){                   // just a hack for initial boot when 2nd slider doesn't yet have a value
Blynk.virtualWrite(V3, Display);
Serial.println(Display);
}  
}

BLYNK_WRITE(V2) {
Slider2 = param.asInt();
Display = Slider1 + Slider2;
Blynk.virtualWrite(V3, Display);
Serial.println(Display);
}

Read my last post in full and then indicate if dabbing doesn’t work.

Are you a dabber or a slider?

Solved the Problem on my own. The line Serial.println(Display) denied the sending.

Iam really thankfull that u helped me withe the problem Costas. :slight_smile: you are doing a great job.

     #define BLYNK_PRINT Serial1
        #include <BlynkSimpleStream.h>

        char auth[] = "d1d7.............................22e";

        int Slider1;
        int Slider2;
        int Display; // sum of slider 1 and slider 2

        void setup()
        { 
          Serial1.begin(9600);
          Serial.begin(9600);
          Blynk.begin(Serial, auth);
          Blynk.syncVirtual(V1);
          Blynk.syncVirtual(V2);
        }

        void loop()
        {
          Blynk.run();
        }


        BLYNK_WRITE(V1)
        {
          Slider1 = param.asInt();
          Display = Slider1 + Slider2;
          if(Slider2 != 0){                   // just a hack for initial boot when 2nd slider doesn't yet have a value
            Blynk.virtualWrite(V3, Display);
          }  
        }

        BLYNK_WRITE(V2) {
          Slider2 = param.asInt();
          Display = Slider1 + Slider2;
          Blynk.virtualWrite(V3, Display);
        }
1 Like

FYI, please start using this method to format your code as the <Preformatted Text> option doesn’t always display properly. I have already edited most of your past posts, so you can go back and see how it is done. Thanks

Ah, yes… you are using the USB link, thus can’t use the same serial port for serial prints. But if you have a TTL-USB adapter, then you can setup a software serial (or alternate UART) port for it and send diagnostic or information prints to a terminal program on your PC.

EDIT - I see you have already done that with #define BLYNK_PRINT Serial1 :+1: