My python script prints these two lines
[10,20,40,600]
1.67422771454
For the first line, the Arduino sketch reads it using ParseInt() and everything is working fine. The second line is a double. how can I read it?
I tried with parseFloat() but it's not working, it's returning random values like
0.0
0.1
-0.1
0.3
but if I simply try with readString(), it is reading it well and returning the string 1.67422771454.
How can I read a double? Or how can I convert the string into a double?
Part of the sketch
while (proc.available()){
if((char)proc.read() != ']'){
array[i] = proc.parseInt();
Serial.println(array[i]);
i++;
}
else{
of = proc.parseFloat();
//of = proc.readString();
Serial.println(of);
break;
}
While it took me a moment to figure out what the code was trying to do, I don't see anything wrong with it. My first thought was that there is a newline character after the ']' that might be confusing things, even though parseFloat() is supposed to ignore leading whitespace. I tried adding a read() to throw away the newline before calling parseFloat() but it made no difference.
I've not used parseFloat() before, but it seems to me like your code should work. Hopefully someone else will see something?
I had to do this:
String next = proc.readString();
char buf[next.length()];
next.toCharArray(buf, next.length());
double off=atof(buf);
But I'd like to understand why parseFloat() it's not working.
mridolfi:
Part of the sketch
...
of = proc.parseFloat();
//of = proc.readString();
Serial.println(of);
break;
}
...
Try :
...
String Value = proc.readString();
float of = Value.toFloat();
...
Or:
...
of = proc.readString().toFloat();
...
of = proc.readString().toFloat();
works!
Thank you