Start a new topic

global Variables

In one of the project I am using 7" nextion display and mega 2560.

Previously I was not using Nextion Display on the same project.

Every thing is working fine.

following are my findings

With Nextion display

1) Main program memory on arduino side has reduced considerably from 100k Bytes to 17 K bytes

2) Gui programming is fastest

3) Global variable requirement on arduino side has increased from 1000 to 2000 Bytes.

Is there any way to reduce or decrease the requirement  of global variables on Arduino side so that I can use 328P instead of 2560?


So please break down what is chewing up the extra ram

Not all variables need be global on Nextion, or global scope on MCU

Those needing to be triggered by NexTouch::iterate - maybe so

but those only using setValue, getValue can be local scope in function


Was able to free only 44 bytes.

It looks like "NexTouch *nex_listen_list[]" takes up lot of space!

is there any workaround for it? 

or go for larger MCU

go page based?  page based preinitialize event sendme

nex_listen_list[] only needs to hold relevant objects

 -  those that can trigger an 0x65

As each page changes, this list can change to just page based

page3.b2 button can not be triggered when user is on page1

but your nex_listen_list[] is probably holding entire HMI worth.

Yes I have two pages and there is only one nex_listen_list[].

Please explain "-those that can trigger an 0X65"

Following are my page wise separated lists


NexTouch *nex_listen_list2[] = 








NexTouch *nex_listen_list1[]=


   &tempareture,&humidity, &light,&up,&down,&up_key, &down_key,







Should I declare them in separate functions? I really do not  how to go about it.

In your HMI file

 - component's Touch Press has checkbox Send Component ID

   - if this is checked 0x65 0xAA 0xBB 0x01 0xFF 0xFF 0xFF is sent

     (where 0xAA is the page number, and 0xBB is the .id attribute)

 - component's Touch Release has checkbox Send Component ID

   - if this is checked 0x65 0xAA 0xBB 0x00 0xFF 0xFF 0xFF is sent

     (where 0xAA is the page number, and 0xBB is the .id attribute)

On your MCU side code your system maybe catches these

  - to catch these events the matching .attachPush or .attachPop has

    to be defined properly and set up.

  - if a component does not have an .attachPush or .attachPop then

    there is no need for it to be in the nex_listen_list as you haven't

    defined the event to be passed to anything.

nexLoop() listens for these 0x65 0xAA 0xBB 0x0? 0xFF 0xFF 0xFF

    (check NexHardware.cpp) and when it comes across a 0x65 event

    it calls iterate to go through your nex_listen_list[] to find an AA BB match

    and if a match is found, checks if you setup the .attachPush/.attachPop

    for the incoming 0x0? - if so, then it triggers your function. push or pop.

nexLoop() does not come configured to automatically also catch 0x66 events

    (see the Nextion Instruction Set :: Nextion Return Data)


Now I am going to go out on a limb and say that list2 is to set RTC values

  - but from a coding standpoint only the ok button needs to trigger an MCU

    side event after the user has committed that the values on the screen are

    the values they wish to commit to the RTC.  But I haven't seen your design.

IF the 0x65 event is not declared and used MCU side, then there is no need

  for the checkbox Send Component ID to be checked, or for the &component

  to be in the nex_listen_list[]

Next, if the only "commit" component is like the &rtc_ok_button, then it is the

  only component needed to be in the nex_listen_list (as opposed to &hr_set...)

void callback_for_rtc_ok(void *ptr) {

   NexNumber hour_set = NexNumber(.....);

   NexNumber minute_set = NexNumber(.....);

   NexNumber day_set = NexNumber(.....);

   NexNumber month_set = NexNumber(.....);

   NexNumber year_set = NexNumber(.....);

   uint32_t hr, mn, day, month, year;






and in setup() if the Release Event's Send Component ID checked


In this manner, your variables are declared local and available inside

the routines that they are needed, and nex_listen_list is not clogged.

But sendme in PreIntialize Event of your HMI pages and setting up

the 0x66 page event requires you to do some more advanced coding.

Thank you very much .

That clears most of my points.

Thanks for your " Excellent" and "BIG" and "timely"  support

Hi again,

attaching my HMI file ,

Please tell me is it worth trying to fit the code in UNO .

It is working perfectly well with mega2560

(3.66 MB)

existing ino needed to see about shortening

- this is out of ordinary, debug is not what we provide

 if it's worth to adopt for an UNO, that's up to your own preferences. That's something you must answer by yourself ...

    - own skills

    - to invest time

    - overall estimated savings

    - ...

Existing HMI would be used on 7" display

regardless of if MCU used is Mega or UNO.

(It is the MCU code that needs shrinking)


My Opinion is this:

 - if working "perfectly" on Mega2560 - you have a product

Reworking it for an UNO may reduce costs by a bit

 - so you must think how many units is realistic?

   is this high enough for new costs to rework into UNO

   - if so, then maybe consider UNO rework for $8/unit.

However ....

   an UNO product will introduce new limits.

   ... Mega2560 offers an ability to offer upgraded versions

   if UNO is packed full, do you have same ability to upgrade?

attachPop "function :- callback_for_rtc_ok(void*ptr)" not getting called.

Following is my simple code that you have suggested.

where am I going wrong?

Send component ID check box checked for " rtc_ok_button"

#include "Time.h"

#include "DS1307RTC.h"

#include "Wire.h"

#include "Nextion.h"




NexButton rtc_ok_button = NexButton(1,12,"rtc_ok_button");


NexTouch *nex_listen_list1[]=








void callback_for_rtc_ok(void*ptr) {

    //Define nextion rtc variables for rtc setting

   NexNumber hour_set = NexNumber(1,20,"hour_set");

   NexNumber minute_set = NexNumber(1,23,"minute_set");

   NexNumber day_set = NexNumber(1,28,"day_set");

   NexNumber month_set = NexNumber(1,29,"month_set");

   NexNumber year_set = NexNumber(1,30,"year_set");

   uint32_t hour_rtc, minute_rtc, sec_rtc, date_rtc, month_rtc, year_rtc,set_rtc;








   time_t tSet = now();

   RTC.set(tSet); // set the RTC and the system time to the received value




void setup(void)










      setSyncProvider(RTC.get); // the function to sync the time from the RTC




void loop(void)





 // read current time







How certain are you that the callback_for_rtc_ok function is not being called?  Put a Serial.println in to send to your Serial Monitor and be sure if it is or if it is not being called.

Issue could be that ok has changed pages on Nextion side from button HMI side.

First tell me if mcu side is actually triggering

I will debug it and see what happens

in the mean  time elaborate on"Issue could be that ok has changed pages on Nextion side from button HMI side."

  in my HMI rtc_ok_button is on second page. 

but now as suggested I have checked "send component id"box so it will send the page number.

Is there any documentation other than library itself or nextion instruction set? 

Documentation also includes knowledge in forum.

Nextion is driven by STM/GD MCU on back side PCB.

Actually I was assuming page command in rtc_ok_button.

You can disregard theory page changes before received.

now your statement

attachPop "function :- callback_for_rtc_ok(void*ptr)" not getting called.

I assume it really is being called

  - Place Serial.println(' -- Callback function called');  first line inside function

  - Your code may not be doing as you wish. but I think the function is being called

Login or Signup to post a comment