|

Split String Arduino From The Serial Port

A string is a class used to store text. A String variable containing text enclosed in quotation marks “text”. Example: x = “Ok. Fine!”; In this article, I will share how to split strings on Arduino with data from Serial Communication.



What is Split String?

When Arduino communicates serially, the data we send or receive is most likely in text format. If in our program there is logic like “IF” which checks every incoming serial data, we have to separate the data so that it can be recognized.

In various programming languages, this text splitting can be found like string splitting in Python, java, javascript, etc. For example, in our program, there is logic to turn on the RGB LED with two commands, namely “Color” and “Duration” for the LED to light up. So, the data received is in the format “Color.Time;”.

Each command data to be received is separated by a dot “.”. For example, Arduino receives data “Red.1000;” then the red LED will light up for 1 second and turn off after 1 second. We divide the text based on the separator character being the dot “.”



How to split strings Arduino?

The methods for separating this text are:

  1. The program will count how many characters are contained in the text.
  2. The program will read one by one characters and save the characters into the Array to 0.
  3. When it finds a dot character “.”, The program will stop saving to the 0 array.
  4. The program will read the characters after the period “.” and save it to the 1st Array.
  5. etc.

That’s it.



Split String Code

I made a small sample program, copy and paste into your Arduino IDe. Then, upload the code to your board:

// Set the incoming data to 10 data
String ab[10];
String data;
String sectionData;
int a;
int stringData;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
    data = Serial.readString();

    for (a = 0; a < data.length() - 1; a++)
    {
      // split data based on point (.), Can also be replaced by comma (,)
      ab[a] = getData(data, '.', a);

      if (ab[a] != NULL)
      {
        Serial.print("Array ");
        Serial.print(a);
        Serial.print(" = ");
        Serial.println(ab[a]);
      }
    }
  }
}

String getData(String data, char delimiter, int sequence)
{
  stringData = 0;
  sectionData = "";

  for (int i = 0; i < data.length() - 1; i++)
  {

    if (data[i] == delimiter)
    {
      stringData++;
    }

    else if (stringData == sequence)
    {
      sectionData.concat(data[i]);
    }

    else if (stringData > sequence)
    {
      return sectionData;
      break;
    }
  }

  return sectionData;
}

Now, open the serial monitor and type “Oliver Manuel (2001). Origin of Elements in the Solar System“. Then click Send. You will see the result below:

How To Split String Arduino From The Serial Port


Note

You can replace separator ‘.’ with what you like.

For example a semicolon ‘;’.

On the line “ab[a] = getData(data, ‘.’, a);” to “ab[a] = getData(data, ‘;’, a);”

I tested the program above using Arduino Pro Mini. You can most likely use it on other Arduino variants.

Hopefully, this article is useful.



Search Terms:

Similar Posts