Help please. reading and printing but seem to be jumping in the boxes

n0-n4 are boxes on a nextion and they seam to be crossing over and printing randomly. Am i not telling it to do the right thing? Any help would be appreciated

void loop()

{
  // Send page number to the object called np:
  // This object (np) exist on every page so at this point we don't need to check which page is loaded on the display.
 // Send page number to the object called np:
  // This object (np) exist on every page so at this point we don't need to check which page is loaded on the display.
  Serial.print("np.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  Serial.print(CurrentPage);  // This is the value you want to send to that object and atribute mentioned before.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial.write(0xff);
  Serial.write(0xff);

  
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
    if (currentMillis - startMillis >= period)  //test whether the period has elapsed

if(CurrentPage == 1){


if (sendCommandWait(s0))
{
    int Tank_PSI = analogRead(Tank_PSI);  //Read the analog value. Values from 0 to 1024
  Tank_Value = (Tank_PSI-102)* 200.0 / 817;
  Serial.print("n4.val=");              //We print the variable we want to cahnge on the screen
  Serial.print(Tank_Value);                        //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
}
else
{
    // command is invalid, or failed
}

if (sendCommandWait(s1))
{
    int LF_PSI = analogRead(LF_PSI);  //Read the analog value. Values from 0 to 1024
  LF_PSI_Value = (LF_PSI-102)* 200.0 / 817;
  Serial.print("n0.val=");              //We print the variable we want to cahnge on the screen
  Serial.print(RR_PSI_Value);                        //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
}
else
{
    // command is invalid, or failed
}
  if (sendCommandWait(s2))
{
    int RF_PSI = analogRead(RF_PSI);  //Read the analog value. Values from 0 to 1024
  RF_PSI_Value = (RF_PSI-102)* 200.0 / 817;
  Serial.print("n2.val=");              //We print the variable we want to cahnge on the screen
  Serial.print(RF_PSI_Value);                        //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
}
else
{
    // command is invalid, or failed
}

if (sendCommandWait(s3))
{
    int LR_PSI = analogRead(LR_PSI);  //Read the analog value. Values from 0 to 1024
  LR_PSI_Value = (LR_PSI-102)* 200.0 / 817;
  Serial.print("n1.val=");              //We print the variable we want to cahnge on the screen
  Serial.print(LR_PSI_Value);                        //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
}
else
{
    // command is invalid, or failed
}

if (sendCommandWait(s4))
{
    int RR_PSI = analogRead(RR_PSI);  //Read the analog value. Values from 0 to 1024
  RR_PSI_Value = (RR_PSI-102)* 200.0 / 817;
  Serial.print("n3.val=");              //We print the variable we want to cahnge on the screen
  Serial.print(RR_PSI_Value);                        //Print the value we want to be displayed
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);
}
else
{
    // command is invalid, or failed
}


startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.

}
else{  
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);}


if(CurrentPage == 3){ 
}
  else{
  }
  nexLoop(nex_listen_list);  // Check for any touch event
}

Someone with nextion experience may be able to help you. I have no nextion experience. What I DO know is that someone who has posted 22 times should know better than to post a code snippet.

williamchivers:
Am i not telling it to do the right thing?

Obviously. Whatever 'it' may be.

If that is not helpful enough, maybe you should give more information than an unformated snippet of a program

  • running on unknown hardware
  • powered somehow
  • talking over an unknown connection
  • to an unknown Nextion device
  • powered somehow
  • setup in an unknown way

Hello William,
Have you read my tutorial 'using Nextion displays with Arduino'? I suggest that you don't bother with number boxes as they can only display integers, making them pretty restricted in what they can show, for example showing
Page 3 or £12.64 or 89.67% is a pain using number boxes and dead simple using text boxes.

The formatting of your code needs tidying up. I find it difficult to read other people's code at the best of times, reading it when it's a mess makes it harder.

You don't need else after if() when there is nothing to do when if() fails.

In terms of sending stuff to the Nextion I think your code is correct. In terms of how it all hangs together, I am struggling to make sense of it. Please learn to write functions to do specific tasks then call those functions from loop(); The code in my tutorial illustrates how to do this.

thank you for the reply. i used your tutorial to help me achieve this.

PerryBebbington:
Hello William,
Have you read my tutorial 'using Nextion displays with Arduino'?

Mega board
nextion NX8048T050_11 screen
8 relay board.
5 sensors 200psi
read on 5 sensors while on page 1 and print them to its correct box

Whandall:
Obviously. Whatever 'it' may be.

If that is not helpful enough, maybe you should give more information than an unformated snippet of a program

here it the code in whole.

Enjoy_the_Ride_final.ino (12.9 KB)

here is the screen

Why Serial? There are three other hardware serial unused, don't you like debug output?

Why do you think it's a good idea to set np to the current page on each loop iteration?

Why do you think setting the values to a pin number before setting them to the correct value is a good idea?

Why does the

    if (CurrentPage == 1) {

have such an odd else?

    } else {
      Serial.write(0xff);                         //Always add 3 full bytes after...
      Serial.write(0xff);
      Serial.write(0xff);
    }

Thank you for responding. Im new to this whole world of programming. I was following a tutorial for the
if (CurrentPage ==1) only do whats on that page.

Serial.write (0xff) was used 3 times to end the comands on the nextion.

You say that there are 3 other hardware serial unused.
Are you reffering to serial montior to see whats going on? If so when i was printing to the monitor everything was printing as i told it to. But i think something else is going on. If you were not can you please elaborate.

Setting pin number before setting them to the correct value.
Can you elaborate on this as well please.

Whandall:
Why Serial? There are three other hardware serial unused, don't you like debug output?

Why do you think it's a good idea to set np to the current page on each loop iteration?

Why do you think setting the values to a pin number before setting them to the correct value is a good idea?

Why does the

    if (CurrentPage == 1) {

have such an odd else?

    } else {

Serial.write(0xff);                        //Always add 3 full bytes after...
      Serial.write(0xff);
      Serial.write(0xff);
    }

So you don't have answers to my questions?

I have one more.
What is this?

(Tank_PSI,INPUT);           //Define that pin as input
  (LF_PSI,INPUT);
  (RF_PSI,INPUT);
  (LR_PSI,INPUT);
  (RR_PSI,INPUT);

Using a different serial port would allow debug prints that would not confuse the Nextion.

williamchivers:
Serial.write (0xff) was used 3 times to end the comands on the nextion.

How is that connected to a timeout if not on page 1?

    if (currentMillis - startMillis >= period)  //test whether the period has elapsed

if(CurrentPage == 1){

}
else{  
  Serial.write(0xff);                         //Always add 3 full bytes after...       
  Serial.write(0xff);
  Serial.write(0xff);}

Why do you think it's a good idea to set np to the current page on each loop iteration?

williamchivers:
Setting pin number before setting them to the correct value.

You are writing the pin number to the fields before writing the real value to them,
why are you doing that? Is that some kind of flicker test?

One example:

char s0[16];
const int Tank_PSI = A0;

  sprintf(s0, "n4.val=%d", Tank_PSI);

      if (sendCommandWait(s0)) {
        int Tank_PSI = analogRead(Tank_PSI);
        Tank_Value = (Tank_PSI - 102) * 200.0 / 817;
        Serial.print("n4.val=");
        Serial.print(Tank_Value);
        Serial.write(0xff);
        Serial.write(0xff);
        Serial.write(0xff);
      }

i had 2 separate sketches i wrote one for getting all of my buttons working and one for the gauges. this was a mistake copy over to this sketch. Ill remove it.

(Tank_PSI,INPUT);           //Define that pin as input
  (LF_PSI,INPUT);
  (RF_PSI,INPUT);
  (LR_PSI,INPUT);
  (RR_PSI,INPUT);

im using TX0 and RX2 on the mega board and the nextion only has 1 set of tx and rx pin?

Using a different serial port would allow debug prints that would not confuse the Nextion.

This was me trying to fix a glitch ill remove it as well

How is that connected to a timeout if not on page 1?

it was suggested to me in another post i made about this project that i was telling it to send the val but not waiting to see if it did. I thought i was adding it to the sketch the correct way as no instruction was given on how to add it. i read up as much as i could find about char. they also said by adding this it would not congest the com line. sorry im a new guy still learning

this was what i was given

char s[16];
sprintf(s, "n4.val=%d", Tank_PSI);
if (sendCommandWait(s))
{
    // command was successfull
}
else
{
    // command is invalid, or failed
}
You are writing the pin number to the fields before writing the real value to them,
why are you doing that? Is that some kind of flicker test?[code]

[/code]

williamchivers:
im using TX0 and RX2 on the mega board

My Mega clone has no TX0, but RX2 belongs to Serial2,
if this is true you should see no touch events and all the sendCommandWaits should fail.

williamchivers:
it was suggested to me in another post i made about this project

So why did you start a new thread then?

You should answer questions, just quoting them is rather pointless.

I get the impression that you have no clue what you are doing.

I'm using TX0 and RX2 on the mega board and the nextion only has 1 set of tx and rx pin?

When I first read your code I missed that you have your Nextion connected to serial port 0. You can't use 0 as that's for the serial monitor. You need to pick a serial port other than 0 and stick to it. I suggest you use serial port 1, but if you prefer 2 or 3 that's fine, just don't use Tx on one port and Rx on another.

Please spend some time, by which I mean hours or even days if necessary and tidy up your code and write functions for individual tasks, as in my tutorial. When you've done that come back and ask for more help.

Thank you.

Ok will do. Thank you

PerryBebbington:
When I first read your code I missed that you have your Nextion connected to serial port 0. You can't use 0 as that's for the serial monitor. You need to pick a serial port other than 0 and stick to it. I suggest you use serial port 1, but if you prefer 2 or 3 that's fine, just don't use Tx on one port and Rx on another.

Please spend some time, by which I mean hours or even days if necessary and tidy up your code and write functions for individual tasks, as in my tutorial. When you've done that come back and ask for more help.

Thank you.