How to index objects on a page

Hello.
I have five numeric display boxes on a page, and I want to put values into them without calling them specifically. Can I index them? And if I do index them, do I use their name or the object id?

For example, suppose I have display boxes, n0, n1, n2, n3, n4, n5, and want to populate the five displays like this…

for(k=1;k<6;k++)
{
n[k].val=k
}

I would assume that the variable “k” would take the place of the object id. But this does not work. I get an invalid variable error. I have tried making them all global. And I have tried putting the page number in there as well (page0.n[k].val, and page[0].n[k].val). Same errors all the way around.

Any help would be appreciated.

AKG

Two indexed arrays exist in Nextion firmware for this purpose, the p[] and the b[] array. Each component can thus be addressed by
p[pageId].b[objectId].attribute=…

Thank you. So buttons and pages can be indexed, nothing else can. Well, I could use buttons for the numeric displays.

Thanks again

Let me ask a follow up question. How would I learn this information without asking this forum? Is there a manual available, aside from the instruction set?

You‘re confusing two things. The b[] array gives access to all components on that page. The name of the array is not related to the name of the components, like buttons f.ex. Remember, you can rename your buttons to anything you like. Instead of b0, b1, b2, … they could be called return, enter, ok, blubblub, … And you could still access them over this array just like you can for numbers, dual state buttons and all the other components. There‘s no exception.

The instruction set covers I‘d say 90% of the knowledge. Nextion has some additional blogposts but really most of the stuff you need to know is on the instruction set page. Yes, these arrays are mentioned and explained there, too.

Kind regards,
Max

That is very valuable. Thanks so much.

To avoid this confusion, I had written “each component”, regarding the b[] array. “Each component” was NOT intended to be “buttons only”. Sorry if that hasn’t been clear.

No worries. It’s my mistake. I’m not used to this level of programming. I just saw code that said p[i].b[k].txt=“xxxx”, and I made the assumption that “b” referred to buttons.

Hi,

I tried the index function with 2 buttons and a textbox. It works fine.
I set b[1].txt=“Test1” (button)
b[12].txt=“Test2” ( button)
b[15].txt=“Test100” (textbox)
It’s convenient. However, you can’t manage the Id (the index) of each object. For that reason, you have to create all your array objects immediatly to be sure to have a continue index in order to realize a for next loop.
Moreover, the b name is not very explicit.

I am right ?

Best regards

RovDan

In the Editor toolbar, there are options to arrange the object order and with it, the object ids. And no, the b name is not explicit, but most people are already happy and satisfied with that indirect addressing.

From the Nextion Editor guide:

The Component Layout Toolbar
For Renumbering components: Bring Top (Arrow Up) will take the selected component(s) and renumber to the highest .id on the page. Bring Bottom (Arrow Down) will take the selected component(s) and renumber to the lowest .id starting at 1 (page component is always 0) on the page.

How would I access these arrays from arduino?

p[x].b[i].val=123 ???

Nevermind, I got it without a library (I did have to add ‘Serial2.begin(115200);’ for the data to be sent from the mega2560:

uint8_t numberBoxes[6] = {1, 2, 3, 10, 11, 12};
  for (uint8_t x = 0; x <= 5; x ++) {
    String objectid = String(String("p[0].b[") + String(numberBoxes[x]) + String("].val=") + String(values[x]));
    Serial2.print(objectid);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);

Now, I am off to find how to read the values from the Nextion without a library.

Found it: “get n0.val”