how to read float number sent from matlab to Arduino

hi, there,

how to read floating numbers sent from matlab to Arduino?

For example:

If I send float number to arduino through serial, arduino will convert it into integer.

for example, in matlab: fwrite(a, 0.5). // write a float number to arduino
in arduino: Serial.read() return 1. NOT 0.5

if send 0.3, in arduino, Serial.read() return 0. NOT exact 0.3

how to solve this problem?

thanks.

Post the code you are using, using code tags (the "#" button) and perhaps someone will help.

My question is really not about any code, but about the exact arduino functions.

in Arduino Reference:
about Serial.read(): its return value seems integer; Shown as below quotes:

"the first byte of incoming serial data available (or -1 if no data is available) - int"

if I am correctly understand Serial.read() return properties, any float numbers send to serial port and received by arduino by using Serial. read() would be converted to integer.

I am wondering how do I send a float number to be received by arduino as it is?

for example,

in matlab:

a=serial('com3','Baudrate',115200)
fopen(a,'com3');
fwrite(a,'t');
fwrite(a,3.1415);
readout=fgets(a)

In Arduino:

float val;
float num;
void setup() {
 Serial.begin(115200);
}

void loop{
 if (Serial.available() >0) {
     val = Serial.read(); 
if (val=='t'){
    while(!Serial.available());
    delay(10);
    num=Serial.read();
    Serial.print("num:");
    Serial.println(num);  
  }
  }
}

try something like this:

if (Serial.available())
{
  float myFloat = Serial.parseInt();
}

it works best if you transmit a start and end marker like this:

t13.678x your float is bookended by a start of transmission and an end of transmission non numeric markers

and you would code your receipt like this:

if (Serial.available())
{
  char myChar = Serial.read();
  if (myChar == 't')
  {
    float myFloat = Serial.parseFloat();
  }
}

the trailing marker is disregarded.

In general idea arduino is all about ints... Everything it's actually doing is in INTs.. The question is how to convert or expression the needed int in different variable HEX or DEC or watever... So ... think.

Thanks BulldogLowell. I just test your method and it did work.
The only issue is : in matlab, I should convert num into string. and In arduino, parsefloat will work.

Slavka85 , I don't really get it.

Take my codes as example, in your view, how to convert the return of Serial.read() into float format directly and get the accurate inputs?
thanks

Im saying that the arduino does any operations and math in INTs everything you know any character inside the arduino is actually int...it's like a base for everything. So if you sending to sereal 0.3 as a char and for you it's like one didgit. But for arduino it actualy 3 ints. one = 0 two=- three = [3].. a nd they look like one =-48 in ints second and so on... So also if sending 0.3 Serial.read will read 1 by 1 you need then put in to the array if you want get int as o.3 or use Strings... So man read more

how to convert the return of Serial.read() into float format directly and get the accurate inputs?

The serial port sends/receives bytes without regard as to what they are supposed to represent. You need to capture the bytes sent to the serial port, then apply some function that turns the bytes sent into what you want the bytes represent. 0.3 is just three ascii characters just as the word the is three ascii characters. You need to convert the bytes into what you expect them to represent.

mouse123:
Thanks BulldogLowell. I just test your method and it did work.
The only issue is : in matlab, I should convert num into string. and In arduino, parsefloat will work.

great.

as discussed, arduino sees bytes coming in... it is up to you to parse the data into an int, float, string, String, etc...

Serial.parseInt and Serial.parseFloat are easy to use but not that flexible, if you want to do a lot with Serial.

Thanks all! I fully understand now and know flexibly dealing this issue. Appreciate
Cool guys.