Can I index Vx pin's?

I’m struggling with a code ‘effenciency’ issue, I have about 30 on/off inputs. Each is processed through the exact same set of criteria and responded to identically changing property settings. But, the set property commands have these peskey pin references that dont seem to be dynamic. See the current code.

      Blynk.setProperty(V30,"offLabel","Door 5 OPEN"); // warn condition
      Blynk.setProperty(V30,"offBackColor","#FF0000"); // Background color red

The strings are easy. Whats not so easy is how to handle the virtual pin number, Ideally i’d like to do something like this.

      Blynk.setProperty(V[i],propertyLabelString[i],condtionString[i]); // warn condition
      Blynk.setProperty(V[i],propertyBackColorString[i],conditionBackColorString[i]); // Set Background color

If i can do this, it will prevent me from having to replicate 9, 30 level switch/case statements.
The Closest I can come is a big tall switch/case statement with the following.
Just seems wasteful…

void SetPropIdx(int i,String propString1, String propString2)
{ 
  switch(i){
  case 0:
    Blynk.setProperty(V0,propString1,propString2);
    break;
    case 1:
    Blynk.setProperty(V1,propString1,propString2);
    break;
    case 2:  
    Blynk.setProperty(V2,propString1,propString2);
    break;  
    // ...
  }

Thanks in advance!
Jim-

The “V” prefix is actually optional, so you should just be able to do

Blynk.setProperty(i,propertyLabelString[i],condtionString[i]); // warn condition

There’s also a little-known feature called BLYNK_WRITE_DEFAULT() which saves you having lots of callbacks like this…

BLYNK_WRITE_V1()
BLYNK_WRITE_V2()
BLYNK_WRITE_V3()
etc...

There’s a few coding examples in here if you want to take a look…

Pete.

Thanks Pete!
I was making it too hard. Pulled the “Vx” and plugged in “i” and it compiled.
(Nice heads up on the GPIO boards too.)

Jim-

1 Like

In my late night musing on this, I began to wonder how this differentiates between Digital, Analog or Virtual…

These commands only relate to virtual pins.
If you want to change the state of a digital pin then it’s a standard C++ digitalWrite() command and the Blynk library does the rest.
I guess the same for Analogue pins, but I’ve never tried that.

Pete.

Interesting,
This solves my problem though. Seems like all of the apps I’ve ever done use the virtual pins almost exclusively.
Thanks!

1 Like