# Anyone knows how to change this to work with 3 integers instead?

**URL:** <https://forum.arduino.cc/t/anyone-knows-how-to-change-this-to-work-with-3-integers-instead/1037572>\
**Category:** Programming\
**Created:** [October 2, 2022, 8:50am UTC](https://forum.arduino.cc/t/anyone-knows-how-to-change-this-to-work-with-3-integers-instead/1037572 "2022-10-02T08:50:48Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![kuro2002](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kuro2002](https://forum.arduino.cc/u/kuro2002)\
**Post date:** [October 12, 2022, 11:58am UTC](https://forum.arduino.cc/t/anyone-knows-how-to-change-this-to-work-with-3-integers-instead/1037572/5 "2022-10-12T11:58:24Z")

</div>

Thank you everyone for the help! I found the solution that I wanted it is from [this](https://eecs.blog/sending-multiple-values-over-serial-to-arduino/) site. The code for breaking up a **single string** into **different variables** are as follows:

```cpp
if(Serial.available()){
  String rxString = "";
  String strArr[2]; //Set the size of the array to equal the number of values you will be receiveing.
  //Keep looping until there is something in the buffer.
  while (Serial.available()) {
    //Delay to allow byte to arrive in input buffer.
    delay(2);
    //Read a single character from the buffer.
    char ch = Serial.read();
    //Append that single character to a string.
    rxString+= ch;
  }
  int stringStart = 0;
  int arrayIndex = 0;
  for (int i=0; i < rxString.length(); i++){
    //Get character and check if it's our "special" character.
    if(rxString.charAt(i) == ','){
      //Clear previous values from array.
      strArr[arrayIndex] = "";
      //Save substring into array.
      strArr[arrayIndex] = rxString.substring(stringStart, i);
      //Set new string starting point.
      stringStart = (i+1);
      arrayIndex++;
    }
  }
  //Put values from the array into the variables.
  String value1 = strArr[0];
  String value2 = strArr[1];
  //Convert string to int if you need it.
  int intValue1 = value1.toInt();
}

```

I studied and changed the code to like this:

```cpp
String value1,value2,value3,value4,value5;
char ch;
int aqi,co;
float temp,hum;
 if(Serial2.available()){
  String rxString = "";
  String strArr[4]; //Set the size of the array to equal the number of values you will be receiveing.
  //Keep looping until there is something in the buffer.
  while (Serial2.available()) {
    //Delay to allow byte to arrive in input buffer.
    delay(2);
    //Read a single character from the buffer.
    ch = Serial2.read();

    //Append that single character to a string.
    rxString+= ch;
  }
  int stringStart = 0;
  int arrayIndex = 0;
  
  for (int i=0; i < rxString.length(); i++){
    //Get character and check if it's our "special" character.
    if(rxString.charAt(i) == ','){
      //Clear previous values from array.
      strArr[arrayIndex] = "";
      //Save substring into array.
      strArr[arrayIndex] = rxString.substring(stringStart, i);
      //Set new string starting point.
      stringStart = (i+1);
      arrayIndex++;
    }
  }

  //Put values from the array into the variables.
  value1 = strArr[0];
  value2 = strArr[1];
  value3 = strArr[2];
  value4 = strArr[3];
  value5 = strArr[4];
  //Convert string to int if you need it.
  aqi = value1.toInt();
  co = value2.toInt();
  temp = value3.toFloat();
  hum = value4.toFloat();

```

I am still a bit new to this. The solution provided by **gcjr** and **J-M-L** doesnt seem to work for me. Moreover, I am not familiar with the code, I dont even know like 80% of the code even does lol. Therefore, I found the next solution. I think that this solution is better for begineers like me and the code is easier to understand, I manged to tweak them by myself.

Anyways I hope it helps other people in the future! I am not a native English speaker, hopefully you guys can understand my english. Thank you very much!

---

_[View the full topic](https://forum.arduino.cc/t/anyone-knows-how-to-change-this-to-work-with-3-integers-instead/1037572)._
