Converting an Array to an Int

Hi all,

I have a Windows program which sends data to my arduino once a second via a serial port. The data sent to the arduino is simply 3 numbers which are terminated with a carrige return.

arduino captures the serial data into and array. I've then tried converting the array value using the atoi command, but it seems to stop my program running.

I would be grateful for any suggestions on how to convert the serial to a int value or how to get the atoi option workingl

My code is as follows.

const int MAX_LEN = 3; // the max digits in the number plus decimal point
char  strValue[MAX_LEN + 1]; // add one for the terminating null

int index = 0;

void loop()
{
  millis();
    GetAz();
    ButtonPress();
    llegirPos();
    delay(100);
}


 void GetAz(){
  if( Serial.available())
  {
    char ch = Serial.read();
    if(index < MAX_LEN && ch != 13) // 13 is ascii value of carriage return
    {
      strValue[index++] = ch; // add the ASCII character to the string;
    }
    else
    {
      // here when buffer full or on the first non digit
      strValue[index] = 0;        // terminate the string with a 0
        int value = atoi(strValue);  // use atof to convert the string int
      index = 0;      // reset the index for the next string

        lcd.setCursor(10,3);
      Serial.println(strValue);
        lcd.print(strValue);


    }
  }
 }

Rich.

what inputs have you tried over the serial port and what has the response been?
I'm wondering if maybe the serial program you're using isn't actually sending the carriage return character.

Assuming there is at least one character that is not a digit seperating the numbers, you could do something like this:

int value = 0;

void GetAz(){

  if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9')              // is ch a number?  
      value = value * 10 + ch - '0';           // yes, accumulate the value
    else  
    {
      if(value > 0)
      {
        lcd.setCursor(10,3);
        Serial.println(value);
        lcd.print(value); 
      }
      value = 0; // reset the value on a non digit
    }
  }
}

Unfortunately all characters are numbers.

I'm my code, if i do a serial print of strValue then it prints perfects, it's only when i do the atoi of this that it fails.

the numbers sent via the serial port are degree beardings (e.g. 1 - 360) and are terminated with a carrige return.

Rich

mem's suggestion could still be made to work, if you have control of the sending program. If the sender always sent 3 digit numbers (360 or 012 or 005), you could simply loop to read all 3 digits. If the program sent "360" or " 12" or " 5", you could treat the spaces as 0s.

Unfortunately all characters are numbers.

13 (the carriage return) is not an ascii numeric digit and will identify the end of the numeric sequence. Did you try the code? ( I must admit I have not, but if you are sending a carriage return between values it should work)

Hi Mem,

I tried your code but it didn't work, i'm now not getting any values showing at all.

I might try using a for loop, and see if i can figure out something that way.

Rich.

can you print the value that are coming in.

char ch = Serial.read();
Serial.print(ch,DEC); // add these line
Serial.print(",");

mem i added the lines to your code, and all i get from the serial monitor is

51,51,51,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-

mem i added the lines to your code, and all i get from the serial monitor is

51,51,51,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-

It looks like you are missing the carriage return (ascii 13) that you were expecting. You need some way to delimit the messages you receive, otherwise you can not be sure you are starting at the beginning of a message. What is sending the data?

Also, where are those -1 values coming from? are you running the code I posted?

Hi mem it's a windows Vb application i wrote. I think it's definately sending it, as the carrige return is what is used to store the data in the array strValue which works perfectly when doinf a serial serial print with my code.

I'm wondering if i can add the ascii value manually in your look then try the atoi function.

51,51,51

'3' '3' '3'

i am not sure what you mean here:

I'm wondering if i can add the ascii value manually in your look then try the atoi function.

My advice is to get the incoming data working as expected. If you are sending an carriage return, why is the read routine not displaying it. Can you post the code that you used to get the output in reply #9

AWOL, we know that was 3 threes. We wan't to find out what happened to the carriage return that is supposed to follow and why -1 is being printed if Serial.available is being called before every read.

The code looks OK, should work. Is this the whole sketch or do you do use much RAM in the remaining functions?

My gut feeling says that this could be an out of memory, stack overflow problem.

Guys,

My apologies, it looks like my source sending the data wasn't actually sending the carrige return after all. it looks like i forgoat to save some changes in my code before closing, and then came back and compiled the next day missing the carrige return.

Thanks for your patience everyone.

Rich.