How to Return Values from BLYNK_WRITE

I would like to Return the value from a widget (in this case, a slider) and use it in other areas of my arduino code… Something like below:

//Slider Setting on Virtual Pin 2
BLYNK_WRITE(2) {
  int virtualsliderSetting = param.asInt();
  return virtualsliderSetting;
}

int finalValue = virtualsliderSetting + 10; 

But I keep getting an error like this:

return-statement with a value, in function returning 'void' [-fpermissive]

Am I attempting the impossible?

Z

You could :

BLYNK_WRITE(2) {
  finalValue = param.asInt() + 10;
}

int finalValue;

Cool. Why does that work but the other way doesn’t?

Because BLYNK_WRITE is void. And doesn’t have a return value.

2 Likes

Okay, I lied… I mean, I didn’t really get it to work. Let’s say virtual pin 2 = 10.

If I type:

BLYNK_WRITE(2) {
  finalValue = param.asInt() + 10;
}

int finalValue;

I would expect that int finalValue = 20, but what I am getting instead is an error that ‘finalValue’ was not declared in this scope… Any Thoughts?

Arduino: 1.6.4 (Linux), Board: “Arduino Yún”

sketch_oct28c.ino: In function 'void BlynkWidgetWrite22(BlynkReq&, const BlynkParam&)':
sketch_oct28c:37: error: 'finalValue' was not declared in this scope
sketch_oct28c.ino: In function 'void loop()':
sketch_oct28c:81: error: a function-definition is not allowed here before '{' token
sketch_oct28c:87: error: a function-definition is not allowed here before '{' token
sketch_oct28c:100: error: a function-definition is not allowed here before '{' token
sketch_oct28c:105: error: a function-definition is not allowed here before '{' token
sketch_oct28c:107: error: expected '}' at end of input
Multiple libraries were found for "BlynkSimpleYun.h"
 Used: /root/Arduino/libraries/blynk-library-0.3.0
 Not used: /home/zee/Desktop/arduino-1.6.4/libraries/blynk-library-0.3.0
Multiple libraries were found for "SparkFunHTU21D.h"
 Used: /root/Arduino/libraries/SparkFun_HTU21D_Breakout_Arduino_Library-master
 Not used: /home/zee/Desktop/arduino-1.6.4/libraries/SparkFun_HTU21D_Breakout_Arduino_Library-master
'finalValue' was not declared in this scope

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

I define global variables, then set their value with BLYNK_WRITE. It works fine.
Perhaps you just need to move the definition up.

1 Like

What electron says is correct. You have to make the var global to be able to use it everywhere. It probably has disadvantages (like more memory use), but it’s the only way to make it happen.

Declare you var’s before the void setup() and everything will work :slight_smile:

Yesssssss.

Annnd it’s working. Thank you, and why the hell does it have to be so confusing?

1 Like

This is how the arduino programming works. Mostly because its a deriative of C and actually its quit logical, but there is reasoning behind all this madness. Variables can be used locally in a function or be global. You can choose what you want it to be. This gives you flexibility in how to use them and organize your code. It also allows for better memory management, which is quit important if you work with microcontrollers. Since they dont have a lot of that :slight_smile:

1 Like