Nextion Display - Tutorial PerryBebbington

HI!
I'm a beginner, and ran the demo of the Berry tutorial
and almost everything worked. I had doubts.
On the display we have 2 screens and each screen has a component t1.txt.
in the function:
// This displays the clock
void HMI_display_clock () {
char timestring [9];
sprintf (timestring, "% 02d:% 02d:% 02d", clock.hour, clock.minute, clock.second);
Serial2.println (timestring);
Serial2.print (F ("t1.txt = " "));
Serial2.print (timestring);
Serial2.print ("" ");
Serial2.write (0xff);
Serial2.write (0xff);
Serial2.write (0xff);
}
how does the arduino know which component to print on, the t1 on page 0 or the one on page1?

Hello vovonauta,
Welcome.

Please can you read https://forum.arduino.cc/index.php?topic=710766.0 again, especially the bit about posting code.

How does the Arduino know which component to print on, the t1 on page 0 or the one on page1?

It doesn't know, it doesn't matter because regardless of which page is displayed the text box for the time is always t1. I was careful to use text boxes with the same object name for displaying the same data on each page.
If you look at the code in part 4, Nextion additional features, I use a variable

uint8_t active_page = 0; //This variable is for the sketch to keep track of which page the Nextion display is on.

active_page is how the Arduino knows which page is being displayed.

However, please note that in the code for the first part of the tutorial I kept it as simple as I could, for the additional features code I crammed as much in as I could think of without any attempt to make it simple. I suggest you avoid the additional features code until you fully understand the first part of the tutorial.

Sry!

I downloaded the simple code (first part Nextion_demonstration43)
Everything works, the variable timestring has the clock working
but does not print to t1.txt (page 0)

All indices are correct, so I was in doubt.

(..additional features not for me.. lol)

thanks for the feedback
and excuse me.

Sorry, can you clarify this for me please:

Everything works, the variable timestring has the clock working
but does not print to t1.txt (page 0)

If it works it must be displaying the time in the middle of page 0, which means it is printing to t1.txt as that is where the time is displayed. So you saying it works but does not print to t0.txt is contradictory. Please clarify.

Thanks.

I know it works because I checked by printing (var timestring) on the Arduino Serial monitor
but the t1.txt on the display does not change, it only shows hh: mm: ss
T0.txt shows the initial message normally.
buttons work (+ and -)
slider page 1 and all text boxes work

Only the t1 on page zero does not show the values.
Shows only hh: mm: ss

for sure the problem is with me lol..

Please open your program in the IDE and under edit select copy for forum. Paste it as a reply so I can see it and try it. Maybe there's a mistake in it I don't know about.

Thanks.

Forget that, I found your mistake:

 Serial2.println (timestring);

Look closely at that.

is not it
I had already verified this error…
forgot to edit in the forum… sry

//https://forum.arduino.cc/index.php?topic=604185.0

void setup() {
  //This is for the Serial2 monitor. Remove the // if you need to use it.
  //Serial2.begin(9600);
  
  //This is for Serial2 port 1, which is the one used for the Nextion in this demonstration. You might need to change this depending on which Serial2 port you are using for your display.
  Serial2.begin(9600);
  Serial.begin(9600);
  uint8_t active_page = 0;
  //For  demonstration
  HMI_startup_message();
}

void loop() {
  HMI_read();
  clock_run();
  
}

struct CLOCK {
  int8_t hour;
  int8_t minute;
  int8_t second;
};
struct CLOCK clock;

//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  Serial2.print(timestring);
  Serial2.print(F("t1.txt=\""));
  Serial2.print(timestring);
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  Serial2.print(F("t0.txt=\""));
  Serial2.print(F("Esta eh a pagina "));
  Serial2.print(page);
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

void HMI_P1_display_slider(uint8_t slider_d1, uint8_t slider_d0) {
  uint16_t slider_val = (slider_d1 <<8 | slider_d0);
  
  //This displays byte 1 of the slider value in HEX
  Serial2.print(F("t2.txt=\""));
  Serial2.print(slider_d1, HEX);
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  
  //This displays byte 0 of the slider value in HEX
  Serial2.print(F("t3.txt=\""));
  Serial2.print(slider_d0, HEX);
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  //This displays the complete slider value in decimal
  Serial2.print(F("t4.txt=\""));
  Serial2.print(slider_val);
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

void HMI_startup_message() {
  Serial2.print(F("t0.txt=\""));
  Serial2.print(F("TUTORIAL DO PERRY https://forum.arduino.cc/index.php?board=7.0"));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

//HMI_read takes the data sent from the Nextion to the Serial2 port and processes it depending on what has been sent
//There are 3 levels of nested switch statements corresponding to the page, the type of object and the index of the object.
void HMI_read() {
  static uint8_t HMI_read_data[10];         //This is a temporary buffer to hold the data from the display. Space for 10 bytes although this demonstration only uses 6 bytes
  static uint8_t HMI_read_data_i;           //This is a count of how many bytes have been received from the display.
  static uint8_t a5count;                   //0xa5 repeated 3 times is used as a start indicator, this is a count of how many times it has been received.
  uint8_t readtemp;                         //This is to hold the last received byte to ensure that it is only read from the receive buffer once.
  
  while (Serial2.available() > 0) {         //Read every byte in the receive buffer
    
    readtemp = Serial2.read();
   // Serial.println(readtemp);
    if (readtemp == 0xa5) {                 //Count the number of times 0xa5 has been received
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }
    else {
      a5count = 0;
    }
    HMI_read_data[HMI_read_data_i] = readtemp;
    if (HMI_read_data_i == 5) {
      
      switch (HMI_read_data[1]) {             //HMI_read_data[1] contains the page the data has come from
        case 0:                               //Case 0 means the data has come from page 0
          switch (HMI_read_data[2]) {         //HMI_read_data[2] contains the type of object on the page that the data has come from
            case 0:                           //In this demonstraton case 0 selects a page
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:  
            Serial.println("entrou +hh");//In this demonstration case 1 is a button for setting the clock
              switch (HMI_read_data[3]) {      //HMI_read_data[3] is the index of the type of button, so 0 to 5 as there are 6 buttons for setting the clock. Each case is a different button.
                case 0:
                  ++clock.hour;
                  if (clock.hour > 23) {
                    clock.hour = 0;
                  }
                  
                  
                    break;
                case 2:
                Serial.println("entrou -hh");
                  --clock.hour;
                  if (clock.hour < 0) {
                    clock.hour = 23;
                  }
                  break;
                case 3:
                Serial.println("entrou +mm");
                  ++clock.minute;
                  if (clock.minute > 59) {
                    clock.minute = 0;
                  }
                  break;
                case 8:
                Serial.println("entrou -mm");
                  --clock.minute;
                  if (clock.minute < 0) {
                    clock.minute = 59;
                  }
                  break;
                case 9:
                Serial.println("entrou +ss");
                  ++clock.second;
                  if (clock.second > 59) {
                    clock.second = 0;
                  }
                  break;
                case 10:
                Serial.println("entrou -ss");
                  --clock.second;
                  if (clock.second < 0) {
                    clock.second = 59;
                  }
                  break;
              }
              HMI_display_clock();
              break;
          }
          break;
        case 1:                               //Case 1 means the data has come from page 1
          switch (HMI_read_data[2]) {
            case 0:                           //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                           //Data from the slider on page 1
              HMI_P1_display_slider(HMI_read_data[5], HMI_read_data[4]);      //HMI_read_data[5] is byte 1 of the slider value, HMI_read_data[4] is byte 0 of the slider value
              
              
              
              break;
          }
          break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i > 9) {
      HMI_read_data_i = 9;
    }
  }
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 1000;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ++clock.second;
    if (clock.second > 59) {
      clock.second = 0;
      ++clock.minute;
      if (clock.minute > 59) {
        clock.minute = 0;
        ++clock.hour;
        if (clock.hour > 23) {
          clock.hour = 0;
        }
      }
    }
    HMI_display_clock();
  }
}

Ok. Getting late here and I'm tired. Looks ok but I will try it tomorrow. If you find the problem before then please let me know.

If you can watch this video.
tHe engine starting rpm at minimum and growing to maximum

Tks!

I’ve had a chance to look. The problem was obvious and staring me in the face but I could not see it despite looking at it several times and in different ways. I had to ask a friend and he spotted it immediately.

You have this:

void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  Serial2.print(timestring);       // <-----This should not be there
  Serial2.print(F("t1.txt=\""));
  Serial2.print(timestring);
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

The correct code is:

void HMI_display_clock() {
 char timestring[9];
 sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
 Serial2.print(F("t1.txt=\""));
 Serial2.print(timestring);
 Serial2.print("\"");
 Serial2.write(0xff);
 Serial2.write(0xff);
 Serial2.write(0xff);
}

There is another mistake I found that is my mistake and I need to correct in the tutorial. There is a space at the end of timestring caused by the space in this:

sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);

Should be:

sprintf(timestring, "%02d:%02d:%02d", clock.hour, clock.minute, clock.second);

As far as I know that is not causing a problem.
Also, the final " should be defined with the F macro to save RAM, again, this is not causing a problem, it’s just untidy by me.

I hope that fixes your problem.

I saw that duplicated line and I think I already tried to delete it and it didn't work either, but I'll check.
Note: In your tutorial and in the file provided, the line is also duplicated

Excuse me. I'm reading and watching a lot of videos to implement a project and I'm getting confused.
I'll go for a little bit and rest.

I met Display Nextion 20 days ago and I'm struggling to learn.
Thank you very much for your patience.

Note: In your tutorial and in the file provided, the line is also duplicated.

Thanks for poiting that out, I’ll edit it.

++Karma; // :slight_smile: