top of page
Search
  • Writer's pictureIfrim Ciprian

String Separation Function - Arduino

Updated: Mar 4, 2022

The Nicla Sense ME sensor library called BHY2 outputs certain sensor data as a string.

The following 2 methods can be used with the sensors:

  1. toString() = outputs data as a string;

  2. value() = outputs data as int/float depending on the sensor;

The issue is that some sensors only accept the toString() method, sensors such as the magnetometer, BSEC or the orientation algorithm output.

Here is an example of the output as a string:


In order to extract the float values that I need and to pass convert them to a float or int, I used the following approach, seen with the BSEC values:

int iaq = ((bsec.toString()).substring(25,28)).toInt();
float voc_eq = ((bsec.toString()).substring(53,57)).toFloat();
int co2_eq = ((bsec.toString()).substring(68,72)).toInt();
int accuracy = ((bsec.toString()).substring(84,85)).toInt();
float comp_temp = ((bsec.toString()).substring(95,101)).toFloat();
int comp_hum = ((bsec.toString()).substring(112,117)).toInt();
int comp_gas_res = ((bsec.toString()).substring(128,130)).toInt();

This approach simply uses the substring() arduino method to extract certain characters, and then by using the conversion .toInt() or .toFloat(), the value can be converted to a number. It has been a bit difficult initially to find the right characters, especially since it every value changes the amount of spaces it takes, for example:

  • Temperature can be 5, 25 or -10. So it takes between 1 to 3 char spaces.

  • The heading for the Orientation can be 5.124, 16.255, 275.000. 4 to 6 spaces.


This variable space makes it difficult to select the right size for the substring() function. I was able through trial and error to get it to work well, but there may be special cases where if all values in the string are of 1 character spacing, the whole string moves to the left, so I am not happy with the current system, and I do not want to leave future errors to chance. Furthemore, the code footprint was too high.


Therefore, I created a function to be recalled, which separates the whole string by empty spaces. So the function adds to a string array all the "in-between strings" at different indexes, which I can then select. The code is based on John Wasser's work available here: https://forum.arduino.cc/t/how-to-split-a-string-with-space-and-store-the-items-in-array/888813


I modified the code so that it is a function that can be recalled with a string (to be split), an array name(to contain the different strings), and a boolean value 0, 1 to represent the Array output with the index + element it contains. The 0,1 may be changed to a True, False at a later stage for an easier code understanding.

//before void setup()
String compass_array[13];
int StringCount = 0;
String compass_heading = "";

//function
void string_separator(String str, String strs[], int result) {       //split the string into substrings, second value
  memset(strs, 0, sizeof(strs));                      //reset char array to empty values
  StringCount = 0;                                    //reset string index to 0

  while (str.length() > 6)
  {
    int index = str.indexOf(' ');
    if (index == -1) // No space found
    {
      strs[StringCount++] = str;
      break;
    }
    else
    {
      strs[StringCount++] = str.substring(0, index);
      str = str.substring(index + 1);
    }
  }

  if (result == 1)
  {
    for (int i = 0; i < StringCount; i++)
    {
      Serial.print(String("[") + String(i) + String("] = "));
      Serial.print(strs[i]);
      Serial.print("\r\n");
    }
  }
}

The output looks as follows:




6 views0 comments

Recent Posts

See All
bottom of page