Unofficial Nextion/TJC User Forum

Problems using dual-state buttons and Number

Hi everyone! I’m making an interface with some dual state buttons and number boxes to set parameters.

When I put a number in the object with the keyboard, the state in the dual state buttons is lost :frowning:
I can not find a way to fix it, i would know if somebody had this problem too and how did you solve it?

Thanks and greetings from Mexico :mexico:

image

Which object? n11? What code do you have in n11, ParoF, EstadoF, and InicioF?

Thanks for reply me my friend.

I’m just sending component Id when I released in all the objects.
But I was testing and does the same with checkboxes

For example:
First I press EstadoF and it shows like ON (that’s fine)

Then I set the speed in n11, the keyboard is open I put the value and click in OK

But when it returns to the page, the EstadoF state returns to OFF :frowning:

And the same occurs with checkboxes :thinking:
I guess is a keyboard problem but I don’t know if there is a solution, a parameter to change or something to fix it :frowning:

Also when I press X in keyboard. This obj (X) have the instruction loadpageid.val, it shows and refresh the previous page. The refresh command loads the page by default on power on

Is there any other alternative to back to the main page without refreshing this?

Need to see the actual code in the Pre and PostInitialize Events for the page as well as the Touch Press and Touch Release for n1. Which keyboard are you using? What is loadpageid.val?

Would an up/down button be an alternative for you I have implemented this in my inverter remote control using only the nextion screen.

Welcome @osvaldointer1!

I guess the issue is that your dual state switch is a local component (not global). That means that it will loose its state whenever you leave or reload the page. Opening any of the built-in keyboards does exactly that: switch to the keyboard page, return to the previous page afterwards. It‘s for that reason that the components you want to edit with the keyboard must be global (otherwise the value wouldn‘t be accessible when changing pages).

This means that the easy fix is to make the dual state switch global, too. However, that approach may sooner or later lead to RAM issues. Global components eat up a lot of RAM since all their properties are kept in RAM all the time (whereas local components are only kept in RAM while the page is active).
If you run out of RAM you can significantly reduce your RAM usage with a little more effort: Need to keep page load in preinitlaization while values update from arduino - #7 by Max

Kind regards,
Max

1 Like

Thanks a lot Max!
That was the solution, just change to global and now is working!
I going to check it about the RAM usage if I need, thanks for the information!

What Max said. The supplied keyboards are just custom pages. Loading the keyboard will remove the state and value of all local values on your current page from memory and these will be lost when return to your original page. You could always switch the components from Local to Global but globals are resource hogs - RAM is a precious resource on the Nextions.

You might be able to get creative though. For example, declare a global variable “buttonState=0”. This variable will consume 4 bytes of RAM but allow you to store the state of 32 components between page transitions as binary in the 32 available bit positions.

Do something like this, dedicate the lowest bit in buttonState as a flag to indicate a page return operation that signals a newly loaded page to pre-populate any button/checkbox states with the values in buttonState. For example, move all checkboxes/radio buttons/dual state components to the top of each page’s component listing so they occupy components b[1]…b[x] then in the Page Exit code, add the following:
buttonState=0
for(sys0=1;sys0<=10;sys0++) {
buttonState+=b[sys0].val
buttonState=buttonState<<1
}
// This stores ten component states b[1]-b[10] into buttonState global as 10 individual bits followed a 0 value bit (used as flag)
The Pre-Init code on your keyboard page would include:
if(buttonState>1) { // check if buttonStates were saved
buttonState=buttonState&1 // add return flag
}
// This adds a flag to the lowest bit position in buttonState to indicate that previous button states were saved then the keyboard page was loaded

On the Pre-Init code for the pages that utilize the keyboard and contain 2 state components (checkboxes, etc), add the following code:
if(buttonState%2==1) { //check flag to indicate KB return
for(sys0=1;sys0<=10;sys0++) {
buttonState=buttonState>>1
sys1=buttonState%2 //get button state for component
if(sys1==1) { //check if non-zero
b[sys0].val=1
}
}
buttonState=0 //clear the saved component states
}

The above will allow pages to automatically detect a return from the keyboard and return up to 10 dual state components to their previous states. This would only require a single global variable (4 bytes of RAM).

You could also extend it restore the states/values of number and text fields. I’d might recommend using the undocumented sya0 and sya1 global variables for this purpose. They’re already global and consuming RAM but not being used. If you have to restore text fields then I’d get creative and maybe use some short global that has multiple uses and an additional flag in buttonStates to signal text fields needing to be restored. You could also pass the text fields to the keyboard page inside the keyboard text input global component - appending a \r (CR/LF) to each field. If the keyboard is set to text wrapping then these passed text fields would not be visible to the user during keyboard entry. Set a flag on the keyboard page and have the original page check and populate the text fields with previous values. I’ve been using a custom keyboard setup for sometime so I can’t remember all the specifics of the included keyboard examples but some version of the above should be a viable solution with some light customization and consuming limited overhead.


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