The best approximation of a subroutine (or function) in Nextion is the hotspot component. Put it somewhere in a corner and hide it using the vis command. You can call its press/release event code by using the click command.
Note that you cannot have global functions/code. If you need to execute the same code on different pages your only option (unfortunately) is to copy it over.
I think this is documented somewhere on the nextion site (editor guide or sunday blog post), too.
Search for my post on using the reparse mode, the way I went was to put code into timers and enable them , then disable them at the end of the code so they became functions.
The main advantage of the hotspot over any other clickable component is that it does not take up any precious bytes of RAM. Buttons have various properties like font, background, state, etc that are completely useless when used as function.
The click action already does what you‘re asking for. It executes the event code of a given component, then Nextion continues with the execution of the code after the click event.
Example:
Component hotspot1:
prints "b",0
Component button1:
prints "a",0
click hotspot1,0
prints "c",0
Pressing button1 will write the characters abc to the serial port. c is not skipped and it doesn‘t take any additional actions to get it printed. Also note that you don‘t need additional click commands to return from the called ‘function’.
You learnt me some things I didn’t know. Well done
I will modify my code soon tomorrow morning !
Have a nice Sunday
RovDan
1 Like
This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).