Need help controlling multiple external loads with Nextion/Arduino Set-Up

Hello,

I have been struggling with the Nextion for a while now and I finally found a code that works… sort of. The code below works for controlling a single button to switch on and off an LED through the Arduino mega. However, I need a code that will work for multiple loads simultaneously (i.e. independent switches). I got this code from an instructables page whose link is below as well. I have tried using multiple if else statements and cases but nothing seems to work. I do see the other buttons come in on the serial monitor, however, with their respective strings. If anyone can please help me format this code to control multiple loads (i.e. 3 LED’s) it would be very much appreciated.

/*
This code is based on the software serial example on Arduino's website

 */
#include <SoftwareSerial.h> //Include the library
bool button = false; //will store the button state

SoftwareSerial mySerial(10, 11); // RX, TX

String message;

void setup() {
  // Open serial communications and wait for port to open:
  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");
  pinMode(13, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() { // run over and over
  while (!mySerial.available()){ //While no data is coming in from the Nextion
    if (message.length() > 14){ //This was the easiest way for me to check that the whole message has been received. Some messages might be longer, but the shortest ones are 15 characters.
      Serial.println(message);   //See what the entire message that the Arduino receives from the Nextion
      Serial.print("button state: "); 
      Serial.println(button); //See what state the button is in
      
      Serial.print("message[3]: "); //according to the Nextion message structure, item 4 will be the page number.
      Serial.println(message[3]); //See what message[3] (the fourth character in the message) is

      Serial.print("message[4]: "); //according to the Nextion message structure, item5 will be the component number.
      Serial.println(message[4]); //See what message[4] (the fifth character in the message) is
      
      if (button == false && message[3] == '1' && message[4] == '6' ){ //Test that the button is off and that it was indeed this button that has been pressed
        button = true; //The button is now pressed
        digitalWrite(13, HIGH); //Switch on the led
        Serial.println("case 1"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
      else{ //Else the button is on and that it was indeed this button that has been pressed
        button = false; //The button has been deactivated 
        digitalWrite(13, LOW); //Switch the led off
        Serial.println("case 2"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise we might get mixed messages       
      }      
    }
  }
  
  while (mySerial.available()){ //Is data coming through the serial from the Nextion?
    message.concat(mySerial.read()); //If so, construct the message
    } 
}

Please read Serial Input Basics and it will show you a much better way to receive messages through Serial and then deal with them once a complete message has arrived.

I was able to figure it out. My final code is posted below in case anyone needs a reference. Thank you for your help.

/*
This code is based on the software serial example on Arduino's website

 */
#include <SoftwareSerial.h> //Include the library
bool LOCK = false; //will store the button state
bool LED = false;
bool FAN = false;

SoftwareSerial mySerial(10, 11); // RX, TX

String message;

void setup() {
  // Open serial communications and wait for port to open:
  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");
  pinMode(13, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() { // run over and over
  while (!mySerial.available()){ //While no data is coming in from the Nextion
    if (message.length() > 14){ //This was the easiest way for me to check that the whole message has been received. Some messages might be longer, but the shortest ones are 15 characters.
      Serial.println(message);   //See what the entire message that the Arduino receives from the Nextion
      Serial.print("LOCK state: "); 
      Serial.println(LOCK); //See what state the button is in
      Serial.print("LED state: ");
      Serial.println(LED);
      Serial.print("FAN state: ");
      Serial.println(FAN);
      
      Serial.print("message[3]: "); //according to the Nextion message structure, item 4 will be the page number.
      Serial.println(message[3]); //See what message[3] (the fourth character in the message) is

      Serial.print("message[4]: "); //according to the Nextion message structure, item5 will be the component number.
      Serial.println(message[4]); //See what message[4] (the fifth character in the message) is
      
      if (LOCK == false && message[3] == '1' && message[4] == '6' ){ //Test that the button is off and that it was indeed this button that has been pressed
        LOCK = true; //The button is now pressed
        digitalWrite(13, HIGH); //Unlock
        delay(3000);
        digitalWrite(13, LOW); //Lock
        LOCK = false; //The button is now off
        Serial.println("Unlocked Shortly"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
      if (LED == false && message[3] == '1' && message[4] == '7' ){ //Test that the button is off and that it was indeed this button that has been pressed
        LED = true; //The button is now pressed
        digitalWrite(8, HIGH); //Switch on the led
        Serial.println("LED On"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
      if (FAN == false && message[3] == '1' && message[4] == '8' ){ //Test that the button is off and that it was indeed this button that has been pressed
        FAN = true; //The button is now pressed
        digitalWrite(4, HIGH); //Switch on the fan
        Serial.println("Fan On"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
      if (LED == true && message[3] == '1' && message[4] == '7' ){ //Test that the button is off and that it was indeed this button that has been pressed
        LED = false; //The button is now pressed
        digitalWrite(8, LOW); //Switch off the led
        Serial.println("LED Off"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
      if (FAN == true && message[3] == '1' && message[4] == '8' ){ //Test that the button is off and that it was indeed this button that has been pressed
        FAN = false; //The button is now pressed
        digitalWrite(4, LOW); //Switch off the led
        Serial.println("Fan Off"); //For debugging. Check that the 'if' was entered
        message = ""; //Clear the message, otherwise the next if will also be activated
      }
    }
  }
  
  while (mySerial.available()){ //Is data coming through the serial from the Nextion?
    message.concat(mySerial.read()); //If so, construct the message
    } 
}