Pages: [1]   Go Down
Author Topic: Char to float conversion  (Read 2355 times)
0 Members and 1 Guest are viewing this topic.
Planet Earth
Offline Offline
Newbie
*
Karma: 0
Posts: 36
I hope I didn't brain my damage...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

0
Offline Offline
Shannon Member
****
Karma: 129
Posts: 10441
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Code:
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;
          }
        }
      }
    }
  }
 
}
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Georgina Ontario
Offline Offline
Sr. Member
****
Karma: 5
Posts: 437
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Just another Hacker

Planet Earth
Offline Offline
Newbie
*
Karma: 0
Posts: 36
I hope I didn't brain my damage...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

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

void loop()
{
}

Quote
Output = 231.07
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Planet Earth
Offline Offline
Newbie
*
Karma: 0
Posts: 36
I hope I didn't brain my damage...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


hey, it works....thanks a lot! smiley
Logged

Pages: [1]   Go Up
Jump to: