Skip to content Skip to sidebar Skip to footer

How to split string arduino from the serial port programming

Split a String Arduino

How to split string arduino from the serial port programming - String is a class used to store text. A String variable containing text enclosed in quotation marks. Example: x = "Hello"; 
 

A. What is Split String in Arduino Programming?

When serial data communication, the data that we transmit or receive, data may be contains a text.
 
If in our program there is some logic such as "IF" on the incoming serial data,  we have to split the  data.
How to split string arduino from the serial port programming
 
In various programming languages, separating this text can be found such as split string in python, java, javascript, etc. which you can find on search engines.
 
For example, in our program there is a logic to turn on the RGB LED with two commands, "Color" and "Duration" of the LED on. 

So, the data received is in the format "Color. Time;". Every command that will be received ends with a point ".". 

When Arduino receives data "Red.1000;" then the LED will turn on red and will turn off after 1 second.
 
From the example above, we are split the text with the boundary is a point "." 

The simple logic is accept the first data up to the point "." and save it in the 1st Array, then read the second data until the point "." and store it in the 2nd Array, and so on.
 

B. How to split Arduino C++ strings?

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 character 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.

I made a small sample program. Upload this program to Arduino, open the serial monitor. Type a word (whatever) and separate it with a period of ".".

For example, I send "Oliver Manuel (2001). Origin of Elements in the Solar System". Source of this words is wikipedia.

 

C. Split String Arduino Program

// 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;
}
 
 
How to split string arduino from the serial port programming

 
 
You can replace the point "." 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 variants such as Arduino Uno, Arduino Leonardo, Arduino Pro Micro, Mega 2560, Esp8266 Wifi, Arduino Nano V3, Arduino Genuino, Esp8266 Board, STM32 like Blue Pill and others.
 
Hopefully the program above is useful. End.

Search Terms:

Post a Comment for "How to split string arduino from the serial port programming"

Subscribe via Email