Parsing char array to integer, atoi() returns squares

132 views Asked by At

I'm using an android app to send values to control servos. Code:

char inputData[4];  
char buffer[3];
void loop()
{



   if(Serial.available() > 3) {
      for (int i = 0; i < 4; i++){
        inputData[i] = Serial.read();

        }
    char buffer[4];
    buffer[0] = inputData[1];
    buffer[1] = inputData[2];
    buffer[2] = inputData[3];
    buffer[3] = '\0';
    int angle = atoi(buffer);
    Serial.write(angle);


   }
}

Issue: I'm getting the values + A-F letters to address each servo - A10, A180, B30 etc. Now the trouble is turning this to an actual integer. As you can see I've declared a character array to store the integers in and as suggested in a post on the arduino forum, I added a \0 at the end of the array. Currently, the Atoi returns random characters, mostly squares and some random numbers. I've tried even assigning them to a string and then .toInt() but same issue there, mostly squares. Any ideas? Thanks!

1

There are 1 answers

4
Juraj On

Use print or println to see the number as text. write sends it as byte and Serial Monitor shows a symbol with that ASCII code.