Splitting a String into multiple Integer

Hello!

I'm a bit new to programming, and just started on my first big project.

The Arduino receives a String from my computer, and saves the whole string.

I want to split this string in to three integers. How can I do that?

// Buffer to store incoming commands from serial port
String inData;

void setup() {
    Serial.begin(9600);
    Serial.println("Waiting for LabView to send a signal...\n");
}

void loop() 
{
    while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 

        // Process message when new line character is recieved
        if (recieved == ';')
        {
            Serial.print("Arduino Received: ");
            Serial.println(inData);
            
            inData = ""; // Clear recieved buffer
        }
    }
}

String from PC looks like this: 12,14,14;

The inData looks like this: 12,14,14

I want to move the three numbers to three different variables/integers. And everytime the Arduino recives a new string, the old values in the integers should med written over.

@GreenRay

Lose String, look at simple character arrays and "strtok"

AWOL:
Lose String, look at simple character arrays and "strtok"

Could somone trie to describe how i could use "strtok" in my program?

I just can't get it to work/understand it :slight_smile:

void setup() {
   char input[] = "12,14,15";
   char separator[] = ",";
   char *token;
   
   Serial.begin(9600);
   /* get the first token */
   token = strtok(input, separator);
   
  // Find any more?
   while(token != NULL) 
   {
      Serial.println(token );   
      token = strtok(NULL, separator);
   }
   Serial.println(input);  // Proof that original string is chopped up

}

void loop() {

}

On the first call to strtok(), you pass in the input string and a second string that holds the character(s) that delimit the fields in the input stream. In this example, we are only using the comma, but you can have as many as you want. If strtok() finds a token from the separator list, it returns a pointer to the first substring if finds. Internally, it also maintains a pointer to the token, so in this example, token points to "12" and the internal pointer points to the comma between 12 and 14. However, the function overwrites the comma with a null ('\0') character so the substring ("12") can be treated as a string (note lowercase 's').

On the next call to strtok(), token is advanced to the next character in the input stream (the '1' in 14) and the internal pointer advances to the next comma, where it overwrites it with a null. The call then returns the pointer to "14". This process continues until all separators have been found. Note that strtok() destroyes the original input string in the process.

If your data string id delimited with commas, then you might use code similar to the below. You can also change the character value into a number if needed.

int n = readString.toInt();
//zoomkat 11-12-13 String capture and parsing  
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data 

String readString; //main captured String 
String angle; //data String
String fuel;
String speed1;
String altidude;

int ind1; // , locations
int ind2;
int ind3;
int ind4;
 
void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 90,low,15.6,125*
  //or 130,hi,7.2,389*

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '*') {
      //do stuff
      
      Serial.println();
      Serial.print("captured String is : "); 
      Serial.println(readString); //prints string to serial port out
      
      ind1 = readString.indexOf(',');  //finds location of first ,
      angle = readString.substring(0, ind1);   //captures first data String
      ind2 = readString.indexOf(',', ind1+1 );   //finds location of second ,
      fuel = readString.substring(ind1+1, ind2+1);   //captures second data String
      ind3 = readString.indexOf(',', ind2+1 );
      speed1 = readString.substring(ind2+1, ind3+1);
      ind4 = readString.indexOf(',', ind3+1 );
      altidude = readString.substring(ind3+1); //captures remain part of data after last ,

      Serial.print("angle = ");
      Serial.println(angle); 
      Serial.print("fuel = ");
      Serial.println(fuel);
      Serial.print("speed = ");
      Serial.println(speed1);
      Serial.print("altidude = ");
      Serial.println(altidude);
      Serial.println();
      Serial.println();
      
      readString=""; //clears variable for new input
      angle="";
      fuel="";
      speed1="";
      altidude="";
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}