Reading sensors and printing them to nextion

Good afternoon i would like to read 5 pressure sensors and display them on a nextion screen constantly while being on page1 only as to not slow thing down.
i have taken the forums advice and just work on one thing at a time so this is the sensors part. not printing and im not sure why.

im following a tutorial but writing the code i think i need. My thinking is probably the problem
Any help would be great, Thanks for reading.

#include <Nextion.h>

int variable1 = 0;  // Create a variable to have a counter going up by one on each cycle
int counter = 0;  // Create a variable to have a counter for the + and - buttons
int CurrentPage = 0;  // Create a variable to store which page is currently loaded
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
int sensor4 = 0;
int sensor5 = 0;
int psi = 0;
String snum1 ;
int num1 = 0; 
String snum2 ;
int num2 = 0; 
String snum3 ;
int num3 = 0; 
String snum4 ;
int num4 = 0;



NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event


NexNumber n0 = NexNumber(1, 3, "n0"); // front left psi
NexNumber n1 = NexNumber(1, 4, "n1"); // rear left psi
NexNumber n2 = NexNumber(1, 5, "n2"); // front right psi
NexNumber n3 = NexNumber(1, 6, "n3"); // rear right psi
NexNumber n4 = NexNumber(1, 7, "n4"); // tank psi

NexTouch *nex_listen_list[] = {
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  NULL
};

// Page change event:
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event


// Page change event:
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}  // End of press event


void setup() {
  // put your setup code here, to run once:
  nexInit(115200);

Serial.begin(115200);

  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
  page2.attachPush(page2PushCallback);  // Page press event

pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);

}



void loop(){
  // put your main code here, to run repeatedly:
 // 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);


if(CurrentPage == 1){

    sensor1 = analogRead(A0);
sensor1 = map(sensor1,1000,97,100,0);

 Serial.print("n4.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(sensor1);  // 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);
}} // End of release event

i have taken the forums advice and just work on one thing at a time

So, that explains why you have three pages.

pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);

Do NOT call pinMode() for pins being used as analog pins.

Apparently, you still have the nextion on the main hardware serial port, so you are debugging blind. Why?

i found out about Arduino 2 weeks ago and started learning code 2 weeks ago. i know that i have a lot to learn. i have started reading the books i bought on programming. i must have missed the part about analog pins. ill go back and read up on then.

as for the tx and rx pins they are connected to pins 16 and 17 so i guess adding serial.Println would help me figure out what its doing. but the sensors im reading work in my other sketch im trying to

PaulS:
So, that explains why you have three pages.

pinMode(A0,INPUT);

pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);



Do NOT call pinMode() for pins being used as analog pins.

Apparently, you still have the nextion on the main hardware serial port, so you are debugging blind. Why?

this code works kinda for the sensors and displays them to mit app builer app with Bluetooth connection. i had help making it. it buggy.

char BluetoothData;
int Power = 0;
int MooMoo = 0;
int Time = 0;
int PUMP1 = 22;
int PUMP2 = 23;

int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
int sensor4 = 0;
int sensor5 = 0;
int psi = 0;
String snum1 ;
int num1 = 0; 
String snum2 ;
int num2 = 0; 
String snum3 ;
int num3 = 0; 
String snum4 ;
int num4 = 0;

int ResetValue = 50;

void setup() {
Serial.begin(9600);


pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);

pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);


digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
digitalWrite(13,1);
}

void loop() {
sensor1 = analogRead(A0);
sensor1 = map(sensor1,1000,97,100,0);
Serial.print(sensor1);
Serial.print(" psi");
Serial.print("^");

sensor2 = analogRead(A1);
sensor2 = map(sensor2,1000,97,100,0);
Serial.print(sensor2);
Serial.print(" psi");
Serial.print("^");

sensor3 = analogRead(A2);
sensor3 = map(sensor3,1000,97,100,0);
Serial.print(sensor3);
Serial.print(" psi");
Serial.print("^");

sensor4 = analogRead(A3);
sensor4 = map(sensor4,1000,97,100,0);
Serial.print(sensor4);
Serial.print(" psi");
Serial.print("^");

sensor5 = analogRead(A4);
sensor5 = map(sensor5,1000,97,100,0);
Serial.print(sensor5);
Serial.println(" psi");
//delay(500);

if (Serial.available()) {
  
BluetoothData = Serial.read();
if (BluetoothData == '|') {
  delay(1);
  snum1 = Serial.readString();
  num1 = snum1.toInt();
  //Serial.println(num1);
  
  if (num1 > sensor1)
   {digitalWrite(PUMP1,HIGH);
    digitalWrite(2,LOW);
    int t = 0;
    while(t<15)
    {sensor1 = analogRead(A0);
     sensor1 = map(sensor1,1000,97,100,0);
     delay(1);
     t++;
     if(num1 <= sensor1){
      t=16;
     }
    }
     digitalWrite(2,HIGH);
     digitalWrite(PUMP1,LOW);
     }
     
   else if (num1 < sensor1)
    {digitalWrite(3,LOW);
    int t=0;
    while(t<15)
    {sensor1 = analogRead(A0);
     sensor1 = map(sensor1,1000,97,100,0);
     delay(1);
     t++;
     if(num1 >= sensor1){
      t=16;
     }
    }
      digitalWrite(3,HIGH);
      }
     }

if (BluetoothData == '-') {
  delay(1);
  snum2 = Serial.readString();
    num2 = snum2.toInt();
       
    if (num2 > sensor2)
   {digitalWrite(PUMP1,HIGH);
    digitalWrite(4,LOW);
    int t=0;
    while(t<15)
    {sensor2 = analogRead(A1);
     sensor2 = map(sensor2,1000,97,100,0);
     delay(1);
     t++;
     if(num2 <= sensor2){
      t=16;
     }
    }
     digitalWrite(PUMP1,LOW);
     digitalWrite(4,LOW);}

    else if (num2 < sensor2)
    {digitalWrite(5,LOW);
    int t=0;
    while(t<15)
    {sensor2 = analogRead(A1);
     sensor2 = map(sensor2,1000,97,100,0);
     delay(1);
     t++;
     if(num2 >= sensor2){
      t=16;
     }
    }
      digitalWrite(5,HIGH);
     }
     }
     
if (BluetoothData == '+') {
  delay(1);
  snum3 = Serial.readString();
    num3 = snum3.toInt();
    
    if (num3 > sensor3)
   {digitalWrite(PUMP2,HIGH);
    digitalWrite(6,LOW);
    int t=0;
    while(t<15)
    {sensor3 = analogRead(A2);
     sensor3 = map(sensor3,1000,97,100,0);
     delay(1);
     t++;
     if(num3 <= sensor3){
      t=16;
     }
    }
     digitalWrite(PUMP2,LOW);
     digitalWrite(6,HIGH);}

    else if (num3 < sensor3)
    {digitalWrite(7,LOW);
     int t=0;
    while(t<15)
    {sensor3 = analogRead(A2);
     sensor3 = map(sensor3,1000,97,100,0);
     delay(1);
     t++;
     if(num3 >= sensor3){
      t=16;
     }
    }
      digitalWrite(7,HIGH);
     }
}
if (BluetoothData == '`') {
  delay(1);
  snum4 = Serial.readString();
  num4 = snum4.toInt();
  
  if (num4 > sensor4)
   {digitalWrite(PUMP2,HIGH);
   digitalWrite(8,LOW);
   int t=0;
    while(t<15)
    {sensor4 = analogRead(A3);
     sensor4 = map(sensor4,1000,97,100,0);
     delay(1);
     t++;
     if(num4 <= sensor4){
      t=16;
     }
    }
     digitalWrite(8,LOW);
     digitalWrite(PUMP2,LOW);}

   else if (num4 < sensor4)
    {digitalWrite(9,LOW);
     while(num4 < sensor4)
     {sensor4 = analogRead(A3);
      sensor4 = map(sensor4,1000,97,100,0);}
      digitalWrite(9,HIGH);
     }
}
if (BluetoothData == '

) {
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
}
  //////////////////

if (BluetoothData == ‘M’) {
MooMoo = 1;
}

if (BluetoothData == ‘m’) {
MooMoo = 0;
Time = 0;
}

if (MooMoo == 1 && Time < 100) {
Time++;
delay(1);
//Serial.print(Time);
}

if (Time == 100) {
Power = 0;
}

if (BluetoothData == ‘P’) {
Power = 1;
digitalWrite(13, 1);
}

if (BluetoothData == ‘p’) {
Power = 0;
digitalWrite(13, 0);
}

if (Power == 0) {
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
}

if (BluetoothData == ‘O’) {
digitalWrite(2, 0);
digitalWrite(4, 0);
}
if (BluetoothData == ‘o’) {
digitalWrite(2, 1);
digitalWrite(4, 1);
}

if (BluetoothData == ‘U’) {
digitalWrite(3, 0);
digitalWrite(5, 0);
}

if (BluetoothData == ‘u’) {
digitalWrite(3, 1);
digitalWrite(5, 1);
}

if (BluetoothData == ‘L’) {
digitalWrite(6, 0);
digitalWrite(8, 0);
}

if (BluetoothData == ‘l’) {
digitalWrite(6, 1);
digitalWrite(8, 1);
}

if (BluetoothData == ‘Z’) {
digitalWrite(7, 0);
digitalWrite(9, 0);
}

if (BluetoothData == ‘z’) {
digitalWrite(7, 1);
digitalWrite(9, 1);
}

if (BluetoothData == ‘Y’) {
digitalWrite(3, 0);
digitalWrite(5, 0);
digitalWrite(7, 0);
digitalWrite(9, 0);
}

if (BluetoothData == ‘y’) {
digitalWrite(3, 1);
digitalWrite(5, 1);
digitalWrite(7, 1);
digitalWrite(9, 1);
}

if (BluetoothData == ‘M’) {
digitalWrite(2, 0);
digitalWrite(4, 0);
digitalWrite(6, 0);
digitalWrite(8, 0);
}

if (BluetoothData == ‘m’) {
digitalWrite(2, 1);
digitalWrite(4, 1);
digitalWrite(6, 1);
digitalWrite(8, 1);
}
if (BluetoothData == ‘A’) {
digitalWrite(2, 0);
}

if (BluetoothData == ‘a’) {
digitalWrite(2, 1);
}

if (BluetoothData == ‘B’) {
digitalWrite(3, 0);
}

if (BluetoothData == ‘b’) {
digitalWrite(3, 1);
}

if (BluetoothData == ‘C’) {
digitalWrite(4, 0);
}

if (BluetoothData == ‘c’) {
digitalWrite(4, 1);
}

if (BluetoothData == ‘D’) {
digitalWrite(5, 0);
}

if (BluetoothData == ‘d’) {
digitalWrite(5, 1);
}

if (BluetoothData == ‘E’) {
digitalWrite(6, 0);
}

if (BluetoothData == ‘e’) {
digitalWrite(6, 1);
}

if (BluetoothData == ‘F’) {
digitalWrite(7, 0);
}

if (BluetoothData == ‘f’) {
digitalWrite(7, 1);
}

if (BluetoothData == ‘G’) {
digitalWrite(8, 0);
}

if (BluetoothData == ‘g’) {
digitalWrite(8, 1);
}

if (BluetoothData == ‘H’) {
digitalWrite(9, 0);
}

if (BluetoothData == ‘h’) {
digitalWrite(9, 1);
}
}
delay(1);}

Does this look better?

#include <Nextion.h>

int CurrentPage = 0;  // Create a variable to store which page is currently loaded
int Tank_PSI = A0;
int LF_PSI = A1;
int RF_PSI = A2;
int LR_PSI = A3;
int RR_PSI = A4;
int val = 0;




NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event


NexNumber n0 = NexNumber(1, 3, "n0"); // front left psi
NexNumber n1 = NexNumber(1, 4, "n1"); // rear left psi
NexNumber n2 = NexNumber(1, 5, "n2"); // front right psi
NexNumber n3 = NexNumber(1, 6, "n3"); // rear right psi
NexNumber n4 = NexNumber(1, 7, "n4"); // tank psi

NexTouch *nex_listen_list[] = {
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  NULL
};

// Page change event:
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event


// Page change event:
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}  // End of press event


void setup() {
  // put your setup code here, to run once:
  nexInit(115200);

Serial.begin(115200);

  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
  page2.attachPush(page2PushCallback);  // Page press event
}

void loop(){
  // put your main code here, to run repeatedly:
 // 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);


if(CurrentPage == 1){

    val = analogRead(Tank_PSI);
Tank_PSI = map(Tank_PSI,1000,97,100,0);

 Serial.print("n4.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(Tank_PSI);  // 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);
}} // End of release event

Anyone???

I would suggest NOT sending commands the way you are doing it, as you are ignoring the responses, which will clog the communications pipe. Instead do this:

char s[16];
sprintf(s, "n4.val=%d", Tank_PSI);
if (sendCommandWait(s))
{
    // command was successfull
}
else
{
    // command is invalid, or failed
}

This will send the command and wait for the response, returning a boolean value that indicates whether the display accepted or rejected the command.

Regards,
Ray L.

thank you

i added this to the caode and now it blocking me from posting

RayLivingston:
I would suggest NOT sending commands the way you are doing it, as you are ignoring the responses, which will clog the communications pipe. Instead do this:

char s[16];

sprintf(s, "n4.val=%d", Tank_PSI);
if (sendCommandWait(s))
{
    // command was successfull
}
else
{
    // command is invalid, or failed
}




This will send the command and wait for the response, returning a boolean value that indicates whether the display accepted or rejected the command.

Regards,
Ray L.
#include <Nextion.h>

int CurrentPage = 0;  // Create a variable to store which page is currently loaded
int Tank_PSI = A0;
int val = 0;

NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event

NexNumber n4 = NexNumber(1, 7, "n4"); // tank psi

NexTouch *nex_listen_list[] = {
  
  &page1,  // Page added as a touch event
   NULL
};

// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event

void setup() {
  // put your setup code here, to run once:
  nexInit(115200);

Serial.begin(115200);

  page1.attachPush(page1PushCallback);  // Page press event
}

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.
  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);

char s[16];
sprintf(s, "n4.val=%d", Tank_PSI);
if (sendCommandWait(s))
{
    // command was successfull
}
else
{
    // command is invalid, or failed
}
if(CurrentPage == 1){

    val = analogRead(Tank_PSI);
Tank_PSI = map(Tank_PSI,1000,97,100,0);

 Serial.print("n4.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(Tank_PSI);  // 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);
}} // End of release event