Translating data from Text Input Widgets into an Array

Hi all,

I’m wondering if anyone knows how to translate data from 4 x Text Widgets into an array which subsequently selects a matched value from the array.
At the moment I’ve able to do it with the values declared in the code but failed in the array seeing the values entered in the Text Widget… my working code looks something like this…

int accessIDcount = 4;
int accessIDs[] = {1234, 2345, 3456, 4567};

.
.
.
.
for (int k = 0; k < accessIDcount; k++){ 
         Serial.println(accessIDs[k]);
         if (a == accessIDs[k]){
           knownAccessID = true;
           //Serial.println(knownAccessID);
         }
      }

Show us the code for your BLYNK_WRITE function for one of the text widgets.

Pete.

Thanks @PeteKnight, I’ve tried all that I know to get this working… any ideas will be much appreciated.

BLYNK_WRITE(V20){String accessID1 = param.asStr();}

Then I was trying things like this to declare the array,

int IDs[] = {V20, V21, V22, V23};

String IDs[] = {accessID1, accessID2, accessID3, accessID4)

@GG07 aren’t you confusing strings and ints?

1 Like

Yes @Costas I guess because the value from the text input comes in as a string. I’ve tried a number of combinations to get the array working such as converting string to int using .toInt()

I think there’s potentially two traps that you’re falling in to here…

  1. As @Costas said, you’re taking string values and treating them as integers in your comparison, so they’ll never evaluate as a match.

  2. You might be getting caught out with the scope of your variables. String accessID1 = param.asStr declares accessID1 as being local to that BLYNK_WRITE function, so when you try to reference it outside of the function you’ll either get a compile error saying that it wasn’t declared in this scope, or if you declare it as a global variable then you’re effectively re-declaring the variable within your BLYNK_WRITE function.

Pete.

I’ve tried declaring String accessIDs; globally but for some reason the the Blynk Write data from the text input widget isn’t recognised in the array initialisation.

Post your full code.

Pete.

The code relating to this issue is gone. I’ve reverted back to what is working and that is declaring the values as mentioned in my first post… I just thought if anyone knew how to grab the data from the text input string and have recognised in the array initialisation it would be very helpful to enter that value via the app.

Why not save it into a json?

Are you able to give me an example please @ldb?

If all you’re trying to do is store the BLYNK_WRITE parameter in a global array it may look like this … string or integer, take your pick:

String accessIDstr[4];
int    accessIDint[4];
BLYNK_WRITE(V20) {
   accessIDstr[0] = param.asString();
   accessIDint[0] = param.asInt();
}
BLYNK_WRITE(V21) {
   accessIDstr[1] = param.asString();
   accessIDint[1] = param.asInt();
}
BLYNK_WRITE(V22) {
   accessIDstr[2] = param.asString();
   accessIDint[2] = param.asInt();
}
BLYNK_WRITE(V23) {
   accessIDstr[3] = param.asString();
   accessIDint[3] = param.asInt();
}

Joe

2 Likes

Thanks very much for assisting @wickedbeernut. I will give this a go and let you know.

That worked perfectly @wickedbeernut. Thanks again for the advice. Here’s basically everything together for anyone else needing it in the future.

//global declaration
int accessIDcount = 4;
String accessIDs[4];

BLYNK_CONNECTED() {Blynk.syncVirtual(V20, V21, V22, V23);}

BLYNK_WRITE(V20){accessIDs[0] = param.asStr();}
BLYNK_WRITE(V21){accessIDs[1] = param.asStr();} 
BLYNK_WRITE(V22){accessIDs[2] = param.asStr();} 
BLYNK_WRITE(V23){accessIDs[3] = param.asStr();} 
.
.
.
void checkArray(){
    for (int k = 0; k < accessIDcount; k++){ 
             Serial.println(accessIDs[k]);
              if (a == accessIDs[k]){
              knownAccessID = true;
              //Serial.println(knownAccessID);
             }
    }
}
void setup(){

timer.setInterval(500L, checkArray);

}