Unofficial Nextion/TJC User Forum

Sub routines are possible with Nextion?

Hi,

In order to avoid to repeat the same code in a page, is it possible to create a sub routine in which I go every time.

Example with basic code :

For x.= 1 to 100
gosub Routine


Next

Routine:
a=a+1
Return

Many thanks for your tips.
RovDan

Hi @RovDan,

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.

Hope this helps!
Max

1 Like

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.

1 Like

Thanks for your reply Max

Actually, I did the same way, using a Button which was not visible (vis, myobject,0).

It’s not a problème. Every Time I want to do my routine, I send click mybutton,1. It works fine

However my only problèm is, at the end of the subroutine, to RESUME my main routine, where I left it.

Have you a tip for that ?

Best regards

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’.

Kind regards,
Max

2 Likes

One more time, many thanks Max for your reply.

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).