I am using pressure sensor with Arduino Uno within Processing. Given these variables:
Serial myPort; //the serial port
float sensorValue = 0; //the value form the sensor
Arduino is printing float numbers on Processing console, according to the snippet below.
(...)
void serialEvent (Serial myPort){
//get the ASCII string
String inString = myPort.readStringUntil('\n');
if (inString != null){
//trim off any whitespace:
inString = trim(inString);
//convert to an int and map to the screen height
sensorValue = float(inString);
println(sensorValue);
sensorValue = map(sensorValue, 0, 1023, 0, height);
//println(sensorValue);
However, I am getting a NaN
exception:
sensorValue = map(NaN, 0, 1023, 0, height);
how is this possible? printing numbers that aren't numbers?
1 Answer 1
//convert to an int and map to the screen height sensorValue = float(inString);
You can't convert a String to a Float just like that by casting. You have to interpret the characters within the String. Oh, and a float isn't an integer.
All you're doing there is trying to cast the address of the String object to a float, which it really doesn't like, hence you get NaN.
Try:
int sensorValue;
sensorValue = inString.toInt();