reading/splitting multiple values in one line received in the serial connection

Hello!,
I think I'm just missing the forest through the trees.

For my project, I send 2 Values to my Arduino over USB for example: 23,5 ; 290
these values are in the range of 0-360

I now want these values separated so I can use them in my code to like:
value 1 (in serial connection) = Azimuth
value 2 (in serial connection) = Elevation

How do I write this? I looked in the forum and found some old threads which made this seemingly (in python reference) simple task far too complicated.

(I can change the serial input I doesn't have to be exactly value; value2)

Thanks in Advance! :slight_smile:

The example #5 of the serial input basics tutorial shows how to receive and parse serial data.

Take a look at the strtok() function to split the string into sub strings, if it is a string and not a String. Then you can convert the sub strings into floats, ints, whatever using functions such as atof() and atoi()

Here is an example using methods from the serial input basics tutorial. It uses the strtok() function to separate the values and the atof() function to convert the input string data to float data type numbers. Be sure to set line endings in serial monitor to Both NL&CR or Newline and the baud rate to 9600.

// example code by C. Goulding (AKA groundFungus) 
// credit to Robin2, author of the serial input basics tutorial
const byte numChars = 16;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

float azimuth;
float elevation;

void setup()
{
   Serial.begin(9600);
   Serial.println("<Arduino is ready>  Enter Azimuth and Elevation separated with a semicolon (;) ");
}

void loop()
{
   recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[16]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ";");  // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   //Serial.println(index);
   // print all the parts
   /*Serial.println("The Pieces separated by strtok()");
   for (int n = 0; n < index; n++)
   {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
   }*/
   // convert string data to float numbers
   azimuth = atof(strings[0]);
   elevation = atof(strings[1]);
   newData = false;
   Serial.print("Azimuth = ");
   Serial.print(azimuth, 3);
   Serial.print("   Elevation = ");
   Serial.println(elevation, 3);
   Serial.println(); // blank line
}

Thank you all so much for the fast replies ! The Strtok and atof functions where just what i was looking for :slight_smile:

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