Separate values in arduino

Hello, I wanted to ask a question. In the following sketch made in arduino, to separate data, I want to get all the data that is characterized by the ",", because as the sketch is, I only get the first value.
That is, I want to get the 133 365 4 12… and so on

Thanks

Don't post your code as an image... post using code tags </> in the editor... images can't be copied for anyone trying to debug your code.

So you just want variables that have a "," after them? You don't want variables that have a "/" after them?

Your string split appears to contain two separator characters, that is ',' and '/'.
The function call in the loop() getValue( split, ',', 0) appears, because of the fixed parameter 0, to always return the first substring from split.

String split = "133,215/365,3/4,2/12,3/4,5/23,52,23";

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

void loop() {
int parte1 = getValue(split,',',0).toInt();
Serial.println(parte1);
delay(1000);
}

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {10, -10};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[10] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+2 : i;
    }
  }

  return found>index ? data.substring(strIndex[10], strIndex[1]) : "";
}

yes, I want the data containing "," stored in a variable.
and data containing "/", stored in another variable.
You understand me?

yes, I don't know how to make it not be fixed
I want to get the 133 365 4 12… and so on


char split [] = "133,215/365,3/4,2/12,3/4,5/23,52,23";


void setup()
{
  Serial.begin(115200);

  Serial.println(getInteger(split, sizeof(split), 0));
  Serial.println(getInteger(split, sizeof(split), 2));
  Serial.println(getInteger(split, sizeof(split), 4));
  Serial.println(getInteger(split, sizeof(split), 6));
  Serial.println(getInteger(split, sizeof(split), 8));
  Serial.println(getInteger(split, sizeof(split), 10));
  Serial.println(getInteger(split, sizeof(split), 11));
  Serial.println(getInteger(split, sizeof(split), 12));
}

void loop()
{}


uint16_t getInteger(char *input, uint8_t size, uint8_t index)
{
  uint8_t  delimeters = 0;
  uint16_t result = 0;
  
  for (uint8_t x = 0; x < size; x++)
  {
    if (input[x] >= '0' && input[x] <= '9')
    {
      if (delimeters == index)
      {
        result = result * 10 + input[x] - '0';
      }
    }
    else
      delimeters++;

    if (delimeters > index)
      break;
  }

  return result; 
}
16:03:51.876 -> 133
16:03:51.876 -> 365
16:03:51.876 -> 4
16:03:51.876 -> 12
16:03:51.876 -> 4
16:03:51.876 -> 23
16:03:51.876 -> 52
16:03:51.876 -> 23

This is an array defined as containing 2 integers:
int strIndex[] = {10, -10};

This array element [10 ] is out of bounds:
strIndex[10] = strIndex[1]+1;

String split = "133,215/365,3/4,2/12,3/4,5/23,52,23";

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

void loop() {
int parte1 = getValue(split,',',0).toInt();
Serial.print(parte1);
delay(1000);
}

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

I have made this arrangement, but I need what I said before, that the index parameter is not fixed, if not, I will get values that contain a certain character "," or "/".

I understand how you do it, but placing too many Serial.prints is tedious for me, since I am going to receive hundreds of data. That's why I need all data containing "," to be readable

It's an example.

That's why I need all data containing "," to be readable

What does that mean? What are you doing with each variable?

please describe exactly:

  • where does the data come from
  • how do you read it into your arduino
  • which indicator (/) is used to indicate which variable,

int parte1 = getValue(split,',',0).toInt();

What about changing the 0 to an x and using a for loop to change the value of x ?

Edit

Even if that works, you'll get another mess because you are not handing the delimiter '/'.
The next substring to be returned will be '215/365' which will fail to convert to an integer.

Well I explain better, I am doing a project, in which I have several sensors connected to an arduino, this data is being sent to another Arduino by serial port, but what I want is that every time the data arrives, just take the data from a certain sensor, and so with the other take only the values of this sensor. That is, I want to extract the data received from the sensors. I hope to make myself understood.
In the following image you can see integer values (light sensor) and decimal values (acceleration sensor), so I only want to take the values of the light sensor in one variable, and the acceleration sensor in another variable, for later , to be able to put these data to use.

I've been finding out how to do it, but I can't find the way to do it, I've seen that it can be done with the string class, but I don't know how to do it precisely.

I appreciate your interest in helping me.

Do you always receive the data in pairs?

Why don’t you just read the data as it comes in via serial? Why bother with a String that you then have to break up again?

Similar to post #15 :
Each time you get data from a sensor, send a line terminated with an end of line character. That line should consist of the sensor identifier and the value that sensor returns.
Say:

S01 13.98
S03 5.0
S02 87.9
. . .

If you really want to pack it all in a string then look at json.

In order to make myself understand better, in the following image, the obtaining of values from the sensors is better observed.
What I want is to separate the luminosity and acceleration sensor data into different variables.

And with that I can separate the data from each sensor?

You could use something like this to read/process the data.


char data[32];
uint8_t idx = 0;

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

void loop()
{
  while (Serial.available() > 0)
  {
    char c = Serial.read();

    if (c == ',')
    {
      data[idx] = '\0';
      int lum = atoi(data);
      Serial.print("lum = ");
      Serial.println(lum);
      idx = 0;      
    }
    else if (c == '/')
    {
      data[idx] = '\0';
      float ace = atof(data);
      Serial.print("ace = ");
      Serial.println(ace);
      idx = 0;      
    }
    else
    {
      data[idx++] = c;
    }
  }
}

This expects lum variables to be terminated by ",", and ace variables to be terminated by "/".

1 Like

If there is new data in the serial buffer then read the first 5 characters.
If those 5 characters are 'Ace: ' then set your acceleration variable to the result of Serial.parseFloat()
If those 5 characters are 'Lum: ' then set your luminosity variable to the result of Serial.parseInt()

See How to Read User Input from the Arduino Serial Monitor - Circuit Basics