Cannot convert serial input from int to ASCII using atoi function

Please help me find my error. I can convert random int, but cannot convert single input to ASCII using same atoi function. What did missed? I need both int ( calculation ) and ASCII character (UI) Thanks Cheers Vaclav

int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  delay(5000);
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    // data in 
    Serial.println("Received: ");
    Serial.println(incomingByte);

    char buf[] = {
      "-2147483648\0"    };
    itoa(incomingByte,buf,10);
    Serial.println(buf);
    Serial.println(incomingByte);
    Serial.println(itoa(incomingByte,buf,10));

    char buffer[12]; // "-2147483648\0"
    //lcd.println(itoa(random(1024)-512, buffer, 10));
    Serial.println(itoa(random(1024)-512, buffer, 10)); // TOK 
    Serial.println(itoa(incomingByte, buffer, 10));           // no go still decimal int in buffer 

    ///char *  itoa ( int value, char * str, int base );
  }
}

I don't see any atoi() calls, is that a typo in the blurb or the program?


Rob

TYPO. Integer to ASCII, sorry. The code is correct, but....

Well, several things:

1) part of the problem is that 2147483648 is a tad bit bigger than will fit in an int 2) incomingByte is a horrible name for an int 3) itoa() converts from integer to ASCII but you're reading it as ASCII using the Serial object, so why convert it to what it already is?

I'm not sure I understand what you're trying to do.

econjack: Well, several things:

1) part of the problem is that 2147483648 is a tad bit bigger than will fit in an int

It is commented out. And what that got to do with question?

2) incomingByte is a horrible name for an int

Taken from Arduino sample code. Unfortunate but ....what that got to do with question?

3) itoa() converts from integer to ASCII but you're reading it as ASCII using the Serial object, so why convert it to what it already is?

No it does not in my example. And Serial reads in an integer - as coded.

I'm not sure I understand what you're trying to do.

I am trying to convert int to ASCII. Serial.println(itoa(incomingByte, buffer, 10));

Thank you for you help. Cheers Vaclav

I am trying to convert int to ASCII.

If that's the case, then why are you reading the Serial object?

The following code waits for an integer to be entered from the keyboard and the user to click Send. It also takes the arbitrary value of 12345 and converts it to ASCII.

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

  char buf[12];
  int byteReceived;
  //delay(5000);
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    byteReceived = Serial.readBytesUntil('\n', buf, 11);   // Keep room for null
    buf[byteReceived] = '\0';   // At this point buf[] hold the ASCII for the int that was entered
    Serial.print("Received: ");
    Serial.println(buf);
    Serial.print("To make the ASCII value an int: ");
    Serial.println(atoi(buf));
  // If you want to convert an int to ASCII, why are you reading the Serial object?
  // To convert:
    int val = 12345;
    
    itoa(val, buf, 10);
    Serial.print("val = ");
    Serial.println(val);    // Done...
  } 
}

I think the OP started with this rather poor code example, which is indeed the official example provided to demonstrate use of Serial.read(), and is posted on the Arduino reference site at http://arduino.cc/en/Serial/read

No need to use itoa(), quite obviously.

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

econjack:

I am trying to convert int to ASCII.

If that's the case, then why are you reading the Serial object?

The following code waits for an integer to be entered from the keyboard and the user to click Send. It also takes the arbitrary value of 12345 and converts it to ASCII.

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

  char buf[12];   int byteReceived;   //delay(5000);   // send data only when you receive data:   if (Serial.available() > 0) {     // read the incoming byte:     byteReceived = Serial.readBytesUntil('\n', buf, 11);   // Keep room for null     buf[byteReceived] = '\0';   // At this point buf[] hold the ASCII for the int that was entered     Serial.print("Received: ");     Serial.println(buf);     Serial.print("To make the ASCII value an int: ");     Serial.println(atoi(buf));   // If you want to convert an int to ASCII, why are you reading the Serial object?   // To convert:     int val = 12345;         itoa(val, buf, 10);     Serial.print("val = ");     Serial.println(val);    // Done...   } }

I guess I was not clear in my goal. I want numerical representation of the incoming data ( single variable ) and than convert it using stdlib itoa function to ASCII character.

Serial. read() just does what I want , per documentation, it returns "data" type "int". I do not want char ( ASCII) which read() will return when return variable type is char. readBytesUntil , again per documentation, returns char(s) and also writes char(s) into buffer. I need numerical value, byte or int for further real time analysis. Than ASCII for LCD and other UI stuff.

And I have no problems simulating real time test data. PS The sample code I build on fails to stress the return value depending on variable type. And yes, the "incomingByte" is not the best name for the data returned.

Thanks for all your help so far, but still no solution. Cheers Vaclav

Serial. read() just does what I want , per documentation, it returns "data" type "int".

Yes, it returns an int, but that is only so that it can return an error value if you try to read data that isn't there.

Otherwise it returns a byte that MAY be a character, depending on what is sending the data.

If you use the Serial Monitor application to send 123, what gets sent is three bytes, '1', '2', and '3', and you will need to call Serial.read() three times to get the three bytes. You will NEVER be able to call Serial.read() and get 123.

FYI The Serial.println(atoi(buf)); in this example does not work either.

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

  char buf[12];
  int byteReceived;
  //delay(5000);
  // send data only when you receive data:
  if (Serial.available() > 0) {
   
    // read the incoming byte:
    byteReceived = Serial.readBytesUntil('\n', buf, 11);   // Keep room for null
    Serial.print("byteReceived count ");
    Serial.println( byteReceived );
    
    buf[byteReceived] = '\0';   // At this point buf[] hold the ASCII for the int that was entered
    Serial.print("Received: ");
    Serial.println(buf);
    Serial.print("To make the ASCII value an int: ");
    Serial.println(atoi(buf));
  // If you want to convert an int to ASCII, why are you reading the Serial object?
  // To convert:
    int val = atoi(buf);
    
    itoa(val, buf, 10);
    Serial.print("val = ");
    Serial.println(val);    // Done...
  } 
}

FYI The Serial.println(atoi(buf)); in this example does not work either.

Proof? If you are going to make a claim like this, you need to show your serial output.

The code in message #9 prints:

byteReceived count 4
Received: 1234
To make the ASCII value an int: 1234
val = 1234

All of which is correct and as expected. What is wrong with "To make the ASCII value an int: 1234"?

Pete

My error, both functions are not C standard, so why should they work in Arduino IDE. Thanks for all the help I have received to solve this. Cheers Vaclav

My error, both functions are not C standard, so why should they work in Arduino IDE.

They are not part of the C standard, but every compiler manufacturer supplies them, and they DO work on the Arduino (which has nothing to do with the IDE).