Serial read conversion to number

I have an application where I want to read a string from the serial port when one appears there, and convert it to a number which I can then use in further calculations.

The string will always be a number between -90.0 and 90.0 and will not be a fixed length e.g. it could be -87.3 or 7 or 34 or 3.6. It is not terminated with a CR. The string will be sent to the serial port at irregular intervals, but the time between strings will one minute of more. Hence rapid processing is not an issue

void setup() {
 lcd.begin(16, 2);
 Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
 Serial.setTimeout(50); // 50ms timeout for reading serial data
}

void loop() {
    if (Serial.available() > 0) {  //if there's data at the serial port
         lcd.print (Serial.readString());   //read it and show it on the LCD display
         // Here I'd like to convert the string into a number to be used in calculations
    } 
  delay (2000);
  lcd.clear();
}

I have no problem reading the string and sending it to an LCD display as shown in the code above. My problem is saving the string as a floating point number, though saving it as an integer would also be acceptable.

I'm confused about how strings are handled, the difference between a string and a byte array and the various commands which are supposed to convert them to integers or floating point numbers! Nothing I have tried seems to work (compile errors), so I clearly don't understand what I'm doing.

Hello KA1GT,

I suggest you read Robin2's excellent Serial input Basics

The first problem you have is you are reading the input and immediately sending it to the display without saving it anywhere, so you've lost it, so you can't convert it to anything. Hopefully Robin's tutorial will tell you what you need to know.

You are going to get stuck on timing, because you are depending on it. That is because you say you are not using any delimiters. For examples, your "7" and "3.6". That arrives at the computer as "73.6". If you want to separate them, you have to introduce a time-out delay to function as a de facto delimiter. I don't recommend it, especially as a beginner it will cause you no end of headaches. Can you change your transmission logic to separate the values at least by a ","?

I'll just give you one example of a pitfall. It will give you latency. Suppose you receive a "7". But what if that is just the beginning of "73"? So you have to wait, let's say 10 seconds. The time passes and after 9 seconds, you get a "3". Great, but it might be a "73." so you have to wait again. Sure enough, after 1 second you get a ".8" and you begin the time out again. If the 10 second time out is reached this time, you know by your rules that it must be complete and you have, "73.8". With this scheme, you will have at best a 10 second delay between receiving a value and registering it (recognizing it and being able to use it). Also on the transmit end, you are now prohibited from having any transmission delay whatsoever, that might exceed the time out period.

floatVar = Serial.parseFloat();

It will read characters until it finds a number followed by a non-number or receives no characters for one second. If you get a value of 0.0 it means no number was found before the timeout.