Start a new topic

duel state button picaxe

 

Hi All

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

Trying

hserout -high speed serial from picaxe

picaxe code

hserout 0,("bt0.val=1"")0xff,0xff,0xff as a refresh via an if statement

nextion code in press event

if(bt0.val==1)
{
  get "bt0:pic1"
}else
{
  get "bt0:pic0"
}
code_c

any help would be more than welcome

love the product

regards

john
apoligise for putting it in wrong forum

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.

Hi patrick
thank you for your reply
I have on the nextion side when the duel state btn pressed send print "MM"
the picaxe picks up these 2 bytes and then sets the relivent picaxe pin to 0 or 1 (outpinsB)
that is then used later on dependent on conditions to high if 1 or maintains low if 0
I see your point about pre and post Initialization and will investigate that further
As you feel that the duel state funtion may not be the best route would using a hotspot and the
2 images be better
Another point what is the correct form with in the debug simulator being trying
"bt0.pic 1=8""0x00,0x00,0x00 gives parse variable incorrect
"bt0.pic 1=""8,0x00,0x00,0x00 as above
baffling me this one
regards
john

 

Hi Patrick
have stuck the following in the post inisalising
th.val=th.val
cf.val=cf.val
tde.val=tde.val
thu.val=thu.val
sda.val=sda.val
th,ect renamed btn
and seems to work in the simulator flipping between 2 diffrent pages so time for a download and see
once again thanks for the help
regard
john

 

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.

Hi Patrick
programed the display with the code from my last post
usinng the post inalisation as sugested
and changing the vscope to global
works a dream
set the time date, set points and program slot to run (auto or manual) in menu page 0
run slot turn on or off equipment in slot nip out back to slot0 page 0
alter value back to run slot equipment preselected displayed the same as when it left
works a dream
many thanks can sleep know
until the next time
regards
john

 

Always good when it works, and happy coding ... =)

Login or Signup to post a comment