Help to simplify a function

I am processing various data from a gps and imu from string to int the function is working well is there any way to create a math function to reach the data i want?

here you can find the function that working:

String readString = offset;

int delimiter, delimiter_1, delimiter_2, delimiter_3, delimiter_4,delimiter_5,delimiter_6;
delimiter = readString.indexOf(",");
delimiter_1 = readString.indexOf(",", delimiter_1 );
delimiter_2 = readString.indexOf(",", delimiter_1 +1);
delimiter_3 = readString.indexOf(",", delimiter_2 +1);
delimiter_4 = readString.indexOf(",", delimiter_3 +1);
delimiter_5 = readString.indexOf(",", delimiter_4 +1);
delimiter_6 = readString.indexOf(",", delimiter_5 +1);


////// Define variables to be executed on the code later by collecting information from the readString as substrings.
String gx = readString.substring(delimiter -2 , delimiter_1);
String gy = readString.substring(delimiter_1 + 1, delimiter_2);
String gz = readString.substring(delimiter_2 + 1, delimiter_3);
String accx = readString.substring(delimiter_3 + 1, delimiter_4);
String accy = readString.substring(delimiter_4 + 1, delimiter_5);
String accz = readString.substring(delimiter_5 + 1, delimiter_6);

int GX = gx.toInt();
int GY = gy.toInt();
int GZ = gz.toInt();
int AX = accx.toInt();
int AY = accy.toInt();
int AZ = accy.toInt();

What does the String look like?
(Your code has no error management, are you sure you’ll always receive a valid string?)

If you use the TinyGPS++ library, all the decoding work is done for you.

1 Like

Hi @espmax ,

if you search the Internet for "split string to array Arduino" you will find a number of solutions (of different quality).

This one looks close to your needs

https://forum.arduino.cc/t/convert-string-to-an-array/1133788/5

It depends on the String class but there are also others that use C strings.

I recommend to add some safety to the function e.g. if the string does not contain the number of data expected...

Good luck!
ec2021

Yes it's working very well the problem is that i have to call some value in other function sure i have to rework this function

Thank Faraday your link seems to be promissing i going to work on it

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