Reading multiple values from serial at a time

I am working on a project that will use bluetooth to send data from one arduino to another. The data has to consist of different values that are each interpreted differently (e.g. first value is a servo angle, second a motor speed etc.)

I’m thinking that maybe I could send a string made up of different numbers with seperation characters inbetween, then seperate each value , convert them to integer and store them in the respective variables.

However I have absolutely no idea on how to do this. Neither do I know how to seperate the string to seperate substrings, nor how to convert multi-character numbers from string to int. I know that to convert a single character number to its int value I have to subtract ‘0’ ASCII value, but what about a larger number like 1023? Any help would be appreciated.

Have a read through this:

There should be enough in there to get you started.

Bluetooth is very useful when connecting an Arduino to a phone, tablet, laptop, or other appliance with Bluetooth built-in. But for use between Arduinos, I don’t know of any advantages to choosing Bluetooth over the many other types of wireless/radio modules that are available. There will be cheaper ones, longer range ones, lower power ones than Bluetooth that will be available, for example. So don’t think that Bluetooth is your only option.

Okay thanks! This sure is insightful… But even if I’m using adifferent medium of transmission, I still need to figure out how to piece together and interperate strings of data.

Wow thanks so much!

Then you might consider not bothering. The time and code required to make up the string may make the whole idea questionable anyway. The simplest way is to
Read sensors 1,2,3
and
Serial.print them with
Serial.print(",")
in between.
Receiving the data is not much harder, and well covered in the abovementioned tutorial. For one-to-one comms like you propose, Bluetooth is the simplest choice and dirt cheap. You don’t mention range. Standard HC-05 is good for up to 10m line of sight.

Be warned, Serial Input Basics, as written, is safe but only if every char used to hold the parsed results to be the same size as the input string.
This is usually not the case particularly when you are parsing out multiple short values from a larger input.
The fix is to replace strcpy in example 5 with strlcpy
See this sketch for examples of strlcpy strlcpy_strlcat.ino

Also Serial Basics uses the sloppy atoi() to convert from text to numbers.
More robust alternatives are available however they may not be necessary if a program is generating the input rather than a user.

Also a minor issue to be aware of is that if your input exceeds the allocated input buffer size it is just truncated and still returned for parsing.
The fix for this is to check the strlen() of the returned input against the buffer length

if (strlen(receivedChars) == (numChars -1)) {
    // data truncation likely
}

Do you need to send the parameters as human readable text? If you package your parameters in a struct, then you can send that between Arduinos and avoid the parsing issues.

Nope I need the int values

For receiving numbers there is Serial.parseInt() and Serial.parseFloat(). They throw away anything that doesn’t look like a number (spaces or commas, for example) and returns the number it receives until it finds another non-number or has not received a character for a full second.

To keep your messages from getting out of sync it is good to have a start character. Let’s use ‘$’:

  while (Serial.read() != '$') ;  // Wait for $

  int var1 = Serial.parseInt(); // Integer
  int var2 = Serial.parseInt(); // Integer
  float var3 = Serial.parseFloat(); // float

Aw man thanks so much! This seems to be exactly what I was looking for. I’ll give it a try.