Splitting Strings Arduino

UPDATED CODE HERE


 

Hello!

Today I ordered parts for the proto phase of the plane.

This is a bit of code i’ve found that’s usefully for splitting strings in the arduino C

</pre>
#include <string.h>
#define MAX_STRING_LEN 20

char *inputstring1 = "one,two,three,four,five,six,seven,eight,nine,ten"; //this is the string that will be split. this can be changed!
char *p, *i;
int x;

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

void loop () {
 for (int x = 1; x <= 10; x++) {
 Serial.println(subStr(inputstring1, ",", x));
 delay(1000);
 }
}

char* subStr (char* input_string, char *separator, int segment_number) {
 char *act, *sub, *ptr;
 static char copy[MAX_STRING_LEN];
 int i;

 strcpy(copy, input_string);

for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {

 sub = strtok_r(act, separator, &ptr);
 if (sub == NULL) break;
 }
 return sub;
}

//esologic.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.