Problem using millis() with a Nextion display

Hello, So this code have helped me a lot but I need to do multitask with the nextion display and another command in arduino, so I have to use millis instead of delay to change the images and create my animation, but the sketch that I create don't work with the nextion display using millis. Can anyone look at my code and see if there's anything wrong??

miilis_with_nextion_display.ino (22.4 KB)

Split from an old topic

Hello Natanael152
Welcome to the Arduino fora.

Before you do anything else please take a moment to read General guidance and
How to use this forum
Especially item #7 on posting code.

Thank you.

22.38 kB is too large to embed. (Maximum about 9k) :astonished:

Paul__B:
22.38 kB is too large to embed. (Maximum about 9k) :astonished:

That's a pain!

Natanael152,
You should post a shortened version of your code that illustrates your problem. However, here is a sample of your code:

#include <SoftwareSerial.h> //Include the library
SoftwareSerial mySerial(17, 16); // RX, TX

void setup() {
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }
  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop () {

}

Followed by:

static unsigned long nextion;

if (((millis() - nextion) < 166) && ((millis() - nextion) <= 166)) {
  //Change the image of image box p0
  mySerial.print("p0.pic=41");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
}
else if (((millis() - nextion) > 166) && ((millis() - nextion) <= 332)) {
  mySerial.print("p0.pic=43");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
}
else if (((millis() - nextion) > 332) && ((millis() - nextion) <= 532)) {
  mySerial.print("p0.pic=45");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
}
// ..... This goes on and on and on and...
// ending with

if ((millis() - nextion) > 7444) {
  nextion = millis();
}
}

None of the above is inside a function, nothing is ever called from loop() (or anywhere else) so none it ever does anything.

Please read:
Using millis for timing

And modify your code accordingly.

Please next time post a short version of your code that illustrates the problem in code tags.

Thank you.

Ohhh sorry in my original code I called it in void loop, leet me show you... but it doesn´t work :frowning:

#include <SoftwareSerial.h> //Include the library
SoftwareSerial mySerial(17, 16); // RX, TX

void bomdiacriador();

void setup() {
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }
  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop () {

  bomdiacriador();

}

void bomdiacriador() {

static unsigned long nextion;

if (((millis() - nextion) > 0) && ((millis() - nextion) <=166)) {
    //Change the image of image box p0
    mySerial.print("p0.pic=41");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  else if (((millis() - nextion) > 166) && ((millis() - nextion) <=332)) {
    mySerial.print("p0.pic=43");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  else if (((millis() - nextion) > 332) && ((millis() - nextion) <=532)) {
    mySerial.print("p0.pic=45");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  else if (((millis() - nextion) > 532) && ((millis() - nextion) <=698)) {
    mySerial.print("p0.pic=35");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  else if (((millis() - nextion) > 698) && ((millis() - nextion) <=864)) {
    mySerial.print("p0.pic=37");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  }

//... ending with ...

if ((millis() - nextion) > 7444) {
    nextion = millis();
  }
}

I really don't know what's wrong with this code when I use millis, but when I use delay it works, but I can't use delay because I want to do multitasking with the easy VR shield at the same time...

I tried to use this code too but it doesn't work again... :frowning:

#include <SoftwareSerial.h> //Include the library
SoftwareSerial mySerial(17, 16); // RX, TX

void bomdiacriador();

void setup() {
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }
  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}
void loop () {

  bomdiacriador();

}

void bomdiacriador() {

static unsigned long nextion;

 if ((millis() - nextion) >= 0) {
    //Change the image of image box p0
    mySerial.print("p0.pic=41");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  if ((millis() - nextion) >= 166) {
    mySerial.print("p0.pic=43");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  if ((millis() - nextion) >= 332) {
    mySerial.print("p0.pic=45");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  } 
  if ((millis() - nextion) >= 532) {
    mySerial.print("p0.pic=35");
    mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
    mySerial.write(0xff);
    mySerial.write(0xff);
  }

//// ..... This goes on and on and on and...
// ending with

if ((millis() - nextion) >= 7444) {
    nextion = millis();
  }
}

Some things you should try:

Put
Serial.print("Some text"); inside each if() statement and see if they are working.
Try your code for changing the picture in the Nextion simulator and see if it does what you expect.

Thank you PerryBebbington, but none of the tips worked. So I decided to do something different, Instead of putting delay on the arduino sketch, I put a timer on each page of my project with the Nextion Editor and managed to execute the sketch on the arduino, however when a page changes, a white image appears for a few milliseconds between each page, like a lag and I want each image to appear without that lag, I'll attach a gif here!! Do you know how I can get rid of this??

That's my new code, I also changed the baud rate of the Nextion display to 115200 but it didn't solve my problem, It only happens when I try to change pages with the arduino sketch...

#include <SoftwareSerial.h> //Include the library
SoftwareSerial mySerial(17, 16); // RX, TX

void bomdiacriador();

void setup() {
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }
  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(115200); // set the data rate for the SoftwareSerial port
}
void loop () {

  bomdiacriador();

}

void bomdiacriador() {

  mySerial.print("page 0");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.print("page 1");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.print("page 2");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.print("page 3");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.print("page 4");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.print("page 5");
  mySerial.write(0xff);  //Send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);

20200423_190519.gif

First off, I know what the problem is but I am deliberately not telling you because I want you to think about it.

Did you try this?

Serial.print("Some text"); inside each if() statement and see if they are working.

What happened? I mean describe the result, what was printed on the serial monitor? Please don't tell me that something 'didn't work', please tell me what actually happened.

Consider this:
In the code you have posted above how many times in 1 second does loop() loop?

yes, I tried to put If in Serial.print, and it was the same thing, nothing new happened. So the first animation have 8 seconds more or less, and it reproduces only 1 time when executed

You need to consider this very carefully:

In the code you have posted above how many times in 1 second does loop() loop?

I have already done everything and I didn't find the solution, and I’m still racking my brains trying to find an answer. So if you know how to solve this problem please send me an example of the code, that I have to use, and I see if it works and I can understand better what you said me to do... :slight_smile:

I've asked you a specific question at least twice and you have not acknowledged the question, let alone answered it. Please answer the question in my replies #11 and #9. Until you show signs of considering that question and provide some kind of answer that shows you are thinking about it I don't have anything more to offer.

Thank you.

Hey PerryBebbington, sorry for the very late reply, I was very busy trying to find a solution for the problem and I changed some things in the Nextion Editor and I finally solved all the problem, and I finished my project, You can see my project in this link on Youtube: NEX Project - YouTube and here in Instructables: https://www.instructables.com/id/NEX-Project/, and thank you for all the support that you provided!! :wink: :smiley:

WOW!
I am well impressed! Excellent job, love it! Love the video :slight_smile:

++Karma; // Well done!