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. ![]()
thanks in advance,