how to convert string coming from serial port to float num


hi, i'm trying send data through serial port, data float number
what planning convert these data coming serial port float in order apply mathematical , logical manipulation inside arduino
for example when send 1 want return 1 , 500 return 500 in 1 line , defined float
i tried use code found here gives me error
the code arduino
code: [select]

int val;
void setup() {
 
  // put setup code here, run once:
  serial.begin(9600);

}

void loop() {
  while(serial.available() == 0 );
  val = serial.read();
  serial.println(val);
  char carray[val.length()+1];
  val.tochararray(carray,sizeof(val));
  int n = atoi(val);
  serial.println(n);
 

}


code: [select]
arduino: 1.6.1 (windows 8.1), board: "arduino uno"

serial.ino: in function 'void loop()':

serial.ino:14:19: error: request member 'length' in 'val', of non-class type 'int'

serial.ino:15:7: error: request member 'tochararray' in 'val', of non-class type 'int'

serial.ino:15:19: error: 'carray' not declared in scope

serial.ino:16:19: error: invalid conversion 'int' 'const char*' [-fpermissive]

in file included c:\program files (x86)\arduino\hardware\arduino\avr\cores\arduino/arduino.h:23:0,

                 from serial.ino:1:

c:\program files (x86)\arduino\hardware\tools\avr\avr\include\stdlib.h:274:12: error:   initializing argument 1 of 'int atoi(const char*)' [-fpermissive]

 extern int atoi(const char *__s) __attr_pure__;

            ^

error compiling.

  report have more information with
  "show verbose output during compilation"
  enabled in file > preferences.

any appreciated

you try use functions of string class on value of type int.

serial.read reads 1 characters, , removes input buffer.

store each of these characters in char arrays, , use function atof convert string float.

alternatively, can use serial.parsefloat().


Arduino Forum > Using Arduino > Programming Questions > how to convert string coming from serial port to float num


arduino

Comments