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

Reading virtual Pins programatically

Is there a way to read virtual pins programatically rather than building one function for every virtual pin I wantt to read? i.e.
BLYNK_WRITE(Vx) { int value = param.asInt();} //where “value” is a global variable?
BLYNK_WRITE(Vy) {int value = param.asInt();} …

I’d like to be able to read in a for-loop a number of pins. Any ideas?

There is a function called BLYNK_WRITE_DEFAULT() which will trigger when ANY virtual pin value changes on the server.
The request.pin command can be used to to tell you which pin has changed, and param.asInt will give the value of that pin, like this…

BLYNK_WRITE_DEFAULT()
{
  int widget_pin = request.pin;      // Which virtual pin triggered this BLYNK_WRITE_DEFAULT callback?
  int widget_value = param.asInt();  // Get the value from the virtual pin (O = off, 1 = on)
} 

As with BLYNK_WRITE(vPin), BLYNK_WRITE_DEFAULT() the callback is only triggered when the value stored against the virtual pin changes, or a Blynk.syncVirtual(vPin) command is used.

The “V” at the beginning of the virtual pin designation is optional in Blynk.syncVirtual and Blynk.virtualWrite, so you can do a for loop like this…

  for(int loop=0;loop<=7;loop++) 
  {
    Blynk.syncVirtual(loop); // This forces the BLYNK_WRITE_DEFAULT callback function to trigger for each virtual pin in turn 
  }

These code snippets are taken from the sketch in this post…

Pete.

Pete!
Your anwser helps me a great deal! Thanks!!

1 Like