Using Nextion display button to interrupt serial comms loop

Hi, I am currently using a basic Nextion display with an Arduino Uno. I am creating a data logging module for upto 4 sensors.

The following image shows the screen accessed for displaying/logging:

The code, including parts of @PerryBebbington s introduction to Nextion tutorial, reads the data from a sensor and displays it as it should, however upon pressing start, I cannot stop the following 'while' loop effectively when 'II' is pressed on the screen.

while (Graph_pauseState != 1)

I have had to put in a delay to allow enough time for the 'II' button on screen to be recognised and break out of the loop.
This works, however it is not effective and I know the delay is blocking the program somewhat.

Full code below:

/*Changelog:
  -18/07/2022: Starting to add analog input to test printing to Nextion.
  -20/07/2022: Added graph display data and botton to start and pause waveform. Added sensor state for start and pause conditions
*/
enum pages {page0, page1, page2, noOfPages};
enum HMIData {spare, page, type, index, data0, data1};
int sample;
int Graph_startState = 0; // variable for reading the Nextion start button state
int Graph_pauseState = 0; // variable for reading the Nextion pause button state
int h;
int m;
int s;
int dd;
int mm;
int yy;
int sensor0 = A0;
int v;
int readingScaled;
String dc;
String ddi;
String mmi;
String yyi;
String dat;
String tim;
String hc;
String hi;
String mi;
String si;

#include <SoftwareSerial.h>
#include <SPI.h>
#include <DS3231.h>
SoftwareSerial Serial2(2, 3); // RX, TX

DS3231  rtc(SDA, SCL);

void setup() {

  Serial2.begin(9600);
  Serial.begin(9600);
  rtc.begin();
  pinMode(A0, INPUT_PULLUP);
  pinMode(sensor0, INPUT);




}
//************************************************************ main************************************************
void loop() {
  clock_run();
  HMI_read();
}

//************************************************************ main************************************************

//This displays the clock
void HMI_display_clock() {
  dat = rtc.getDateStr();
  tim = rtc.getTimeStr();
  Serial2.print(F("t1.txt=\""));
  Serial2.print(rtc.getTimeStr());
  endLine();        // Sends 0xff 0xff 0xff, which tell the Nextion that it has a complete string of data
  Serial2.print(F("t2.txt=\""));
  Serial2.print(tim);
  endLine();
  Serial2.print(F("t3.txt=\""));
  Serial2.print(dat);
  endLine();
  Serial2.print(F("t4.txt=\""));
  Serial2.print(dat);
  endLine();
  Serial2.print(F("t5.txt=\""));
  Serial2.print(rtc.getTemp());
  Serial2.print(F("C"));
  endLine();
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  //  Serial2.print(F("t0.txt=\""));
  //  Serial2.print(F("This is page "));
  //  Serial2.print(page);
  //  endLine();
  //  Serial.println("Page");
}

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);
  endLine();
  Serial.println("slider 1" + slider_d1);

  //This displays byte 0 of the slider value in HEX
  Serial2.print(F("t3.txt=\""));
  Serial2.print(slider_d0, HEX);
  endLine();
  Serial.println("slider 2 " + slider_d0);

  //This displays the complete slider value in decimal
  Serial2.print(F("t4.txt=\""));
  Serial2.print(slider_val);
  endLine();
  Serial.println("slider 3" + slider_val);
}

//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() {

#define read_data_size 10                       //Size of buffer use to receive data from Nextion (The string sent from nextiojn is 10 long) 
  static uint8_t HMI_read_data[read_data_size];   //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.
  //   Serial.println("rec");
  while (Serial2.available() > 0) {               //Read every byte in the receive buffer
    readtemp = Serial2.read();
    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) {                    //This reads the 5th section of the Nextion string/HEX value

      switch (HMI_read_data[page]) {               //The page the data has come from

        /* ---------- Page 0 ----------*/
        case page0 :
          switch (HMI_read_data[type]) { //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
            case 0:
              HMI_display_page(HMI_read_data[index]); //Select a page
              break;
          }
        /* ---------- Page 1 ----------*/
        case page1 :
          switch (HMI_read_data[type]) { //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
            case 0:
              HMI_display_page(HMI_read_data[index]); //Select a page
              break;
            case 1:                               //In this demonstration case 1 is a button for setting the clock (This points to the 4th section of the Nextion Hex)
              values();
              v = HMI_read_data[index];
              modRt(v); //move the next bit to a function
              switch (HMI_read_data[index]) {     //HMI_read_data[index] 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. (index in this case refers to the 5th section of HEX)
                case 0:
                  Serial.println("clock set Hour + pushed");
                  h++;
                  if (h > 23) {
                    h = 0;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 1:
                  Serial.println("clock set Hour - pushed");
                  h--;
                  if (h < 0) {
                    h = 23;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 2:
                  Serial.println("clock set Min + pushed");
                  m++;
                  if (m > 59) {
                    m = 0;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 3:
                  Serial.println("clock set Min - pushed");
                  m--;
                  if (m < 0) {
                    m = 59;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 4:
                  Serial.println("clock set Sec + pushed ");
                  s++;
                  if (s > 59) {
                    s = 0;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 5:
                  Serial.println("clock set Sec - pushed ");
                  s--;
                  if (s < 0) {
                    s = 59;
                  }
                  rtc.setTime(h, m, s);
                  break;
                case 6:
                  Serial.println("date set day + pushed");
                  dd++;
                  if (dd > 31) {
                    dd = 1;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
                case 7:
                  Serial.println("date set day - pushed");
                  dd--;
                  if (dd < 1) {
                    h = 31;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
                case 8:
                  Serial.println("date set month + pushed");
                  mm++;
                  if (mm > 12) {
                    mm = 1;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
                case 9:
                  Serial.println("date set month - pushed");
                  mm--;
                  if (mm < 1) {
                    mm = 12;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
                case 10: //REMEMBER IN HEX e.g. 10 = 16 in HEX and A5 in HEX = 10 in DEC
                  Serial.println("date set year + pushed");
                  yy++;
                  if (yy > 2099) {
                    yy = 0;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
                case 11: //
                  Serial.println("date set year - pushed");
                  yy--;
                  if (yy < 0) {
                    yy = 2099;
                  }
                  rtc.setDate(dd, mm, yy);
                  break;
              }
              HMI_display_clock();
              break;
          }
          break;
        /* ---------- Page 2 ----------*/
        case page2:                               //Case 2 means the data has come from page 2
          switch (HMI_read_data[type]) {
            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[index]);
              break;
            case 1:                           //Data from the Sensors on page 2
              switch (HMI_read_data[index]) {     //HMI_read_data[index] 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. (index in this case refers to the 5th section of HEX)
                case 0:
                  break;

                case 1: //Graph start button
                  Graph_pauseState = 0;
                  Serial.println(Graph_pauseState);
                  Sensor_read();
                  break;
              }
          }
          break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i >= read_data_size) {
      HMI_read_data_i = read_data_size - 1;
    }
  }
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 500;
  if (currentMillis - previousMillis >= interval) {
    previousMillis += interval;
    HMI_display_clock();
  }
}

void Sensor_read() { //Called when entering a page requiring sensor data to be printed.

  //for (int sample = 0; sample <= 1000; sample++) {// number of samples
  while (Graph_pauseState != 1) {// number of samples

    //----------------------------------------------------------------------------------
#define read_data_size 10                       //Size of buffer use to receive data from Nextion (The string sent from nextiojn is 10 long) 
    static uint8_t HMI_read_data[read_data_size];   //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.
    //   Serial.println("rec");
    while (Serial2.available() > 0) {               //Read every byte in the receive buffer
      readtemp = Serial2.read();
      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) {                    //This reads the 5th section of the Nextion string/HEX value

        switch (HMI_read_data[page]) {               //The page the data has come from

          /* ---------- Page 2 ----------*/
          case page2:                               //Case 2 means the data has come from page 2
            switch (HMI_read_data[type]) {
              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[index]);
                break;
              case 1:                           //Data from the Sensors on page 2
                switch (HMI_read_data[index]) {     //HMI_read_data[index] 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. (index in this case refers to the 5th section of HEX)

                  case 2: //Graph pause button
                    Graph_pauseState = 1;
                    Serial.println(Graph_pauseState);
                    break;
                }
            }
            break;
        }
      }
      ++HMI_read_data_i;
      if (HMI_read_data_i >= read_data_size) {
        HMI_read_data_i = read_data_size - 1;
      }
    }
    int sensor0Read = analogRead(sensor0);
    Serial2.print("add 9,0,");
    readingScaled = map(sensor0Read, 0, 1200, 2, 198);
    Serial2.print(readingScaled);
    Serial.println(sensor0Read);
    Serial2.print(F("\xFF\xFF\xFF"));
    Serial2.print(F("sense0.txt=\""));
    Serial2.print(sensor0Read);
    endLine();
    clock_run();
    delay(125); 



    //-----------------------------------------------------------------------------------
  }
}

void endLine() {
  Serial2.print(F("\""));
  Serial2.print(F("\xFF\xFF\xFF"));
}

void values() {
  hc = rtc.getTimeStr();
  hi = hc.substring(0, 2);
  mi = hc.substring(3, 5);
  si = hc.substring(6, 8);

  h = hi.toInt();
  m = mi.toInt();
  s = si.toInt();

  dc = rtc.getDateStr();
  ddi = dc.substring(0, 2);
  mmi = dc.substring(3, 5);
  yyi = dc.substring(6, 10);

  dd = ddi.toInt();
  mm = mmi.toInt();
  yy = yyi.toInt();
}

int modRt(int vu) {
  Serial.println(vu);
}

Could someone please advise on a method of breaking out of this loop?

Cheers.

Quick reply as I am not at home with access to my PC: don't use a while loop, it's blocking anything else from happening. There are tutorials for multi tasking, you need to understand them and re-write your code. Look at how I handle receiving data.

Why have you included software serial when you obviously don't need it?

Use of Strings can lead to problems, stick with null terminated char arrays, AKA C strings.

1 Like

Thank you.

I shall have a look into the multitasking and get rid of the While.

I was using software serial to debug with the serial monitor.

I have tried a few methods and I think it is because I am trying to send and receive data to and from the Nextion at the same time and it only works with the delay because I can 'sometimes' push the button at the correct time in the loop.

I know this because as soon as I comment out the 'Serial2.print' actions within sensor_read, the buttons register and break out of the loop...

I'm on my PC now and can read and type properly without the pain of using a tiny phone screen.

I missed that you need software serial because you have a Uno with only one serial port.

The Nextion does not mind if you send to it and receive from it at the same time but I'm not sure software serial can cope with it. I always discourage people from using a Uno because of the lack of a second hardware serial port. My preferred board is the Nano Every, which has another serial port, or perhaps a Mega. I suggest you buy one or more Nano Everys or Megas to make life easier.

You have 2 lots of code to read data from the Nextion, once in

void HMI_read()

And again in

void Sensor_read()

This makes no sense at all and is going to cause problems. The only place you should be reading the Nextion data is in HMI_read. That you have included it elsewhere is a sign that you need to understand the tutorial I linked to. The way to handle reading the sensor data is to have Sensor_read code called repeatedly in loop() just as HMI_read is, each time it is called it should read the sensor once and save the sample somewhere, probably an array - Arduino Reference , then when it is required to send the sensor data to the graph the data is already there in the array. However, this is not a beginner thing to as you will find the serial port fills up and blocks the rest of the code. You have to break the sending into chunks.

My suggestion is:

  • Understand multi tasking from the tutorial
  • Re-write so the sensor read code takes one reading per call and saves them in an array
  • Write another function that sends the sensor data from the array to the Nextion when required to update the graph.

Get this working as best you can then come back and ask for help with the problems I know you will have.

Which Nextion do you have, specifically what size display? If, for example, it is 480 x 272 pixels then the maximum possible width of the graph is 480 pixels so there is no point trying to send more then 480 data points to it in one go.

You absolutely DO NOT need the delay at the end of Sensor_ read().

1 Like

So I tidied up the code and tried one last thing before looking into arrays.
I used the hardware serial to communicate with the display and alas it works as I think it should.
I will be purchasing a Nano Every.
I am still going to look at arrays as it sound like the correct way to capture and send the sensor data.

Code so far is as follows:


enum pages {page0, page1, page2, noOfPages};
enum HMIData {spare, page, type, index, data0, data1};
int sample;
int Graph_startState = 0; // variable for reading the Nextion start button state
int Graph_pauseState = 0; // variable for reading the Nextion pause button state
int h;
int m;
int s;
int dd;
int mm;
int yy;
int sensor0 = A0;
int sensor1 = A1;
int sensor2 = A2;
int sensor3 = A3;
int v;
int readingScaled;
String dc;
String ddi;
String mmi;
String yyi;
String dat;
String tim;
String hc;
String hi;
String mi;
String si;
int P1_B0_State = 0; // Variable for Nextion Page1 button 0
int P1_B1_State = 0;
int P1_B2_State = 0;
int P1_B3_State = 0;
int P1_B4_State = 0;
int P1_B5_State = 0;
int P1_B6_State = 0;
int P1_B7_State = 0;
int P1_B8_State = 0;
int P1_B9_State = 0;
int P1_B10_State = 0;
int P1_B11_State = 0;
int P2_B0_State = 0;
int P2_B1_State = 0;
int P2_B2_State = 0;

#include <SoftwareSerial.h>
#include <SPI.h>
#include <DS3231.h>
SoftwareSerial Serial2(2, 3); // RX, TX

DS3231  rtc(SDA, SCL);

void setup() {

  Serial2.begin(9600);
  Serial.begin(9600);
  rtc.begin();
  pinMode(A0, INPUT_PULLUP);
  pinMode(sensor0, INPUT);
  pinMode(A1, INPUT_PULLUP);
  pinMode(sensor1, INPUT);
  pinMode(A2, INPUT_PULLUP);
  pinMode(sensor2, INPUT);
  pinMode(A3, INPUT_PULLUP);
  pinMode(sensor3, INPUT);
}

void loop() {
  clock_run();
  Nex_Button_Read();
  Sensor_read();
}

void Nex_Button_Read() {

#define read_data_size 10
  static uint8_t HMI_read_data[read_data_size];   //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.
  //   Serial.println("rec");
  while (Serial.available() > 0) {               //Read every byte in the receive buffer
    readtemp = Serial.read();
    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) {                    //This reads the 5th section of the Nextion string/HEX value

      switch (HMI_read_data[page]) {               //The page the data has come from

        /* ---------- Page 0 ----------*/
        case page0 :
          switch (HMI_read_data[type]) { //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
            case 0:
              HMI_display_page(HMI_read_data[index]); //Select a page
              break;
          }
        /* ---------- Page 1 ----------*/
        case page1 :
          switch (HMI_read_data[type]) { //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
            case 0:
              HMI_display_page(HMI_read_data[index]); //Select a page
              break;
            case 1:                               //In this demonstration case 1 is a button for setting the clock (This points to the 4th section of the Nextion Hex)
              values();
              v = HMI_read_data[index];
              modRt(v); //move the next bit to a function
              switch (HMI_read_data[index]) {     //HMI_read_data[index] 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. (index in this case refers to the 5th section of HEX)
                case 0:
                  P1_B0_State = 1;
                  break;
                case 1:
                  P1_B1_State = 1;
                  break;
                case 2:
                  P1_B2_State = 1;
                  break;
                case 3:
                  P1_B3_State = 1;
                  break;
                case 4:
                  P1_B4_State = 1;
                  break;
                case 5:
                  P1_B5_State = 1;
                  break;
                case 6:
                  P1_B6_State = 1;
                  break;
                case 7:
                  P1_B7_State = 1;
                  break;
                case 8:
                  P1_B8_State = 1;
                  break;
                case 9:
                  P1_B9_State = 1;
                  break;
                case 10: //REMEMBER IN HEX e.g. 10 = 16 in HEX and A5 in HEX = 10 in DEC
                  P1_B10_State = 1;
                  break;
                case 11: //
                  P1_B11_State = 1;
                  break;
              }
              break;
          }
          break;
        /* ---------- Page 2 ----------*/
        case page2:                               //Case 2 means the data has come from page 2
          switch (HMI_read_data[type]) {
            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[index]);
              break;
            case 1:                           //Data from the Sensors on page 2
              switch (HMI_read_data[index]) {     //HMI_read_data[index] 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. (index in this case refers to the 5th section of HEX)
                case 0:
                  P2_B0_State = 1;
                  break;
                case 1: //Graph start button
                  P2_B1_State = 1;
                  P2_B2_State = 0;
                  ////serial.println(P2_B1_State);
                  break;
                case 2: //Graph pause button
                  P2_B2_State = 1;
                  P2_B1_State = 0;
                  //serial.println(P2_B2_State);
                  break;
              }
          }
          break;
      }
      Adjust_clock();
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i >= read_data_size) {
      HMI_read_data_i = read_data_size - 1;
    }
  }

}

void Adjust_clock() {
  if (P1_B0_State != 0) {
    //serial.println("clock set Hour + pushed");
    h++;
    if (h > 23) {
      h = 0;
    }
    rtc.setTime(h, m, s);
    P1_B0_State = 0;
    HMI_display_clock();
  }
  if (P1_B1_State != 0) {
    //serial.println("clock set Hour - pushed");
    h--;
    if (h < 0) {
      h = 23;
    }
    rtc.setTime(h, m, s);
    P1_B1_State = 0;
    HMI_display_clock();
  }
  if (P1_B2_State != 0) {
    //serial.println("clock set Min + pushed");
    m++;
    if (m > 59) {
      m = 0;
    }
    rtc.setTime(h, m, s);
    P1_B2_State = 0;
    HMI_display_clock();
  }
  if (P1_B3_State != 0) {
    //serial.println("clock set Min - pushed");
    m--;
    if (m < 0) {
      m = 59;
    }
    rtc.setTime(h, m, s);
    P1_B3_State = 0;
    HMI_display_clock();
  }
  if (P1_B4_State != 0) {
    //serial.println("clock set Sec + pushed ");
    s++;
    if (s > 59) {
      s = 0;
    }
    rtc.setTime(h, m, s);
    P1_B4_State = 0;
    HMI_display_clock();
  }
  if (P1_B5_State != 0) {
    //serial.println("clock set Sec - pushed ");
    s--;
    if (s < 0) {
      s = 59;
    }
    rtc.setTime(h, m, s);
    P1_B5_State = 0;
    HMI_display_clock();
  }
  if (P1_B6_State != 0) {
    //serial.println("date set day + pushed");
    dd++;
    if (dd > 31) {
      dd = 1;
    }
    rtc.setDate(dd, mm, yy);
    P1_B6_State = 0;
    HMI_display_clock();
  }
  if (P1_B7_State != 0) {
    //serial.println("date set day - pushed");
    dd--;
    if (dd < 1) {
      h = 31;
    }
    rtc.setDate(dd, mm, yy);
    P1_B7_State = 0;
    HMI_display_clock();
  }
  if (P1_B8_State != 0) {
    //serial.println("date set month + pushed");
    mm++;
    if (mm > 12) {
      mm = 1;
    }
    rtc.setDate(dd, mm, yy);
    P1_B8_State = 0;
    HMI_display_clock();
  }
  if (P1_B9_State != 0) {
    //serial.println("date set month - pushed");
    mm--;
    if (mm < 1) {
      mm = 12;
    }
    rtc.setDate(dd, mm, yy);
    P1_B9_State = 0;
    HMI_display_clock();
  }
  if (P1_B10_State != 0) {
    //serial.println("date set year + pushed");
    yy++;
    if (yy > 2099) {
      yy = 0;
    }
    rtc.setDate(dd, mm, yy);
    P1_B10_State = 0;
    HMI_display_clock();
  }
  if (P1_B11_State != 0) {
    //serial.println("date set year - pushed");
    yy--;
    if (yy < 0) {
      yy = 2099;
    }
    rtc.setDate(dd, mm, yy);
    P1_B11_State = 0;
    HMI_display_clock();
  }
}

void HMI_display_clock() {
  dat = rtc.getDateStr();
  tim = rtc.getTimeStr();
  Serial.print(F("t1.txt=\""));
  Serial.print(rtc.getTimeStr());
  endLine();        // Sends 0xff 0xff 0xff, which tell the Nextion that it has a complete string of data
  Serial.print(F("t2.txt=\""));
  Serial.print(tim);
  endLine();
  Serial.print(F("t3.txt=\""));
  Serial.print(dat);
  endLine();
  Serial.print(F("t4.txt=\""));
  Serial.print(dat);
  endLine();
  Serial.print(F("t5.txt=\""));
  Serial.print(rtc.getTemp());
  Serial.print(F("C"));
  endLine();
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 500;
  if (currentMillis - previousMillis >= interval) {
    previousMillis += interval;
    HMI_display_clock();
  }
}

void endLine() {
  Serial.print(F("\""));
  Serial.print(F("\xFF\xFF\xFF"));
}

void values() {
  hc = rtc.getTimeStr();
  hi = hc.substring(0, 2);
  mi = hc.substring(3, 5);
  si = hc.substring(6, 8);

  h = hi.toInt();
  m = mi.toInt();
  s = si.toInt();

  dc = rtc.getDateStr();
  ddi = dc.substring(0, 2);
  mmi = dc.substring(3, 5);
  yyi = dc.substring(6, 10);

  dd = ddi.toInt();
  mm = mmi.toInt();
  yy = yyi.toInt();
}

int modRt(int vu) {
  //serial.println(vu);
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  //  Serial2.print(F("t0.txt=\""));
  //  Serial2.print(F("This is page "));
  //  Serial2.print(page);
  //  endLine();
  //  Serial.println("Page");
}

void Sensor_read() {

  if (P2_B1_State != 0) {
    int sensor0Read = analogRead(sensor0);
    int sensor1Read = analogRead(sensor1);
    int sensor2Read = analogRead(sensor2);
    int sensor3Read = analogRead(sensor3);
    Serial.print("add 9,0,");
    readingScaled = map(sensor0Read, 0, 1200, 2, 198);
    Serial.print(readingScaled);
    //serial.println(sensor0Read);
    Serial.print(F("\xFF\xFF\xFF"));
    Serial.print(F("sense0.txt=\""));
    Serial.print(sensor0Read);
    endLine();
    Serial.print(F("sense1.txt=\""));
    Serial.print(sensor1Read);
    endLine();
    Serial.print(F("sense2.txt=\""));
    Serial.print(sensor2Read);
    endLine();
    Serial.print(F("sense3.txt=\""));
    Serial.print(sensor3Read);
    endLine();
    Nex_Button_Read();
  }
  else {
    return;
  }
}

Annoying that a lot of time was spent on such a simple change, but at least I won't be making this mistake again.

Thank you for your help @PerryBebbington i'm sure I will be back pestering about arrays :slightly_smiling_face:

OK, good.

Next to do:
Separate reading the sensors from sending to the Nextion, these should be in separate functions.
Consider how often you need to read the sensors, maybe a few times a second, and use millis() to only read that number of times.

Let me know how you get on with the Nano Every.

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