Nextion page value update (EasyNextionLibrary)

Hi guys, I need some help with my arduino code. I have 2 pages with values. Whenever I change pages, I wan to have the value from before to appear on my display without pressing any button. Eg. in page0 my value in n0.val is 988, when I press a button to change page and go back I want the same value "988" to be in n0.val

I am not sure about my code for the currentpageID
This is my first time using it.
Please help

#include <EasyNextionLibrary.h>

EasyNex myNex(Serial2);

// Debouncing variables
const int incrementButtonPin_1 = 5; // Pin connected to increment button
const int decrementButtonPin_1 = 6; // Pin connected to decrement button
const int incrementButtonPin_2 = 7; // Pin connected to increment button
const int decrementButtonPin_2 = 8; // Pin connected to decrement button

int value1 = 0;
int value2 = 0;
unsigned long pressStartTime = 0;
unsigned long lastUpdateTime = 0;
bool button0Pressed_1 = false;
bool button1Pressed_1= false;
bool button0Pressed_2 = false;
bool button1Pressed_2 = false;
const unsigned long longPressDuration = 1000; // Duration for long press (1 second)
const unsigned long updateInterval = 75; // Interval for updating value1 (75 ms)

void release() {
  button0Pressed_1 = false;
  button1Pressed_1 = false;
  button0Pressed_2 = false;
  button1Pressed_2 = false;
  pressStartTime = millis();
  lastUpdateTime = millis();
}
  
void setup() {
  Serial.begin(9600);
  myNex.begin(9600);
  myNex.writeNum("n0.val", value1); // Initialize number box with value1 
  myNex.writeNum("n1.val", value2); // Initialize number box with value2 
}

void loop() {
  myNex.NextionListen();
  checkLongPress();
}

void trigger0() {
  unsigned long currentTime = millis();
  button0Pressed_1 = true;
  pressStartTime = millis();
  lastUpdateTime = millis();
  if (currentTime - pressStartTime < longPressDuration) {
    value1 += 1; // Short press, decrease value1 by 1
    myNex.writeNum("n0.val", value1);
  }
  button1Pressed_1 = false;
}

void trigger1() {
  release();
}

void trigger2() {
  unsigned long currentTime = millis();
  button1Pressed_1 = true;
  pressStartTime = millis();
  lastUpdateTime = millis();
  value1 = myNex.readNumber("n0.val");
  if (currentTime - pressStartTime < longPressDuration) {
      if (value1 > 0) {
        value1 -= 1; // Short press, decrease value1 by 1 if value1 is above 0
      } else {
        value1 = 0; // Ensure value1 does not go below 0
      }    myNex.writeNum("n0.val", value1);
    }
    button0Pressed_1 = false;
  }

void trigger3() {
  release();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void trigger4() {
  unsigned long currentTime = millis();
  button0Pressed_2 = true;
  pressStartTime = millis();
  lastUpdateTime = millis();
  if (currentTime - pressStartTime < longPressDuration) {
    value2 += 1; // Short press, decrease value2 by 1
    myNex.writeNum("n1.val", value2);
  }
  button1Pressed_2 = false;
}

void trigger5() {
  release();
}

void trigger6() {
  unsigned long currentTime = millis();
  button1Pressed_2 = true;
  pressStartTime = millis();
  lastUpdateTime = millis();
  value2 = myNex.readNumber("n1.val");
  if (currentTime - pressStartTime < longPressDuration) {
      if (value2 > 0) {
        value2 -= 1; // Short press, decrease value2 by 1 if value1 is above 0
      } else {
        value2 = 0; // Ensure value1 does not go below 0
      }    myNex.writeNum("n1.val", value2);
    }
    button0Pressed_2 = false;
  }

void trigger7() {
  release();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void checkLongPress() {
  unsigned long currentTime = millis();

  if (button0Pressed_1) {
    if (currentTime - pressStartTime > longPressDuration) {
      if (currentTime - lastUpdateTime > updateInterval) {
        value1 += 1; // Increase value1 by 1 every 75 ms
        myNex.writeNum("n0.val", value1);
        lastUpdateTime = currentTime;
      }
    }
  } else if (button1Pressed_1) {
    if (currentTime - pressStartTime > longPressDuration) {
      if (currentTime - lastUpdateTime > updateInterval) {
        if (value1 > 0) { 
          value1 -= 1;} // Decrease value1 by 1 every 75 ms
        myNex.writeNum("n0.val", value1);
        lastUpdateTime = currentTime;
      }
    }
  }

    else if (button0Pressed_2) {
    if (currentTime - pressStartTime > longPressDuration) {
      if (currentTime - lastUpdateTime > updateInterval) {
        value2 += 1; // Increase value2 by 1 every 75 ms
        myNex.writeNum("n1.val", value2);
        lastUpdateTime = currentTime;
      }
    }
  } else if (button1Pressed_2) {
    if (currentTime - pressStartTime > longPressDuration) {
      if (currentTime - lastUpdateTime > updateInterval) {
        if (value2 > 0) { 
          value2 -= 1;} // Decrease value2 by 1 every 75 ms
        myNex.writeNum("n1.val", value2);
        lastUpdateTime = currentTime;
      }
    }
  }
}

countspeed.zip (12.4 KB)

This is my code and my HMI.

Please help @cattledog

For the number to retain its value when leaving and returning to a page, the vscope needs to be global.

See this "Cheap Controls" video tutorial.
https://www.youtube.com/watch?v=jXpgeZ7cmRo

I am not sure about my code for the currentpageID

I don't see any code where you are using the library variables for currentPageID and lastCurrentPageID.

 //--------------------------------------- 
     // public variables
    //-----------------------------------------
    
    /* currentPageId: shows the id of the current page shown on Nextion
     * WARNING: At the Preinitialize Event of every page, we must write: 
     * printh 23 02 50 xx , where xx the id of the page in hex 
     * (example: for page0, we write: printh 23 02 50 00 , for page9: printh23 02 50 09, for page10: printh 23 02 50 0A) 
     * Use can call it by writing in the .ino file code:  variable = myObject.currentPageId;
     * 
     * lastCurrentPageId: stores the value of the previous page shown on Nextion
     * No need to write anything in Preinitialize Event on Nextion
     * You can call it by writing in the .ino file code:  variable = myObject.lastCurrentPageId;
    command
     */ 
    int currentPageId;  
    int lastCurrentPageId;

There is a library example code "Change Pages and Send Float Values" which uses the currentPageId and lastCurrentPageId variables.

if I were to set vscope to global only will do right?
And quick question, i am using nextion NX8048K050_011. I have another HMI file. When I upload that file into the nextion, there is an error, saying insufficient space. what can I do to solve it?

I will post in another topic. Please do check it out and see if I could improve in any way

Hi i need help. I don't know how to optimize the space of my nextion nx8048k050_011

Please help

M1.zip (32.8 KB)

M1.zip triggered a virus checker and I could not download it.

I would suspect images and animation of images as the biggest memory users on the Nextion. Perhaps fonts as well if you are using multiples.

@Shan1401 ,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I don't have any images or animation
its just buttons, numberbox and textbox

Sorry

I thought each code have to be done in another topic
Didn't mean it in any other way
Once again i apologize

I got it alrdy.
Its because in my textbox I tend to set the maxl of the textbox to a very long length
After setting in a proper way, i got it right. Thank you.

Now I have another problem. Earlier, in the nextion editor, I set the display to the basic display. And it worked. now when I set it to the enhanced version, it does not worked. When Reset it back to basic version also didn't work.. Could be because of virus as u said?

(I have mentioned this similar problem in another topic before. It seems it occurred again)

The Device needs to be set to the model you are coding for and uploading code to.

What does "it worked" and "it didn't work" actually mean.

I have no idea why there is a virus detected in the zipped HMI file you attached and what it means for the program on the Nextion.

Its like this:
Earlier i set the model to the basic model and connect it to the basic model and upload it.
The display is shown and the arduino run as it should.

When I set the model to the enhanced model, connect to the enhanced model and upload it, the display is shown. But its not working according the code. Nothing in the nextion display is functioning.

I also have no idea abt the virus. Is there any way I can do, to have you see the code without the virus.

This is my first time having this kind of situation. I don't have any other nextion model.

I guess you can not switch models like that. Different models have different features and capabilities, and it would seem that code needs to be written or copied into the editor and compiled when it is set for the model.

I done that before.
I opened a new file for nextion editor and do it step by step.
Same problem.
The shocking part is tht after testing the enhanced version, going back to the basic model. the basic is not working. The arduino is copy and paste. i even try using a different arduino board. initially i was using arduino mega (Serial2) changing to arduino nano every (Serial). Even after changing, same problem

Could it be virus?

I don't know?

My recommendation would be to uninstall the Nextion Editor and start with a clean version. Do not open any existing files with the new editor, but recode them. I would hope the behaviour of the Nextion is better.

None of what you are experiencing has ever happened to me, but I only have one Nextion model.

Thank you very much sir
You have been a great help to me

I see what I can do

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.