Avoid use of too many "String"s

Hello there!!

Currently i am working on a project where I am supposed to accept the inputs from the serial monitor in format <Chiran, 99, 99.9,....>(so on these values are just for example but the format would remain same) and load it on to DMD display. I am using 1 down 4 across DMD mono color. My problem is my code makes use too many of "String" and I don't know how to reduce them or is there any way to achieve it by minimum use of "String" also I am not quite sure if making use of "String these many times creates any problems or not. any help? Thanks in advance.

#include "SPI.h"        
#include "DMD.h"        
#include "TimerOne.h"
#include "Arial_black_16.h" 

#define DISPLAYS_ACROSS 4
#define DISPLAYS_DOWN 1
DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};

int int_DD = 0;
int int_MM = 0;
int int_YY = 0;

int int_HR = 0;
int int_Min= 0;
int int_Sec= 0;

int int_Total_FB = 0;
int int_Total_Air= 0;
int int_Total_toilt = 0;
int int_Total_service = 0;

int int_Air_avg = 0;
int int_toilt_avg = 0;
int int_service_avg = 0;
int int_total_avg = 0;

boolean newData = false;
String mac;

String date;
String times;

String Total_fb;
String Total_Air;
String Total_toilt;
String Total_Service;

String Air_avg;
String Toilt_avg;
String Service_avg;
String Total_avg;

String ScrollDate = "Date: ";
String ScrollSemi = ":";
String ScrollTime = "Time: ";

String ScrollText1 = "Total Feedback: ";
String ScrollText2 = "Total Air Feedback: ";
String ScrollText3 = "Total Toilet Feedback: ";
String ScrollText4 = "Total Service Feedback: ";

String ScrollText5 = "Total Average Feedback: ";
String ScrollText6 = "Total Average Feedback for Air: ";
String ScrollText7 = "Total Average Feedback for Toilet: ";
String ScrollText8 = "Total Average Feedback for Overall Service: ";

void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
       Timer1.initialize( 5000 );           
   Timer1.attachInterrupt( ScanDMD );  
   dmd.clearScreen( true );            

}


void drawText( String dispString ) 
{
  dmd.clearScreen( true );
  dmd.selectFont( Arial_Black_16 );
  char newString[256];
  int sLength = dispString.length();
  dispString.toCharArray( newString, sLength+1 );
  dmd.drawMarquee( newString , sLength , ( 32*DISPLAYS_ACROSS )-1 ,0);
  long start=millis();
  long timer=start;
  long timer2=start;
  boolean ret=false;
  while( !ret ){
    if ( ( timer+50 ) < millis() ) {
      ret=dmd.stepMarquee( -1 , 0 );
      timer=millis();
    }
  }
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        
        newData = false;
    }
    drawText(messageFromPC);
        drawText(mac);
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_DD = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    int_MM = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_YY = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_HR = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_Min = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_Sec = atoi(strtokIndx);     // convert this part to a float



    strtokIndx = strtok(NULL, ",");
    int_Total_FB = atoi(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_Total_Air = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ",");
    int_Total_toilt = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_Total_service = atoi(strtokIndx);     // convert this part to a float
    
    
    
    strtokIndx = strtok(NULL, ",");
    int_Air_avg = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_toilt_avg = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_service_avg = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_total_avg = atoi(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);

    //day
    Serial.print("Day ");
    Serial.println(int_DD);
    
    //month
    Serial.print("Month ");
    Serial.println(int_MM);
    
    //year
    Serial.print("Year ");
    Serial.println(int_YY);
    
    //hour
    Serial.print("Hour ");
    Serial.println(int_HR);
    
    //min
    Serial.print("Mins ");
    Serial.println(int_Min);
    
    //sec
    Serial.print("Seconds ");
    Serial.println(int_Sec);
    
    //int_Total_FB
    Serial.print("int_Total_FB");
    Serial.println(int_Total_FB);
    
    //int_Total_Air
    Serial.print("int_Total_Air ");
    Serial.println(int_Total_Air);
    
    //int_Total_toilt
    Serial.print("int_Total_toilt ");
    Serial.println(int_Total_toilt);
    
    //int_Total_service
    Serial.print("int_Total_service ");
    Serial.println(int_Total_service);
    
    //int_Air_avg
    Serial.print("int_Air_avg ");
    Serial.println(int_Air_avg);
    
    //int_toilt_avg
    Serial.print("int_toilt_avg ");
    Serial.println(int_toilt_avg);
    
    //int_service_avg
    Serial.print("int_service_avg ");
    Serial.println(int_service_avg);
    
    //int_total_avg
    Serial.print("int_total_avg ");
    Serial.println(int_total_avg);
/*
    mac = String(integerFromPC);
    Serial.print("Mac:");
    Serial.println(mac);
*/
 date = String(ScrollDate + int_DD + ScrollSemi + int_MM + ScrollSemi + int_YY);
 times = String(ScrollTime + int_HR + ScrollSemi + int_Min + ScrollSemi + int_Sec);

       Serial.print("date:");
       Serial.println(date);
       /*
 Total_fb = String();
String Total_Air;
String Total_toilt;
String Total_Service;
-
String Air_avg;
String Toilt_avg;
String Service_avg;
String Total_avg;
*/
}

The usual advice is to use c strings and do NOT use String unless it is needed by some library call - and use it right there and nowhere else.

You do not write what Arduino you are using. String may work if you have an Arduino with a lot of memory.

You can replace all those String variables by character arrays; e.g.

char ScrollDate[] = "Date: ";

When you print, you do not have to print everything in one go; below works just as well

Serial.print(ScrollDate);
Serial.print(int_DD);
Serial.print(ScrollSemi);
...
...
Serial.println()

vaj4088:
The usual advice is to use c strings and do NOT use String unless it is needed by some library call - and use it right there and nowhere else.

You do not write what Arduino you are using. String may work if you have an Arduino with a lot of memory.

I am pretty new to use of strings. It would be more helpful if u could put some more light on how these c string and String are different and how and when to use which one. If theres any link which I can refer to please point me towards it. Sorry, I forgot to mention, I am using Arduino Nano or I can even use Uno as well, I have bothe the options open for me.

sterretje:
You can replace all those String variables by character arrays; e.g.

char ScrollDate[] = "Date: ";

When you print, you do not have to print everything in one go; below works just as well

Serial.print(ScrollDate);

Serial.print(int_DD);
Serial.print(ScrollSemi);
...
...
Serial.println()

I wanted to print it all in one go just to see if prints like that or not bcoz if it does I have to print everything like that on DMD display. So if it prints like that on serial monitor then I just need to pass the variable "date" to the function which will display it on the DMD. But sadly that didn't worked. Can u tell me what changes should I make to it?

Thanks to both of u guys for taking time to reply me...My heartiest thanks

You may want to read this Are Strings really that Evil? – Arduino++

All those different Strings that you declare in the beginning are not being manipulated, and as long as they aren't, there is no fragmentation issue. But !! what you are doing here

/*
    mac = String(integerFromPC);
    Serial.print("Mac:");
    Serial.println(mac);
*/
 date = String(ScrollDate + int_DD + ScrollSemi + int_MM + ScrollSemi + int_YY);
 times = String(ScrollTime + int_HR + ScrollSemi + int_Min + ScrollSemi + int_Sec);

will cause fragmentation which may result in etc. etc. unexpectedly etc etc. Looking through your code i just see 'Robin's Serial Input Basics' and that just uses c-strings, so you know it is not that hard. Also in void drawText( String dispString ) you take a String and then convert it to a c-string (inefficiently at that, it should be char newString[dispString.length()+1]; ) so you don't need the String, but if you could use just 'const String' no fragmentation can occur. If you start running low on Program memory, eradicating all Strings should free some up, since if there is no call to the String class, it will not need to be compiled at all.

the Arduino doc lightly describes the differences between cstring and the String class

Thanks for all those wonderful and helpful suggestions. I am going through each of the links you guys provided me. Meanwhile I re wrote the code as following:

// Example 5 - Receive with start- and end-markers combined with parsing

#include "SPI.h"        
#include "DMD.h"        
#include "TimerOne.h"
#include "Arial_black_16.h" 

#define DISPLAYS_ACROSS 4
#define DISPLAYS_DOWN 1
DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};

int int_DD = 0;
int int_MM = 0;
int int_YY = 0;
int int_HR = 0;
int int_MI = 0;
int int_SE = 0;

int int_Total_FB = 0;
int int_Total_Air= 0;
int int_Total_toilt = 0;
int int_Total_service = 0;

boolean newData = false;

String date = String();
String times = String();
String totalFB = String();
String totalAir = String();
String totalToilet = String();
String totalService = String();

String ScrollDate = "going on Date: ";
String ScrollTime = "going on Time: ";
String ScrollMsg1 = "Total feedback recorded till now: ";
String ScrollMsg2 = "Total Air feedback recorded till now: ";
String ScrollMsg3 = "Total Toilet feedback recorded till now: ";
String ScrollMsg4 = "Total Service feedback recorded till now: ";

String ScrollSemi = ":";

void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
       Timer1.initialize( 5000 );           
   Timer1.attachInterrupt( ScanDMD );  
   dmd.clearScreen( true );            

}


void drawText( String dispString ) 
{
  dmd.clearScreen( true );
  dmd.selectFont( Arial_Black_16 );
  char newString[256];
  int sLength = dispString.length();
  dispString.toCharArray( newString, sLength+1 );
  dmd.drawMarquee( newString , sLength , ( 32*DISPLAYS_ACROSS )-1 ,0);
  long start=millis();
  long timer=start;
  long timer2=start;
  boolean ret=false;
  while( !ret ){
    if ( ( timer+50 ) < millis() ) {
      ret=dmd.stepMarquee( -1 , 0 );
      timer=millis();
    }
  }
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        
        newData = false;
    }
    drawText(messageFromPC);

}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_DD = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    int_MM = atoi(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ",");
    int_YY = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_HR = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_MI = atoi(strtokIndx);     // convert this part to a float
    
    
    strtokIndx = strtok(NULL, ",");
    int_Total_FB = atoi(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_Total_Air = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ",");
    int_Total_toilt = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_Total_service = atoi(strtokIndx);     // convert this part to a float
}

//============

void showParsedData() 
{
    Serial.println("INPUTS:");
    Serial.print("Message ");
    Serial.println(messageFromPC);

    //day
    Serial.print("Day ");
    Serial.println(int_DD);
    //Month
    Serial.print("Month ");
    Serial.println(int_MM);
    //Year
    Serial.print("Year ");
    Serial.println(int_YY);
    //Hours
    Serial.print("Hours ");
    Serial.println(int_HR);
    //minutes
    Serial.print("Minutes ");
    Serial.println(int_MI);
    //totalFB
    Serial.print("Total FB: ");
    Serial.println(int_Total_FB);
    //totalAir
    Serial.print("Total Air FB: ");
    Serial.println(int_Total_Air);
    //totalToilet
    Serial.print("Total Toilet FB: ");
    Serial.println(int_Total_toilt);
    //totalService
    Serial.print("Total Service FB: ");
    Serial.println(int_Total_service);
    
    Serial.println();
    Serial.println();
    
    Serial.println("OUTPUTS:");
    
 date = ScrollDate + int_DD + ScrollSemi + int_MM + ScrollSemi + int_YY; //+ ScrollSemi + MM + ScrollSemi + YY);
times = ScrollTime + int_HR + ScrollSemi + int_MI;
totalFB = ScrollMsg1 + int_Total_FB;
totalAir= ScrollMsg2 + int_Total_Air;
totalToilet = ScrollMsg3 + int_Total_toilt;
totalService = ScrollMsg4 + int_Total_service;

   Serial.println(date);
   Serial.println(times);
   Serial.println();
   Serial.println(totalFB);
   Serial.println(totalAir);
   Serial.println(totalToilet);
   Serial.println(totalService);
}

I dont know why but on serial monitor I only get the output for "totalToilet" but "totalService seems to get completely ignored what could be the reason? Have I ran out of the mem?

Following is the output I get on Serial monitor

This demo expects 3 pieces of data - text, an integer and a floating point value
Enter data in this style <HelloWorld, 12, 24.7>  

INPUTS:
Message May
Day 12
Month 5
Year 2019
Hours 8
Minutes 1
Total FB: 6
Total Air FB: 7
Total Toilet FB: 8
Total Service FB: 9


OUTPUTS:
going on Date: 12:5:2019
going on Time: 8:1

Total feedback recorded till now: 6
Total Air feedback recorded till now: 7
8
9

I need to add more parameters in this code but it seems that I am not able to get this right till here yet so I haven't added the remaining parameters. Any help Suggestions with this? Thanks in advance

Count:
Have I ran out of the mem?

Possibly yes. or at least out of space using the String class. You are aware that you don't need to create an extra String for each output line ? also try putting the initial Strings as const, or even better, do something with all the advice since if you use the 'String' class like this, stuff is going to get lost.

you receive everything without using the String class, why do you feel obliged to add some afterwards... Not sure you need the temp buffer, there is no harm really in replacing the commas with '\0' as you parse since what you are interested in are really the items you received which you duplicate anyway in messageFromPC and your numbers variables

J-M-L:
you receive everything without using the String class, why do you feel obliged to add some afterwards... Not sure you need the temp buffer, there is no harm really in replacing the commas with '\0' as you parse since what you are interested in are really the items you received which you duplicate anyway in messageFromPC and your numbers variables

It would be more helpful if you can please elaborate this point. this is the first time I am working with the string class and its aspects so its really confusing for me.

Meanwhile I also tried using c string approach following is the code I used for it but I guess I ran into another problem

// Example 5 - Receive with start- and end-markers combined with parsing

#include "SPI.h"        
#include "DMD.h"        
#include "TimerOne.h"
#include "Arial_black_16.h" 

#define DISPLAYS_ACROSS 4
#define DISPLAYS_DOWN 1
DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
char messageFromPC[numChars] = {0};

int int_DD = 0;
int int_YY = 0;

int int_HR = 0;
int int_MI = 0;

int int_Total_FB = 0;
int int_Total_Air= 0;
int int_Total_toilt = 0;
int int_Total_service = 0;

int int_Air_avg = 0;
int int_toilt_avg = 0;
int int_service_avg = 0;
int int_total_avg = 0;

boolean newData = false;

String date = String();
String times = String();
String totalFB = String();
String totalAir = String();
String totalToilet = String();
String totalService = String();

String air_avg = String();
String  

String ScrollDate = "going on Date: ";
String ScrollTime = "going on Time: ";
String ScrollMsg1 = "Total feedback recorded till now: ";
String ScrollMsg2 = "Total Air feedback recorded till now: ";
String ScrollMsg3 = "Total Toilet feedback recorded till now: ";
String ScrollMsg4 = "Total Service feedback recorded till now: ";

String ScrollSemi = ":";

void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}

//============

void setup()
{
    Serial.begin(9600);
    Timer1.initialize( 5000 );           
   Timer1.attachInterrupt( ScanDMD );  
   dmd.clearScreen( true );            

}


void drawText( String dispString ) 
{
  dmd.clearScreen( true );
  dmd.selectFont( Arial_Black_16 );
  char newString[256];
  int sLength = dispString.length();
  dispString.toCharArray( newString, sLength+1 );
  dmd.drawMarquee( newString , sLength , ( 32*DISPLAYS_ACROSS )-1 ,0);
  long start=millis();
  long timer=start;
  long timer2=start;
  boolean ret=false;
  while( !ret ){
    if ( ( timer+50 ) < millis() ) {
      ret=dmd.stepMarquee( -1 , 0 );
      timer=millis();
    }
  }
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        
        newData = false;
    }
    drawText(messageFromPC);

}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() 
{      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_DD = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    int_YY = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_HR = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_MI = atoi(strtokIndx);     // convert this part to a float
    
    
    strtokIndx = strtok(NULL, ",");
    int_Total_FB = atoi(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    int_Total_Air = atoi(strtokIndx);     // convert this part to an integer
    
    strtokIndx = strtok(NULL, ",");
    int_Total_toilt = atoi(strtokIndx);     // convert this part to a float
    
    strtokIndx = strtok(NULL, ",");
    int_Total_service = atoi(strtokIndx);     // convert this part to a float
}

//============

void showParsedData() 
{
 date = ScrollDate + int_DD + messageFromPC + int_YY; //+ ScrollSemi + MM + ScrollSemi + YY);
 times = ScrollTime + int_HR + ScrollSemi + int_MI;
 totalFB = ScrollMsg1 + int_Total_FB;
 totalAir= ScrollMsg2 + int_Total_Air;
 totalToilet = ScrollMsg3 + int_Total_toilt;
 totalService = ScrollMsg4 + int_Total_service;

   Serial.println(date);
   Serial.println(times);
   Serial.println(totalFB);
   Serial.println(totalAir);
   Serial.println(totalToilet);
   Serial.println(totalService);
}

I guess I need to convert those integers to char to get this thing working right. If m right about this how should I do it?

can you take a step back and explain

  • what is the exact format of the data you receive on Serial and who is sending it
  • what are you trying to do with that data

J-M-L:
can you take a step back and explain

  • what is the exact format of the data you receive on Serial and who is sending it
  • what are you trying to do with that data
  1. the exact format of the received data is like <text, int1, int2, int3, int4, int5, int6, int7, int8, int9>

where:
text is anything random text
int1, int2, int3 holds integer as date : DD, MM, YY
int4, int5 holds time as HR, Mins
int6, int7, int8, int9 holds the integer of some mathematical calculations done by other arduino mega and sent to this arduino UNO/Nano via serial which will be after concatenation will be sent to DMD display

  1. The main purpose of this data handling is to fetch it from the another mega via serial and concatenate it and then send it to DMD display

just thinking aloud:

Can't the other MEGA send the data already in the format you need for display then ? I've seen scrolling text, so do you really need to extract the items to just rewrite them in a string? are you doing any maths with the values ?

if you want to get rid the String class - which you really use just to build a new long string to display, you could use sprintf()

J-M-L:
just thinking aloud:

Can't the other MEGA send the data already in the format you need for display then ? I've seen scrolling text, so do you really need to extract the items to just rewrite them in a string? are you doing any maths with the values ?

if you want to get rid the String class - which you really use just to build a new long string to display, you could use sprintf()

the other mega has many other devices attached to it like GPS, keypad, a LCD, etc I tried it before that meag would itself handle the work of scrolling the text on DMD but then it slows down the system and sometimes even skips accepting inputs from user which is its prime function.

Other than using sprintf will it be possible to do it with c string like the code I recently posted? I am not quite sure if sprintf is part of c string itself tho!!

is there any way I can convert int to char?

Count:
is there any way I can convert int to char?

If you mean convert an int to its ASCII representation then yes, you can use itoa()

But if you really don't do anything with the integer values, then as you receive them as already ASCII representation, you don't need to go back to a number to change them again to a ASCII string... just keep the pointers to the incoming string to build your scrolling text

here is an example using sprintf() and strcat() to give you an idea on how that works - simulating the incoming buffer as you know how to do that:

char incomingMessage[] = "Hello World, int1, int2, int3, int4, int5, int6, int7, int8, int9";

const byte nbTokenExpected = 10;
char * ptrArray[nbTokenExpected]; // pointing at each token when we parse

bool parseIncomingMessage()
{
  char * strtokIndx;
  byte tokenCount = 1;
  bool success = false;

  for (byte i = 0; i < nbTokenExpected; i++) ptrArray[i] = NULL; // reset the list

  ptrArray[0] = (strtokIndx = strtok(incomingMessage, ","));
  while ((tokenCount < nbTokenExpected) && (strtokIndx != NULL)) {
    ptrArray[tokenCount++] = (strtokIndx =  strtok(NULL, ","));
  }

  if ((tokenCount == nbTokenExpected) && (strtokIndx != NULL)) {
    Serial.println("Got them all");
    for (byte i = 0; i < nbTokenExpected; i++) {
      Serial.print("ptrArray[");
      Serial.print(i);
      Serial.print("] --> \"");
      Serial.print(ptrArray[i]);
      Serial.println("\"");
      success = true;
    }
  } else {
    Serial.println("Not enough data");
  }
  return success;
}

void setup()
{
  Serial.begin(115200);
  if (parseIncomingMessage()) { // if we can successfully parse the message
    // then build our scrolling text 
    char scrollingText[200]; // big enough for what we want to do

    sprintf(scrollingText, "The final scrolling text will hold \"%s\" and the %d values I found were", ptrArray[0], nbTokenExpected-1);
    for (byte i = 1; i < nbTokenExpected; i++) {
      strcat(scrollingText, " |"); // use a | to separate the values
      strcat(scrollingText, ptrArray[i]);
    }
    Serial.println();
    Serial.println(scrollingText);
    Serial.print("The length of the scrolling text is ");
    Serial.println(strlen(scrollingText));
  }
}

void loop() {}

you'll see in the console (set at 115200 bauds)

[color=purple]Got them all
ptrArray[0] --> "Hello World"
ptrArray[1] --> " int1"
ptrArray[2] --> " int2"
ptrArray[3] --> " int3"
ptrArray[4] --> " int4"
ptrArray[5] --> " int5"
ptrArray[6] --> " int6"
ptrArray[7] --> " int7"
ptrArray[8] --> " int8"
ptrArray[9] --> " int9"

The final scrolling text will hold "[color=blue]Hello World[/color]" and the [color=blue]9[/color] values I found were | [color=blue]int1[/color] | [color=blue]int2[/color] | [color=blue]int3[/color] | [color=blue]int4[/color] | [color=blue]int5[/color] | [color=blue]int6[/color] | [color=blue]int7[/color] | [color=blue]int8[/color] | [color=blue]int9[/color]
The length of the scrolling text is 141
[/color]

You can see that we first built a list of pointers (pointing into then incoming message buffer) and as the strtok() function adds '\0' each pointers is actually pointing at a well formatted cString which is the ASCII representation of your token.

You can then use those pointers to build your final message as you see fit - I added the blue color coding above to show you what was built form the pointers or dynamically calculated (note that the heading space in the integers is part of cstring since we did not remove it when parsing)

Thanks!! Wow!!! Really heartiest thanks!! I have been a life saver for me!!! The program u wrote is just amazing!!

And the explanation made it more easy to understand. I guess this is what really seperate amateurs like from pro like u!!

Just one last question which I really can't figure out is, m supposed to receive all those over a serial communication so will it possible just to read the serially incoming data or I have to read one index at a time?

char incomingMessage[] = serial.read()

Is this correct?

M pretty sure that the parser that I was using and the method I was using to read the incoming data via serial won't b helpful now as it wasn't really effective. So can u please guide me further more and help me out on how should I read the incoming data via serial?

Count:
Just one last question which I really can't figure out is, m supposed to receive all those over a serial communication so will it possible just to read the serially incoming data or I have to read one index at a time?

char incomingMessage[] = serial.read()

You want to build the incoming message until you get the end Marker. that's what recvWithStartEndMarkers() does. it waits patiently to get the start marker, then build up the message until it gets the end marker. Once this as arrived, then you have the flag that tells you the message is ready for processing and that's when you parse and do the work

Never try to second guess how fast or slow the Serial communication will be... you'll get it wrong and your code won't be robust.

May be you need to read again Serial Input Basics

Really really thanks for helping me out there...m really greatfull of u. M gonna try that out right now and get back to tell u what happens..