Go Down

Topic: Arduino: Split String (Read 180 times) previous topic - next topic

Max2002_

Oct 10, 2017, 09:26 pm Last Edit: Oct 10, 2017, 09:37 pm by Max2002_
Hello,

I want to split a long string and I tried many different things but none of them work.
Now I tried this:
Code: [Select]
void setup() {
  Serial.begin(9600);
  String str = "51:false:8:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,1,1,0,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,";

  int first = str.indexOf(':');
  int second = str.indexOf(':', first + 1);
  int third = str.indexOf(':', second + 1);
  int fourth = str.indexOf(':', third + 1);
  int fifth = str.indexOf(':', fourth + 1);
  int sixth = str.indexOf(':', fifth + 1);
  int seventh = str.indexOf(':', sixth + 1);
  int eighth = str.indexOf(':', seventh + 1);
  int ninth = str.indexOf(':', eighth + 1);

  String width = str.substring(0, first);
  String isAnimation = str.substring(first + 1, second);
  String animationSpeed = str.substring(second + 1, third);
  String one = str.substring(third + 1, fourth);
  String two = str.substring(fourth + 1, fifth);
  String three = str.substring(fifth + 1, sixth);
  String four = str.substring(sixth + 1, seventh);
  String five = str.substring(seventh + 1, eighth);
  String six = str.substring(eighth + 1, ninth);
  String seven = str.substring(ninth + 1);

  Serial.println(width);
  Serial.println(isAnimation);
  Serial.println(animationSpeed);
  Serial.println(one);
  Serial.println(two);
  Serial.println(three);
  Serial.println(four);
  Serial.println(five);
  Serial.println(six);
  Serial.println(seven);
}


But I get this as output:
Code: [Select]
51
false
8
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,








Did I do something wrong?
I use an Arduino Uno (ATMEGA328P-PU).

Thanks.

Gahhhrrrlic

Looks to me like you're using the wrong token ':' to break up the 0's, which are comma delimited.
https://turbofuture.com/misc/The-Ultimate-Arduino-Tutorial

Max2002_

#2
Oct 10, 2017, 10:18 pm Last Edit: Oct 10, 2017, 10:23 pm by Max2002_
Looks to me like you're using the wrong token ':' to break up the 0's, which are comma delimited.
No, that's not the problem because that is the second step. First I need these 10 strings (and 7 of them are strings with ones, zeroes and commas).

ron_sutherland

I am clueless with software, so take this with a grain of salt.

Does String not have a bad reputation in the Arduino world, isn't it an object, e.g. one of those OOP things.

On a Raspberry Pi Linux will stop the program and tell the user they have run out of memory (e.g. the heap and stack have collided) but Arduino provides no such service (I don't think AVR even has an interrupt that could be used to do such a service but I have not looked for it either).

On AVR I learned to distrust C++ because I am not good enough to know when I have done something that will use the heap memory, but I have learned that I can use plain old C and it does not use the heap memory (unless I tell it to with memory allocation functions).

Anyway, try to figure out a way to not use String. Also if you want to learn C++ then I suggest the Raspberry Pi will be a better environment.

sterretje

#4
Oct 12, 2017, 08:14 pm Last Edit: Oct 12, 2017, 08:23 pm by sterretje
You're running low on or out of memory. It is 732 plus 1 plus some String overhead bytes long and you basically make a copy spread over several other variables.

Where does the string come from? What you exactly need to do with it.

Basically you will have to process on 'block' at a time.

strchr (and maybe strtok) will be your friends once you get rid of String (capital S).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up