Global Timer stops on page change

Hello,
I’m new with the Nextion display, and I’m try to use it in my Arduino projects to bring more possibility.

I’m doing some experiment to understand the better way to interact with the display. the scope is to understand if use it as “display” of the information coming from Arduino, or thanks to it’s programming features, to leave the code of the graphics change on the display itself.

I have created a simple sketch of : 2 pages, one timer, on/off buttons and some text to show min, sec e msec. The Timer is declared as “Global” and everything is on page 0, when I move o page 1 (that is empty) the time freeze and continue the count when come back on page 0.

It’s correct? If no which solution, to obtain that the timer continue the count also on page change?

Post your code. :grinning:

Sure elf, here is it:
1)in the Program.s
"
int timer_value=0, msec_value=0, sec_value=0, min_value=0, n_sec=0, n_msec=0
int sensorLimitType=0
page 0
"
2)in the Timer object
"
timer_value=timer_value+tm0.tim
msec_value=timer_value
sec_value=msec_value/1000
n_msec=sec_value1000
msec_value=msec_value-n_msec
min_value=sec_value/60
n_sec=min_value
60
sec_value=sec_value-n_sec
covx min_value,t4.txt,2,0
covx sec_value,t6.txt,2,0
covx msec_value,t8.txt,3,0
"
3) In the “On” button
"
tm0.en=1
timer_value=0
msec_value=0
sec_value=0
"
4)In the “Off” button
"
tm0.en=0
"

Hi and welcome @Maag,

The somewhat confusing thing about “global” components on Nextion is that they’re not “fully” global. Actually, only their properties (.val, .tim, …) are global - and that’s it. Neither their code, nor their appearance are global. In the case of a timer that means that it stops running when leaving the page.

The unfortunate consequence of this concept is that Nextion UIs tend to contain loads of duplicated code and components for all the stuff that’s required on multiple pages. In your case one solution could be to make the variables holding the time global (such that they’re not affected by page changes), and have a timer object on every page that increments them.
That would still introduce some jitter during the page changes /where one timer stops and the next one hasn’t started yet). If that’s not good enough you might want to generate a clock/time signal on your MCU and send it to Nextion. Could be something like sec_value++once every second.

Hope this helps!
Max

2 Likes

In general, in my opinion, scripts performed by the display itself should be left only as a last resort, when there is no other option. It is better to perform all actions with an external MCU, leaving the display only with the functions of displaying and responding to user actions. This will give you better control over everything that happens and will allow you to quite easily change display models if necessary, without being tied to a specific manufacturer or specific series of displays.
An exception, perhaps, may be the case when there is no external MCU and all logic is performed only by the display.

3 Likes