Code Order

Hi All. First post and a newbie so be gentle please :slight_smile: :slight_smile:

I am in the process of learning coding as well as trying to get to grips with a Nextion display. I have got the basic grasp of what i need to do but when i come to "declare objects",

  • Do you list the objects in page/object order?
  • List the objects by type?
  • It dosent matter?
NexButton b0 = NexButton(2, 1, "b0");  // Button added
NexButton b1 = NexButton(2, 2, "b1");  // Button added
NexButton b2 = NexButton(2, 3, "b2");  // Button added
NexButton b3 = NexButton(2, 4, "b3");  // Button added
NexButton b4 = NexButton(2, 5, "b4");  // Button added
NexButton b5 = NexButton(2, 6, "b5");  // Button added
NexButton b6 = NexButton(2, 7, "b6");  // Button added
NexButton b7 = NexButton(2, 8, "b7");  // Button added
NexButton b8 = NexButton(2, 9, "b8");  // Button added
NexButton b9 = NexButton(2, 10, "b9");  // Button added
NexButton b10 = NexButton(2, 11, "b10");  // Button added
NexButton b1 = NexButton(0, 9, "b1");  // Button added
NexButton b0 = NexButton(0, 1, "b0");  // Button added
NexButton b4 = NexButton(0, 11, "b4");  // Button added
NexDSButton bt0 = NexDSButton(0, 8, "bt0");  // Dual state button added
NexSlider h0 = NexSlider(0, 4, "h0");  // Slider added
NexText t5 = NexText(2, 12, "t5");  // Text box added, so we can read it
NexText t6 = NexText(2, 13, "t6");  // Text box added, so we can read it
NexText t7 = NexText(2, 14, "t7");  // Text box added, so we can read it
NexRadio r0 = NexRadio(2, 4, "r0");  // Radio checkbox added
NexRadio r1 = NexRadio(2, 5, "r1");  // Radio checkbox added

In most cases the declaration order isn’t important, as long as any dependencies are accommodated.
They’re often arranged into meaningful / logical blocks for readability - or on occasion - tidiness!

One observation... when you start ‘numbering’ objects you should start considering arrays of that object. Your code will almost inevitably be shorter and more readable.

One observation... when you start 'numbering' objects you should start considering arrays of that object. Your code will almost inevitably be shorter and more readable.

Sorry, as i said, i am a newbie to programming. Can you explain a bit more or give an example.

Many thanks

I’ll help —- when you fix your initial post with code tags !

Is that better? :slight_smile:

// there are 'clever' ways to expand on this 
// - but since you had sequential single digit button names, this is the easiest

//========================================================
#define BUTTON_TYPE 2
const int NUM_BUTTONS = 12;
// create array of buttons
NexButton myButton[NUM_BUTTONS];

// initialise each button member
for (int index = 0; index < NUM_BUTTONS; index++) {
 char button_name[4] = {"b"}; // button name prefix
// myBad ! // strcat(button_name, index + 0x30); // add the index number as ASCII digit
 strcat(button_name, '0' + index);  // I SHOULD have done this first time !
 myButton[index] = NexButton(BUTTON_TYPE, index+1, button_name);  // Add button 
}
// - to reference these buttons - exacgtly the same as you did before - with a new name
myButton[n] // being the index of the button

//========================================================

// You can extend the same idea over to other objects as needed.
// It's no always the sweetest, but in many cases it is---

// ----------------------------------------------
// Curious - why are you RE-declaring these ?
/*
NexButton b1 = NexButton(0, 9, "b1");  // Button added
NexButton b0 = NexButton(0, 1, "b0");  // Button added
NexButton b4 = NexButton(0, 11, "b4");  // Button added
*/
// ----------------------------------------------
// if there's only one or two of a type - just leave them as is
// you could generate them programmatically, but that's
// beyond the scope of this answer !

NexDSButton bt0 = NexDSButton(0, 8, "bt0");  // Dual state button added
NexSlider h0 = NexSlider(0, 4, "h0");  // Slider added

NexText t5 = NexText(2, 12, "t5");  // Text box added, so we can read it
NexText t6 = NexText(2, 13, "t6");  // Text box added, so we can read it
NexText t7 = NexText(2, 14, "t7");  // Text box added, so we can read it

NexRadio r0 = NexRadio(2, 4, "r0");  // Radio checkbox added
NexRadio r1 = NexRadio(2, 5, "r1");  // Radio checkbox added

// ----------------------------------------------
// Curious - why are you RE-declaring these ?
/*
NexDSButton bt0 = NexDSButton(0, 8, "bt0");  // Dual state button added
NexSlider h0 = NexSlider(0, 4, "h0");  // Slider added
NexText t5 = NexText(2, 12, "t5");  // Text box added, so we can read it
NexText t6 = NexText(2, 13, "t6");  // Text box added, so we can read it
NexText t7 = NexText(2, 14, "t7");  // Text box added, so we can read it
NexRadio r0 = NexRadio(2, 4, "r0");  // Radio checkbox added
NexRadio r1 = NexRadio(2, 5, "r1");  // Radio checkbox added
*/

BTW in your code - the button instances have the a same name as the NexButton class...
NOT a good idea o several levels... note - I usedmyButton

strcat(button_name, index + 0x40); sp. "0x30"

Thanks - oops, finger slip! ('0' not 'A') !

lastchancename:
Thanks - oops, finger slip! ('0' not 'A') !

sp. "('0' not '@')"

It's been a while !
:frowning:

I am trying to do this to iterate through text actually. But tried the button sketch as a starting point. When I try to compile I get the following error:

no matching function for call to 'NexButton::NexButton()'

The only thing I added to the sketch above was:

#include <Nextion.h>

to add the library.

My plan is to build a display with 16 different names and I would like to be able to "for" through each one but having difficulty initialized the array of items.
Any help would be appreciated.
Thanks