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?
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
2)in the Timer object
3) In the “On” button
4)In the “Off” button
Hi and welcome @Maag,
The somewhat confusing thing about “global” components on Nextion is that they’re not “fully” global. Actually, only their properties (
.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!
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.