Combining Char Array into Single Integer

I'm close with this code, but it prints out 2 , 4, 6, 8 , 0 individually. I am wondering how I get this array into just one Integer type like int=24680?

This is taking serial data char string from one arduino through tx/rx lines and sending to another arduino (as "24680") but on the receive arduino it is separating out each value (I see this in the Serial Monitor Printout).

Help would be greatly appreciated!

String inString = "";
int stringToInteger;
char mystr[10]; //Initialized variable to store recieved data

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  // Serial.readBytes(mystr,5); //Read the serial data and store in var
  //Serial.println(Serial.read()); //Print data on Serial Monitor
  delay(1000);
  int inChar = (Serial.read());
  inString += (char)inChar;

  if (inChar == '\n') {

    stringToInteger = inString.toInt();
  }

  Serial.print("Value:");
  Serial.println(inString.toInt());
  Serial.print("String: ");
  Serial.println(inString);
  // clear the string for new input:
  inString = "";


}

I would suggest to study Serial Input Basics to handle this

Not sure why are converting to/from int if you are just passing through
Can you post the sending Arduino code as well.
Looks like there is a delay() on the send side that causes Serial.readInt() to timeout and just return one char as a number
See my tutorial on Arduino Software Solutions for various ways to read a 'whole' line of text.

This posting has a complete Serial connection between two arduinos.
It uses JSON, but you can just send plain text.
This tutorial is an earlier version of the code (use the code referred to in the post), but it contains the circuits for connecting various Arduino boards

there is a serial function that does all this for you called serial.parceint();

the code doesn't wait for something to be available() on the serial input.

it also prints values before the string is completely read (terminated by a \n)

consider

String inString = "";
int stringToInteger;
char mystr[10]; //Initialized variable to store recieved data

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

void loop() {
    if (! Serial.available())
        return;

    int inChar = (Serial.read());
    inString += (char)inChar;
    if (inChar != '\n') {   // wait for entire string
        return;
    }

    stringToInteger = inString.toInt();
    Serial.print("Value:");
    Serial.println(inString.toInt());
    Serial.print("String: ");
    Serial.println(inString);
    // clear the string for new input:
    inString = "";
}

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