Separate values in arduino

You could also use a library for text parsing. E.g.,

#include <textparser.h>

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

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

  TextParser parser1(",");
  char parts[8][8];  // We expect 8 stings with a maximum length of 8.
  parser1.parseLine(split.c_str(), parts);

  for (char const (&part)[8]: parts) {
    TextParser parser2("/");
    int a, b;
    parser2.parseLine(part, a, b);

    Serial.print(a);
    Serial.print(", ");
    Serial.println(b);
  }
}

void loop() {}

Result:

133, 0
215, 365
3, 4
2, 12
3, 4
5, 23
52, 0
23, 0

Thanks for the code, but i have problem, I receive values of zero.

the values are alternated, the first five values are not from a single sensor, but are a value from one sensor and another value from another sensor

Show the code you are running and the data you are sending it. The data is expected is it looks in post #1

hello, the values that are enclosed are the ones that I want to obtain in a variable. the remaining values separate them in another variable.

this is the code of the evaluated sensors.

include<BH1750.h>
#include<Wire.h>
BH1750 sensor;
#include<Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
int ADXLAddress= 0x53;
int Sensorluz= 0x23;
Adafruit_ADXL345_Unified accel=Adafruit_ADXL345_Unified();
void setup() {
  
Serial.begin(9600);
if(!accel.begin()){
Serial.println("el sensor esta inacvtivo");
while(1);

}

sensor.begin();
}
void loop() {
 
  unsigned int lux = sensor.readLightLevel();


Serial.print ("Lum: ");
Serial.print(lux, DEC);
Serial.println(",");




delay(1000);
sensors_event_t event;
 accel.getEvent (&event);
Serial.print ("Ace: ");
  Serial.print(event.acceleration.x); 
  Serial.println("/");

  delay(2000);
}

this is what is being sent by serial port.

I have added "," at the end of the brightness sensor values
and I have added "/" at the end of the acceleration sensor values

Of those observed values, I only want to obtain the data from the luminosity sensor in a variable, to later give it a use, for example, if it reaches 20 lux write "low luminosity"
do you understand me ?

Remove these lines... the receiving code is expecting the values to be sent as in post #1

So just like this...

30,-0.16/11,0.55/ ...

1 Like

The code worked perfectly for me, thank you very much. Thank you so much.
Sorry one more question, if you could help me.
If I had more data, with more characters, for example

"133,215/36534%45,23/12345%198,32/76*98%

How could I separate these characters into different variables, would it be the same code that you provided me?

Thank you very much indeed for your help and patience.

Yes.same code. You just need to add another else-if statement for the new delimiter %. Just copy the one for /

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.