Go Down

Topic: Char to float conversion (Read 2 times) previous topic - next topic

Bachna

Hey,

I have some data in a char array, lets say, char string[] = "231.067521"

I want to convert it to float and store in a variable (temp).

I tried atof but that yields only:  temp = 231.07

I need more no of digits, please suggest some efficient method for implementation in Arduino.

Thanks...

lesto

this code works for flaot and negative number...
But is for serial, the input must be *a char:value
you can easily extrapolate the code you need :)

Code: [Select]
char input1;
char input2[10];
int indexInput2=0;
boolean inputValue=false;
boolean negativo=false;
void readSerial(){
  /*
  input data: *name:value\n
  */
  while (Serial.available() > 0){
    char c = Serial.read();
    if (c =='*'){
      indexInput2=0;
      inputValue=false;
      negativo=false;
//      Serial.println("starting input");
    }else{
      if (c =='\n'){
        input2[indexInput2]='\0';
//        Serial.print("value: ");
//        Serial.println(input2);
        int point=0, i=0;
        float ris=0;
        while(input2[i]!='\0'){
          if (input2[i]=='.'){
            point=indexInput2-i-1;
          }else{
            ris=(ris*10)+input2[i]-'0';
          }
          i++;
        }
/*     
        Serial.print("ris: ");
        Serial.println(ris);
        Serial.print("point: ");
        Serial.println(point);
*/
        ris/=pow(10, point);
        if (negativo){
          ris*=-1;
        }
        executeCommand(input1, ris);
      }else{
        if (inputValue){
          if (indexInput2==0 && c=='-'){
            negativo = true;
          }else{
            input2[indexInput2]=c;
            indexInput2++;
          }
        }else{
          if (c ==':'){
//            Serial.println("command ok, waiting value");
            inputValue=true;
          }else{
            input1=c;
          }
        }
      }
    }
  }
 
}
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

PaulS

Quote
I tried atof but that yields only:  temp = 231.07

How do you know that atof truncated the data? I don't believe that it did. Prove it.

It's much more likely, to a near certainty in fact, that you caused the truncation when printing the value.

WillR


Quote
I tried atof but that yields only:  temp = 231.07

How do you know that atof truncated the data? I don't believe that it did. Prove it.

It's much more likely, to a near certainty in fact, that you caused the truncation when printing the value.


Nullo Contendo!

No contest -- he's right!
Just another Hacker

Bachna


hey Paul, I tried the following code, please let me know if it's my mistake...

Code: [Select]

char data[] = "231.065792";
float temp;

void setup()
{
  Serial.begin(9600);
  temp = atof (data);
  Serial.println(temp);
}

void loop()
{
}


Quote
Output = 231.07


PaulS

Quote
please let me know if it's my mistake...

It is. The Serial.print(ln)() function has optional arguments that influence how it outputs numbers as strings, for various kinds of numbers.

For floats, the 2nd argument defines how many decimal places to display. The default is 2. Hey, that matches what you see.

Serial.println(temp, 8); would have displayed different results.

Bachna


hey, it works....thanks a lot! :)

Go Up