Problem with Arduino nano U8G2.lib and T6963 240X128

I’m trying to print data from software serial port to 240x128 display. 9 rows prints fine but then something happens and next rows are not printed ok. Is this some kind of memory problem or is the problem in my code. I have verified with debug messages to native serial port that data comes correct to arduino. I cannot use u8g2log because it does not support UTF8.
If i send full 40 character rows it will fail printing but if i send only few characters at row it maybe print all correct.

Thank you in advance

-Janne

Here is my code:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(16, 17); // RX, TX

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_T6963_240X128_1_8080 u8g2(U8G2_R0, 5, 6, 7, 8, 9, 10, 11, 12, /*enable=*/ 4, /*cs=*/ 15, /*dc=*/ 2, /*reset=*/ 14); // Connect RD with +5V, FS0 and FS1 with GND
String inputString = ""; // a string for incoming data
String MinputString = "";
String rivi01 = "";
String rivi02 = "";
String rivi03 = "";
String rivi04 = "";
String rivi05 = "";
String rivi06 = "";
String rivi07 = "";
String rivi08 = "";
String rivi09 = "";
String rivi10 = "";
String rivi11 = "";
String rivi12 = "";
String rivi13 = "";
String rivi14 = "";
String rivi15 = "";
String rivi16 = "";
boolean stringComplete = false;  // is string complete

void setup() {
 // setup code 
 // initialize serial:
  Serial.begin(9600);
 pinMode(3, OUTPUT); 
digitalWrite(3, HIGH); // /RD=HIGH 

 while (!Serial) {
  //  ; // wait native serial port connection
 }
  mySerial.begin(19200);
  //mySerial.println("Hello, world?");
  Serial.println("Hello, world?");
  // reserve 40 bytes for the inputString:
  inputString.reserve(40);

pinMode(13, OUTPUT); 
digitalWrite(13, LOW); //Backlight ON
u8g2.begin(); 
}

void loop() {
 
mySerialEvent();
 // tulosta kun uusrivi tulee
  if (stringComplete) {
    
    Serial.println(inputString);
    mySerial.println(inputString);
    MinputString=inputString;
        // clear the string:
    inputString = "";
    stringComplete = false;
    tulostus();
  }
}

// receive string for certain rows
   void tulostus() {
     
      if (MinputString.startsWith("#ROW01")) {
      rivi01=MinputString;
      rivi01.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW02")) {
      rivi02=MinputString;
      rivi02.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW03")) {
      rivi03=MinputString;
      rivi03.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW04")) {
      rivi04=MinputString;
      rivi04.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW05")) {
      rivi05=MinputString;
      rivi05.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW06")) {
      rivi06=MinputString;
      rivi06.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW07")) {
      rivi07=MinputString;
      rivi07.remove(0, 6);
      //mySerial.println("RIVI 7");
      //mySerial.println(rivi07);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW08")) {
      rivi08=MinputString;
      rivi08.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW09")) {
      rivi09=MinputString;
      rivi09.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW10")) {
      rivi10=MinputString;
      rivi10.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW11")) {
      rivi11=MinputString;
      rivi11.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW12")) {
      rivi12=MinputString;
      rivi12.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW13")) {
      rivi13=MinputString;
      rivi13.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#ROW14")) {
      rivi14=MinputString;
      rivi14.remove(0, 6);
      MinputString="";
      }
      if (MinputString.startsWith("#OUT")) {
           
      Serial.println("tulostus");
      TULOSTUSLCD();
      }
      if (MinputString.startsWith("#L_ON")) {
      digitalWrite(13, LOW); //Valo Päälle Backlight ON
      }    
      if (MinputString.startsWith("#L_OFF")) {
      digitalWrite(13, HIGH); // Valo Pois päältä Backlight OFF
      }  
   
  }
  
  void TULOSTUSLCD(){
   u8g2.firstPage();
 do {
u8g2.setFont(u8g2_font_6x12_t_symbols);
    u8g2.enableUTF8Print(); // enable skandinavian fonts
    u8g2.drawUTF8(0, 9, rivi01.c_str());
    u8g2.drawUTF8(0, 18, rivi02.c_str());
    u8g2.drawUTF8(0, 27, rivi03.c_str());
    u8g2.drawUTF8(0, 36, rivi04.c_str());
    u8g2.drawUTF8(0, 45, rivi05.c_str());
    u8g2.drawUTF8(0, 54, rivi06.c_str());
    u8g2.drawUTF8(0, 63, rivi07.c_str());
    u8g2.drawUTF8(0, 72, rivi08.c_str());
    u8g2.drawUTF8(0, 81, rivi09.c_str());
    u8g2.drawUTF8(0, 90, rivi10.c_str());
    u8g2.drawUTF8(0, 99, rivi11.c_str());
    u8g2.drawUTF8(0, 108, rivi12.c_str());
    u8g2.drawUTF8(0, 117, rivi13.c_str());
    u8g2.drawUTF8(0, 126, rivi14.c_str());
   // delay(1000);
    } while ( u8g2.nextPage() );
 //delay(1000);
    }
 void mySerialEvent() {
  while (mySerial.available()) {
    // ota vastaan uusi merkki
    char inChar = (char)mySerial.read();
    // lisää se tekstijonoon
    inputString += inChar;
    // jos uusrivi otetaan vastaan  merkki päälle
    if (inChar == '\n') {
      stringComplete = true;
    }
  }

}

Both! You’re using the String class on the AVR platform which is a bad idea as it used dynamic allocation (heap) which fragments the available memory too fast and sooner or later you run out of it resulting in unpredictable behavior.

Thank you for your answer! Any suggestions for doing it better way?

Use c-style strings (character arrays)! Declare the variable locally (stack) so no heap allocation occurs.

There is quite good support for c-strings (strncmp,strcat, etc.) but I would agree that the String class is more convenient.

Ok, thank you! I will try that.