Synthesize Multiple Virtual Pin Variables in Separate Function

This code is Arduino UNO with BLE on Samsung S9 with Blynk library 0.6.1
Is it possible to take the readouts of two different Virtual pins and have them combine to create a new variable? Usually I will get an error code (shown after the code)

#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
SoftwareSerial SerialBLE(10, 11); // RX, TX

void setup() 
{
  Serial.begin(9600);
  SerialBLE.begin(9600);//goes through provided I connect to Blynk first
  Blynk.begin(SerialBLE, auth);
}

void loop() 
{
  Blynk.run();
  trasnmit(num1, num2);
}

void transmit(int x, int y)
{
int m;
m = x * y;
Serial.println(m);
}


BLYNK_WRITE(V0) 
  {
    int num1 = param.asInt();
  }

BLYNK_WRITE(V10) 
  {
    int num2 = param.asInt();
  }

and below is the error code

exit status 1
'num1' was not declared in this scope

If you want to use the values outside of the ```BLYNK_WRITE(Vpin)`` function you need to declare them as a global variable.

You also want to keep the void loop() clean, and not run other functions inside it.

if I declare them as blank global variables:
int num1;
then the value associated with them will be 0. How do I make its value be taken from pin v0

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

You declare them as global and when the BLYNK_WRITE(Vpin) Function runs it will update its value.

1 Like

Declare int num1 as a global variable.

Remove the declaration from the BLYNK_WRITE function, so it looks like this:

BLYNK_WRITE(V0) 
  {
    num1 = param.asInt();
  }

Pete.

1 Like

Thanks Pete and Toro, works correctly now!

1 Like