static String message = ""; // Store the incoming message
while (Serial.available()) {
char c = Serial.read(); // Read a character from serial
if (c == '\n') { // If Enter key is pressed, clear LCD and display message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message.substring(0, 16)); // Display first line
if (message.length() > 16) {
lcd.setCursor(0, 1);
lcd.print(message.substring(16, 32)); // Display second line
}
message = ""; // Reset the message buffer
} else {
if (message.length() < 32) { // Limit message length
message += c;
}
}
}