Sending multiples variables via Serial

Hi, recentrly i saw a video about sending multiple variables via Serial to a software on the computer, and here is the code i saw.

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

void loop() {
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A2);
  int sensor3 = analogRead(A3);

  char text[40];
  sprintf(text, "%d,%d,%d/n", sensor1, sensor2, sensor3);

  Serial.println(text);
  delay(100);
}

Basically what i understood is that sprintf() combines variables and text into an char array and it's better to use this function when u have to combine these 2 elements into a string (tell me if it's wrong).

But the point is: when, for example, you make a serial comunication betwen 2 arduinoes, how do you separe the variables contained in the string that you have just received?

Cheers, Luca.

BTW, here is the video that i saw.

Serial Input Basics - updated - #3 by Robin2

A thought for you:

When you read, there are multiple statements written in sentences and paragraphs. How do you separate those?

I think that the parsing example (#5), of the serial input basics tutorial, would be more appropriate for receiving and parsing data from a comma delimited string.

thanks guys for reatching out.

To do so is not wrong, but it's not better, it simply does not matter at all.
Well, it's error prone, because your text buffer might be too small.

BTW: Why do you send the extra two characters "/n" before the newline, produced by the following Serial.println?

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