LCD Updating Information with Serial Communication

Hello, this is my first time posting and as well as working with Arduino LCD Display.
I am trying to create this status update screen, where if serial communication is established the screen will read "ONLINE" and if its not then "OFFLINE".

void systemconnection(boolean online){
    int maxStringLength = 158; //String Pixel Width
    int maxStringWidth = 8; //String Pixel Height
    tft.fillRect(50, 50, maxStringLength, maxStringWidth,ILI9341_BLACK); //draw over previous status update
    tft.setCursor(50,50);

    if (online){
      //status online
      tft.setTextColor(ILI9341_WHITE);
      tft.print("CONNECTION: ");

      tft.setTextColor(ILI9341_GREEN);
      tft.print("ONLINE");
    }
    else if (!online){
      //status online
      tft.setTextColor(ILI9341_WHITE);
      tft.print("CONNECTION: ");

      tft.setTextColor(ILI9341_RED);
      tft.print("OFFLINE");

    }

}
tft.fillRect(50, 50, maxStringLength, maxStringWidth,ILI9341_BLACK); //draw over previous

The problem here is that when communication is restablished or disconnected the print statements overwrite each other, I call this function here,

void gloveInterface() {
  // Set Strings
  int receivedData[5];

  tft.fillScreen(ILI9341_BLACK);
  String thumb = "Thumb: ";
  String index = "Index: ";
  String middle = "Middle: ";
  String ring = "Ring: ";
  String pinky = "Pinky: ";
  
  while (true) {  
    if (Serial.available()) {
      //Read the received data
      Serial.readBytes((byte*)receivedData, sizeof(receivedData));
      // SYSTEM ONLINE
      systemconnection(true);
      
      // Clear region for updated data
      tft.fillRect(195, centerTextHeight(-40) - 10, 60, 20, ILI9341_BLACK);
      tft.fillRect(195, centerTextHeight(-20) - 10, 60, 20, ILI9341_BLACK);
      tft.fillRect(195, centerTextHeight(0) - 10, 60, 20, ILI9341_BLACK);
      tft.fillRect(195, centerTextHeight(20) - 10, 60, 20, ILI9341_BLACK);
      tft.fillRect(195, centerTextHeight(40) - 10, 60, 30, ILI9341_BLACK);

      // Thumb
      tft.setTextColor(ILI9341_WHITE);
      tft.setCursor(centerTextWidth(thumb, 12), centerTextHeight(40));
      tft.println(thumb + String(receivedData[0]/256));

      // Index
      tft.setTextColor(ILI9341_WHITE);
      tft.setCursor(centerTextWidth(index, 12), centerTextHeight(-40));
      tft.println(index + String(receivedData[1]/256));

      // Middle
      tft.setTextColor(ILI9341_WHITE);
      tft.setCursor(centerTextWidth(middle, 12), centerTextHeight(-20));
      tft.println(middle + String(receivedData[2]/256));

      // Ring
      tft.setTextColor(ILI9341_WHITE);
      tft.setCursor(centerTextWidth(ring, 12), centerTextHeight(0));
      tft.println(ring + String(receivedData[3]/256));

      // Pinky
      tft.setTextColor(ILI9341_WHITE);
      tft.setCursor(centerTextWidth(pinky, 12), centerTextHeight(20));
      tft.println(pinky + String(receivedData[4]/256));
    }else{
        systemconnection(false);
    }
  }
}

In this line I am trying to overwrite what was previously on display but would the lcd.clear() function work better. Is there better way I can update information without the previous message interfereing.

Try overwriting the old data with old data in background color, then write new data in foreground color.

Added this chunk of code:

void systemconnection(boolean online){
    int maxStringLength = 158; //String Pixel Width
    int maxStringWidth = 8; //String Pixel Height
    //tft.fillRect(50, 50, maxStringLength, maxStringWidth,ILI9341_BLACK); //draw over previous status update
    tft.setCursor(50,50);

    if (online){
      //status online
      tft.setTextColor(ILI9341_BLACK);
      tft.print("CONNECTION: OFFLINE");
      tft.setCursor(50,50);

      tft.setTextColor(ILI9341_WHITE);
      tft.print("CONNECTION: ");

      tft.setTextColor(ILI9341_GREEN);
      tft.print("ONLINE");
    }
    else if (!online){
      //status online
    
      tft.setTextColor(ILI9341_BLACK);
      tft.print("CONNECTION: ONLINE");
       tft.setCursor(50,50);
      ///////////////////////////////////////////
     
      tft.setTextColor(ILI9341_WHITE);
      tft.print("CONNECTION: ");

      tft.setTextColor(ILI9341_RED);
      tft.print("OFFLINE");

    }

}

once I lose connection, unplug either the tx/rx pin it works fine but when the connection is reestablished the two print statements overlap and the screen flashes between both.

In "online" set a flag to say "we've already written this, don't write it again" something like:

if (online) {
  if (!been_here) { // if we have not written the tft text before, then proceed to do it
    been_here = 1; // this says, "don't pass the if() condition in the line above"
    tft.setTextColor...
.
.
  }
}

AND in "!online" clear that flag

else if (!online) {
  been_here = 0; // clear the "online" flag so if that happens, the tft text will be written
  tft.setTextColor...
.
.
}

I did something along the lines of that but on boot up there is constant flash between the two still, I am pretty sure the fact that I am using serial.available() is making the message flash between the two, is there a buffer between when data is being sent that I can account for?

My guess is the flashing is from the constant over-writing on the tft... maybe caused by "online" and "not online" are occurring very (too) frequently. Try to make the over-writing happen only once (both "online" and "not online") until the other state has occurred. If your online/not online is rapidly changing, the problem is not in tft over-writing.

i fixed the problem i created a time buffer between when serial is available so that if check flag is false and the difference between when check flags have changed were less than 1 second then it wouldn't switch. I guess there is a small gap between when the data was being sent so it would switch between the two because there was a 10ms where it wasn't available.

unsigned long offlineTime = 0;
  offlineTime = millis(); //set this at the end of serial.available()
    }
    else{
      if (!stopLoop && (millis() - offlineTime > 300)){ //the conditions
        systemconnection(false);
      }
      
    }

Very good!

1 Like

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