Start a new topic

remember a value from editior

I am using a Nextion 3.2 basic display. I have created 3 pages with the third page having a slider. The slider will control an analog value back to my arduino mega. If I compile my sketch and go to page3 I can move the slider and see my led intensity change. That part works as expected. However if I return back to any other page the led intensity will go back to the default value that is set to 50 (slider h0 value). I have made the values global thinking that this would allow me to change pages and have the value stick. Is there some way to link the values that I use back to the main page?


So here is the breakdown of your Nextion HMI design


As a page loads

 - the Page Pre Initialize Event runs the code contained there

 - the existing page is dumped to make room for your page

 - the HMI loads your page as per your HMI design (why it goes back to 50)

 - the Page Post Initialize Event runs the code contained there

 - the main loop begins waiting for

    -) text commands coming in over serial

    -) press events coming in from the screen touch sensor

    -) gpio binding events coming in over a gpio pin

    -) a timer event in your HMI design


So the reason why your Slider value maybe doesn't stick is

    #1 - because a page is not in memory

    #2 - the loading page has been reset to the value at HMI design time


Now a component set to global will hold a value but not the other attributes, and depending on your design could be wiping out your variable in the page resetting to design time.  It isn't as "global" as we would like and global variables work best from the first page page #0. And now with the basic understanding of the HMI design - your strategy to hold the value is?


If your HMI has very few values to remember you can use one of the sys0,sys1,sys2 variables as you know that they are not part of the HMI design that has a value that will be reset.

    -- in the event that causes your slider page to exit (button/hotspot) you could put

           sys2=h0.val

    -- in the loading page Post Initialize Event you could reset the slider value to the sys2 value

           n0.val=sys2


If you have many values that need to be remembered you could send the value back to the MCU to hold until you need it.  Let's say you use a button b3 id=5 on page2 to exit the slider page back to page0

     -- in the press event of b3

           - check Send Component ID (this will send hex 65 02 05 01 FF FF FF)

      -- when your MCU receives this issue a

           get h0.val  (this will send hex 71 xx 00 00 00 FF FF FF - where xx is slider value in hex)

           - now you can assign that to an MCU variable

      -- if you need additional time for the MCU to receive put a delay in your Pressed Event

           delay=10  (will delay for 10ms)

      -- in the released event of b3 is your command to switch pages back to page0

           page page0


So now when your MCU receives the appropriate message, you can respond to it and are able to save variables on the nextion to a variable on your MCU.  So how do we get that back to the Nextion?  There is a command called sendme that will send the current page number when the page loads.  In your first page Pre Initialize Event - put the command

     sendme  (this will send the hex 66 xx FF FF FF - where xx is the current page number in hex)


When you receive a page # where the slider value is need, as you catch the current page message you can send the slider value back to the nextion as you know it is needed with that page.  If the value needs to be placed in the Number Component n0 then you would send

      n0.val=24 (or the value of your slider variable on your MCU)


There are many means of accomplishing this as long as you keep in mind the order of the events when switching pages and make the appropriate allowances for dealing with it.  The value that is going to be wiped out and reset by a page reloading needs to be stored somewhere before it gets wiped out, and needs to be restored after the page has been reloaded ... so what is your strategy.

     

thanks for the response. I tried what you mentioned and it compiled with no errors but the value still does not stick. I did notice in my editor that I have page0,page1,page3. For some reason it does not have page2. I wonder if this could be causing an issue. I doubt it but perhaps a bug in the editor. I might try to rebuild my pages on a fresh build and see what happens. The hardware is great but trying to get it to work is not inuitive for someone with little experience.

Joe

there is a page name and a number beside it. 

Page names start sequential as added.

If you deleted page2 then that is why it is not there but

page0 should have a 0 beside it for the number

page1 should have a 1

page3 should have now have a 2


There is a bit of programming logic that gets stronger over time.

attach your HMI and I'll take a look for you

here is a copy of my file from yesterday prior to changes.

HMI
(3.51 MB)

okay so in page3 your sliders are working

I am taking it that your MCU is retrieving the value and setting the light so

when you leave the page, it isn't there anymore to retrieve.


so what I did - took 5 global variables va0 to va5

in the slider Touch Released event

vaX.val=h0X.val  ... slider value is now in global variable

go to first page ... fine.


in your mcu code   get page3.va0.val  will send back your value that has been set

will start off as 0 when the HMI first loads

you can have your mcu send page3.va0.val=50 to set the default on startup.


Give this a shot and see if it helps.


HMI
(3.51 MB)

thanks I will give it a shot a bit later.

Joe M

Some of my issue may be at my mcu level. Im not using the stable released arduino library im actually using a simple library called nextion-master. I just downloaded the offical library called iteadlib_arduino_nextion-7.0.

I plan on re-writing my code bassed off of this library and see what happens. Thanks again for your help and Ill keep you posted on what I find.

Still no luck. I think I might try a different approach. The value is there but switching screens pushes the value back to default. I wonder if I use the eeprom function on arduino to store the set value from screen 3 and than write the new value saved in eeprom.

I turned on some serial monitors and found that when im on page 3 and I set the slider value to 70 (example) my serial monitor also sees a value of 70. When I use the home hot key button and the page refreshes back to page 0  the same value now reports back as -1 ? If I use my hot key on the nextion touch screen to go back to page 3 my serial reports back a value of 70 again.

Sort of strange but Im sure theres a way to make it work.

 

 

 


 

The updated sample HMI that I posted back retained its value and can be checked using

     get page3.h0.val


This would mean you have to isolate the MCU code that is making the change.

A common error is having a statement that initializes the variable/component inside the main loop or initializing/resetting in the message handler.


For clarity, are you using your original sample hmi, or did you  swap the hmi with the one I posted?





this is what I ended up doing. I changed my arduino sketch to read like this; I dont need the serial print line at the bottom but it was good for troubleshooting.This works as it intended and thanks again for all your help.


 white_led_value= myNextion.getComponentValue ("page3.h0") ;

   analogWrite (white_led , white_led_value );

   Serial.println(white_led_value);

I also added a map function because my nextion hmi file uses a value from 0-100 but the led analog number is 0-255. Im still trying to learn how to write programs so its probably not the best approach but it seems to work.


white_led_value= myNextion.getComponentValue ("page3.h0") ;

   white_led_value = map(white_led_value, 0, 100, 0, 255);

   analogWrite (white_led,white_led_value);

   Serial.println(white_led_value);

Perhaps, had you mentioned the requirement also needed to convert, I might have offered something like

- change your slider .maxval attribute from 100 to 255.

- in the slider Touch Move Event

    white.val=h0.val*20/51


This would have allowed the Nextion to do the calculation and not require the Arduino to have to map.

white_led_value= myNextion.getComponentValue ("page3.h0") ;  

   analogWrite (white_led,white_led_value);

   Serial.println(white_led_value);


Enjoy learning to program it is fun.

   Don't fix what is not broken.

   Document your code using the double-slash // for comments

      - everything to the left of // is code, everything to the right is comments

      - consider it like note-to-self (or others should you share your code)

      - in a month when you revisit your program, you will not remember what or the whys





Login or Signup to post a comment