Serial data concatenating somewhere, can't find where

Hey there,

I have two megas, one is a screen, one is a info collector.
The screen mega sends a request to info collector, info collector responds, data is compared and stripped, then it is displayed on screen. This is MOSTLY working correctly.
The only thing I am having issues with at the moment is that when I switch between pages, data from the previous page is concatenated to the data on the new page. For example, light reading on PAGE_ROOMREADINGS often displays 3 digits (771). When I switch to PAGE_RELAYREADINGS, the data displayed will be the correct data (a 0 or a 1), plus the last light reading (e.g. 071). I have tried clearing the CHAR_ARRAY, writing it with zeros, clearing every other string after the data has been displayed, but nothing is working.
Additional example:
Page Room Reading:
Temp: 22
Humidity: 49
Res Temp: 18
Light: 760
Then when I switch to Page Relay Reading (Where each relay should be a 0 or 1):
Relay 1: 02
Relay 2: 09
Relay 3: 08
Relay 4: 060

Can you spot why this is happening? Thank you!

int NEW_READING_AVAIL = 0;
String NEW_READING_DATA;
String mystring = "";
char character;
char CHAR_ARRAY[20];
long mymillis;

void setup(){
  Serial.begin(19200);
  Serial1.begin(1200);
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  myButtons.setTextFont(SmallFont);
  myButtons.setSymbolFont(Dingbats1_XL);
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  PAGE_MAIN();
  mymillis=millis();
}

void SERIAL_UPDATE(){
  if(Serial1.available()){
    character=Serial1.read();
    //Serial.print("Character: ");
    //Serial.println(character);
    mystring.concat(character);
    //Serial.print("Data: ");
    //Serial.println(mystring);
    process(mystring);
  }
}

void process(String DATA) {
  if(DATA.startsWith("@")&&DATA.endsWith("#")){
    DATA.remove(0,1);
    DATA.remove(DATA.indexOf("#",1));
    if(DATA.substring(0,3)=="RRR"){
      DATA.remove(0,3);
      NEW_READING_DATA=DATA;
      NEW_READING_AVAIL=1;
      mystring="";
    }else if(DATA.substring(0,3)=="RRS"){
      DATA.remove(0,3);
      NEW_READING_DATA=DATA;
      NEW_READING_AVAIL=1;
      mystring="";
    }else if(DATA.substring(0,3)=="RDS"){
      DATA.remove(0,3);
      NEW_READING_DATA=DATA;
      NEW_READING_AVAIL=1;
      mystring="";
    } 
  }
}

void PAGE_ROOMREADINGS(){
  myButtons.deleteAllButtons();
  myGLCD.clrScr();
  
  int butBack, pressed_button;
  
  myGLCD.setColor(255, 0, 0); 
  myGLCD.fillRect(0, 0, 319, 20); 
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.print("*** ControlBot V4.2.0 ***", CENTER, 3);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print("ROOM READINGS", CENTER, 30);
  myGLCD.print("Temperature: ", 10, 50);
  myGLCD.print("Humidity: ", 10, 70);
  myGLCD.print("Res Temperature: ", 10, 90);
  myGLCD.print("Light: ", 10, 110);
  myGLCD.print("MQ2: ", 10, 130);
  
  butBack = myButtons.addButton( 10, 190, 300,  30, "Back");
  myButtons.drawButtons();
  
  while(1){
    if(millis()-mymillis>1000){
      Serial1.write("@RRR#");
      mymillis=millis();
    }
    SERIAL_UPDATE();
    if(NEW_READING_AVAIL==1){
      int i=0;
      NEW_READING_DATA.toCharArray(CHAR_ARRAY,20);
      char *p=CHAR_ARRAY;
      char *str;
      while((str=strtok_r(p,",",&p))!=NULL){
        switch (i){
        case 0: 
          myGLCD.print(str,110,50);
          i++;
          break;
        case 1:
         myGLCD.print(str,85,70);
          i++;
          break;
        case 2:
          myGLCD.print(str,140,90);
          i++;
          break;
        case 3:
          myGLCD.print(str,65,110);
          i++;
          break;
        case 4:
          myGLCD.print(str,45,130);
          i++;
          break;
        }
        
      }
      NEW_READING_DATA="";
      NEW_READING_AVAIL=0;
    }
    if(myTouch.dataAvailable()==true){
      pressed_button=myButtons.checkButtons();
      if(pressed_button==butBack){
        NEW_READING_DATA="";
        NEW_READING_AVAIL=0;
        PAGE_READINGS();
      }
    }
  }
}

void PAGE_RELAYREADINGS(){
  myButtons.deleteAllButtons();
  myGLCD.clrScr();
  
  int butRelay1, butRelay2, butRelay3, butRelay4, butTimer1,butTimer2,butTimer3,butTimer4, butBack, pressed_button;
  
  myGLCD.setColor(255, 0, 0); 
  myGLCD.fillRect(0, 0, 319, 20); 
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.print("*** ControlBot V4.2.0 ***", CENTER, 3);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print("RELAY READINGS", CENTER, 30);
  myGLCD.print("Relay 1: ", 10, 60);
  myGLCD.print("Relay 2: ", 90, 60);
  myGLCD.print("Relay 3: ", 170, 60);
  myGLCD.print("Relay 4: ", 250, 60);
  
  butBack = myButtons.addButton( 10, 190, 300,  30, "Back");
  butRelay1 = myButtons.addButton(20, 95,  40,  40, "4", BUTTON_SYMBOL);
  butRelay2 = myButtons.addButton(100, 95,  40,  40, "4", BUTTON_SYMBOL);
  butRelay3 = myButtons.addButton(180, 95,  40,  40, "4", BUTTON_SYMBOL);
  butRelay4 = myButtons.addButton(260, 95,  40,  40, "4", BUTTON_SYMBOL);
  butTimer1 = myButtons.addButton(20, 140,  40,  40, "W", BUTTON_SYMBOL);
  butTimer2 = myButtons.addButton(100, 140,  40,  40, "W", BUTTON_SYMBOL);
  butTimer3 = myButtons.addButton(180, 140,  40,  40, "W", BUTTON_SYMBOL);
  butTimer4 = myButtons.addButton(260, 140,  40,  40, "W", BUTTON_SYMBOL);
  myButtons.drawButtons();

  while(1){
    if(millis()-mymillis>1000){
      Serial1.write("@RRS#");
      mymillis=millis();
    }
    SERIAL_UPDATE();
    if(NEW_READING_AVAIL==1){
      int i=0;
      NEW_READING_DATA.toCharArray(CHAR_ARRAY,20);
      char *p = CHAR_ARRAY;
      char *str;
      while((str=strtok_r(p,",",&p))!=NULL){
        switch (i){
        case 0: 
          myGLCD.print(str, 35, 75);
          i++;
          break;
        case 1:
          myGLCD.print(str, 115, 75);
          i++;
          break;
        case 2:
          myGLCD.print(str, 195, 75);
          i++;
          break;
        case 3:
          myGLCD.print(str, 275,75);
          i++;
          break;
        }
      }
      NEW_READING_AVAIL=0;
      NEW_READING_DATA="";
    }
    if(myTouch.dataAvailable()==true){
      pressed_button=myButtons.checkButtons();
      if(pressed_button==butBack){
        PAGE_READINGS();
      }
    }
  }
}

I know it's not going to solve your bug, but why are you naming those functions in all capital letters? and other places you have variables with capital letters. Usually they're used for #define constants. Makes it really hard to read.

Also where is your main loop()?

Also I'd probably recommend ditching the String class and use char arrays.

tammytam:
I know it's not going to solve your bug, but why are you naming those functions in all capital letters? and other places you have variables with capital letters. Usually they're used for #define constants. Makes it really hard to read.

Also where is your main loop()?

Also I'd probably recommend ditching the String class and use char arrays.

Sorry! It's just a habit I got in to. This is just a select amount of the code (the rest of the code does not come into play unless on a different screen). The main loop just consists of SERIAL_UPDATE, and isn't really used as each of the pages has a while(1) loop. I'v tried with char arrays but I can't get the hang of them, but I have heard it would probably make it a lot easier. Thank you for your suggestions!

It looks like you are not clearing the previous values from the screen before printing the new ones so you print say 49 at a screen position then print 0 at the same position and see 09. To test this theory try printing an identifier character, perhaps !, after each value. Do you now get 0! on the second screen ?

UKHeliBob:
It looks like you are not clearing the previous values from the screen before printing the new ones so you print say 49 at a screen position then print 0 at the same position and see 09. To test this theory try printing an identifier character, perhaps !, after each value. Do you now get 0! on the second screen ?

Hey UKHeliBob,
That is unfortunately not the case. Screen 1 (page relay) has a horizontal lettering layout (words are laid out across the screen with values underneath), where as Screen 2 (page_relay) has a vertical layout (words are laid out down the screen with values to the right). This means that the screen positioning can not just have the characters overlapping (purposely done this because I had the same theory!). They are being fetched from somewhere, I just can't figure out where, how or why!

Further thought: By printing NEW_READING_DATA to serial on screen load, I see the following:
,22,50,19,755,127
,0,0,0,0
,0,0,0,0
,0,0,0,0
,0,0,0,0

So you are correct UKHeliBob, it is overlapping.
The first reading that comes through when a screen is loaded is that of the previous screen, but I have no idea where it is being stored.

      while((str=strtok_r(p,",",&p))!=NULL){

Why are you using the thread-safe, re-entrant version of a function on a single threaded system? The strtok() version is SO much simpler to use.

Storing data in strings AND Strings is stupid. Pick ONE format for storing the data. Do NOT go copying data back and forth. That way lies madness.