How to store incoming serial string in arduino?

In my recent project, I am working with serial data. Till now, I'm able to receive data and display on LCD. Now, suddenly I can't get any serial data. Nothing display on LCD.

My incoming string is:

{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}

And my code is below:

#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

String response = "";
bool begin = false;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop()
{

  while(Serial.available() || !begin)
  {
    char in = Serial.read();

    if (in == '{')
    {
      begin = true;
    }

    if(begin)
    {
      response += (in);
    }

    if(in == '}')
    {
        break;
    }
    delay(1);
  }

  lcd.setCursor(0, 0);
  lcd.print(response);
}

Lets say your incoming string which is 54 bytes long display at the first time, then a second string arrive , from what I can read of your code you will concatenate the next string to the oldest one resulting in something like

{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}...

That will not fit on the lcd.You should empty the string when a new one is receveid
Also why not just use a char array instead a String

I'm able to receive data and display on LCD. Now, suddenly I can't get any serial data

That was the change between before and now?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

hugo007:
Also why not just use a char array instead of a String.

I know that concept. But not idea about how to use it. Because the length of that upcoming data may be not known.

Please correct me if I wrong. Below is my code according to using a char array.

#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

char response[54] = {};
int i;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop()
{
  if(Serial.available())
  {
    char in = Serial.read();

    response[i] += in;
  }
  
  lcd.setCursor(0, 0);
  lcd.print(response);
}

hsn32:
I know that concept. But not idea about how to use it. Because the length of that upcoming data may be not known.

The exact length isn't that important, what's important is the max length it might be :wink:

Just make a string that can hold the max length. DON'T make this extremely big. If you're thinking >100 then you should rethink what you send to the Arduino. And you don't fix this by using String, on the contrary, with the possibility to keep extending the String the problem of memory corrupting may occur rather fast.

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parsing example.

...R

Yes, using String Heap fragmentation happened. Now I check it put your link about "cstings". So, here we have to use a Header file and use functions. But which function I have to use?

You must end the last element of the array with a '\0' to became a string
Then it can be printed.
Maybe something like this will work(Don't have time to test it now)

uint8_t indexPos = 0;
 while(Serial.available())
  {
    char in = Serial.read();

    response[indexPos++] = in;
  }
//Terminate the array in the end
response[indexPos] = '\0';
//reset the index pos to process the next one
indexPos = 0;
//ready to be printed
 lcd.setCursor(0, 0);
  lcd.print(response);

hsn32:
Yes, using String Heap fragmentation happened. Now I check it put your link about "cstings". So, here we have to use a Header file and use functions. But which function I have to use?

Have you actually tried my examples?

...R

hugo007:
Maybe something like this will work

Only for limited definitions of "work"

A nice concise piece of code I like to use is:

  // Store recieved data into command string
  char Command[BUF_SIZE] = "";
  while(Serial.available())
    sprintf(Command, "%s%c", Command, bt.read());

the BUF_SIZE can be defined at the top of the code. Now the variable Command can be used like any other character array