just a quick question
I am trying to change the state (image) of the duel state button
The button changes as expected pic0 (off image) pic1 (on image) after the touch command. However when I leave the page and return I lose the on state image
What is the correct method of retaining its state
hserout -high speed serial from picaxe
hserout 0,("bt0.val=1"")0xff,0xff,0xff as a refresh via an if statement
nextion code in press event
any help would be more than welcome
love the product
a dual-state button can have two pictures associated with the button
- setting .sta to image gives the options for .pic0 and .pic1
- setting .pic0 in Editor and select image for 0 state
- setting .pic1 in Editor and select image for 1 state
In operation, press will turn on or off depending on previous state
- this is handled automatically and can be read by bt0.val
Now there is no Nextion side code required
- such is merely getting in your way. Remove.
The issue of changing pages and expecting all to be the same on re-entry
- this you need to understand
Each Nextion page in your HMI design is like a mini program
- when you change to an alternate page
the Preinitialization Event from new loading page is run
then the old page is dumped from memory to make room for new
then the new loading page is loaded into memory
(this is loaded static as per the way you designed it in HMI design)
then the Postinitialization Event from new loading page is run
When you want something to persist across page changes
- you must do with a global variable Nextion side
handle storing to temp location in Preinitialization Event
handle restoring from temp location in PostInitialization Event
- or you can have your MCU track which page user is on
sending bt0.val=1ÿÿÿ or bt0.val=0ÿÿÿ depending on actual MCU side value
- or you can even set .vscope of your dual-state button to global
Dual-states will present a challenge MCU side as
- is Nextion in control and therefore state of bt0.val is authority
- then on page change, your MCU must adjust to its value
(an example of Nextion in control is
bt0 toggles high/low of GPIO pins on enhanced models)
- if your MCU is in control, then regardless of the state of bt0.val
- your MCU must make change to bt0 to reflect actual state.
Sometimes dual-state maybe the wrong component for task
Regardless, you must choose for your design of which controls
and what method you will use for it to persist - then implement.
I am less certain that you are attempting to dynamically change
the picture set for the 0/1 state of the dual-state
But if you wanted to
bt0.pic0=4ÿÿÿ and bt0.pic1=5ÿÿÿ would select new pictures.
I don't know your project, nor am I its programmer
- so I didn't select if dual-state is good route or not.
I presented your with the facts about such things, and you must evaluate and decide.
- there are many cases where dual-state is the correct choice for a task
- but there may also be more coding considerations and handling.
Much attention to the details.
The debug simulator takes a valid nextion instruction
- the only difference between MCU sending and Instruction Input Area
is that Instruction Input Area automatically adds the trailing ÿÿÿ termination.
Details are important.
bt0.pic assignments will fail - does a dual-state have a green .pic attribute?
if you removed bt0 component, is there a bt0 component to alter attributes?
there is only a trailing ÿÿÿ as terminator (0xFFs), never nulls of 0xFF.
Not paying attention to the details is where errors creep in.
I can't help you on the formatting of your MCU side pixaxe coding,
- too many languages, too many different MCUs, too many compilers.
I can tell what bytes you need to be pushing out. You must push, but exactly.
but perhaps the easiest way to persist the .val of the dual-state between pages
was to set the dual-state's .vscope to global
if either the MCU or the Nextion resets, they may not be in sync
but I didn't say it was a wrong choice - that is for you to evaluate.
Always good when it works, and happy coding ... =)