Sending and reading multiple ints via string over serial

Hello great Arduino community,

I'm trying to send multiple int from an arduino to an arduino over serial.
I've searched the forums for this problem and i found a solution for the receiving end. I receive a string and pull out the ints. This works great.

String readString, sensor1, sensor2;

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

void loop() {

 while (Serial.available()) {
   delay(10);  
   if (Serial.available() >0) {
     char c = Serial.read();  
     readString += c; 
   } 
 }

 if (readString.length() >0) {
     Serial.println(readString);
     
     sensor1 = readString.substring(0, 3); 
     sensor2 = readString.substring(3, 6); 
     
     Serial.println(sensor1);  
     Serial.println(sensor2);
     
     int n1;   
     int n2;
     
     char carray1[6]; 
     sensor1.toCharArray(carray1, sizeof(carray1));
     n1 = atoi(carray1); 
     
     char carray2[6];
     sensor2.toCharArray(carray2, sizeof(carray2));
     n2 = atoi(carray2); 
  
     Serial.println(n1);
     Serial.println(n2);   
     
   readString="";
 } 
}

The problem is right now: How do i put multiple ints into a string that add the zero's before the number. What i mean is that i read out the first 3 char out of the string. So when i send out a 7 for example it needs to be 007 and not 7 because otherwise it starts out reading the second int that is placed in the 3-6 bytes.

String totalstring;

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

  int a=1;     //declaring integer
  int c=2;
  char b[3];   //declaring character array
  char b2[3];   //declaring character array
  
  String str;  //declaring string
  String str2;  //declaring string

  str=String(a); //converting integer into a string
  str.toCharArray(b,3); //passing the value of the string to the character array
  Serial.print("Value of b is \t");  //display that value
  Serial.println(str);
  
  str2=String(c); //converting integer into a string
  str2.toCharArray(b2,3); //passing the value of the string to the character array
  Serial.print("Value of b is \t");  //display that value
  Serial.println(str2);
  
  totalstring = str + str2;
 
  Serial.println(totalstring); 
}

void loop(){}

I've tried this, but this doesnt work ofcourse.. if anyone could give me a nudge in the right direction or give me some code with the some explenation that would be wonderfull. :slight_smile:

thanks in advance,

Look at the sprintf() function

void setup() 
{
  Serial.begin(115200);
  char buffer[10];
  int x = 123;
  int y = 45;
  int z = 6;
  sprintf(buffer,"%03d%03d%03d",x,y,z);
  Serial.println(buffer);
}

void loop() 
{
}

Output

123045006

Rewrite the receiving program so it does not need leading zeros.

See the examples in this demo

...R

Use a delimiter between each value being sent so you don't need to actually parse the way you do. Below is an example for sending servo command values.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}