Send variable name and data trough serial.

Greetings.

I was wondering what is the best way to do that, send a variable name and the data for that variable.

I want to send a string like "var1 data var2 data var3 data etc"
And in the Arduino to split that string and put the x+1 data in the x variable.

Something like this
if x == "var1" var1 = x+1;
else if x = "var2" var2 = x+1;
...

Is there any easier or better way to do this?

Tell us what you want to do, not how you think you should do it.

you can do with a system of reading tokens.

Every time you get a space character you get a new token, which can be an variable name or an data value.

Suppose you just read a variable token (because it's the first data), you know that after you will read the data token corresponding to this variable.

So from in your sketch, it will be easy to update the datas considering the variable.

You can also use special caracter to define the start of the name of a variable and another to the start of a data. They can be for instance the # and @ characters. So from, the string you send by serial would be :

"#var1@data#var2@data#var3@data" ...

Hop it can help you.

There are a lot of way to do it, this is one of theses...

Best regards.

i think you are wanting a way to catch up strings for that purpose you can use data packets technique to do this wherein all the string data you need to send is enclosed in a packet like

AWOL:
Tell us what you want to do, not how you think you should do it.

I need to control an electronic valve for a sequence like
(it is closed by default) -> open -> delay1 -> close -> delay2 -> open-> delay3 -> close (end)

And i need to change the length of those delays.

NI$HANT, Grag38 thanks, I'll try that and see how it goes

PS: I use an ARDUINO MEGA2560

Edit:

This seems to work, I send a string <varname,value><varname,value> and it splits each < > into the temp variable.

All I have to do now assign tmp[1] to the correct variables.

void proceseaza(String data) {
	int lungime = data.length();
	String tmp[2] = {"",""};
	int index = 0; //pozitia in string
	int start = 0; //daca e 1 scrie in tmp. daca e 0 nu scrie
	int unde = 0;  // in ce variabila tmp sa scrie
	do {
		if (data[index] == '<') {
			start = 1; index++;
		} //daca e < incep scrierea in variabila
		else if(data[index] == ',') {
			unde = 1;
			index++;
		}
		else if(data[index] == '>') {
			start = 0;
			unde = 0;
			
			Serial.println(tmp[0]+' '+tmp[1]);
			
			tmp[0] = ""; tmp[1] = "";
		} //daca e > se temrtina scrierea
		
		if(start) {
			tmp[unde] += data[index];
		}
	
	} while(index++<lungime); //end while
}//end proceseaza;