Good day
I am new to Arduino. I have an issue with the data.toFloat();.
I receive data from serial "Level:60 Depth:300 end"
I then extract the 60 & save it into a float (I have tries integer, double , etc)
When I send an updated value for example "Level:70 " and do the data.toFloat() it corrupts & saves a 0
I have to change the 60 from a string to float (or whatever ) to do a simple calculation.
I have attached the serial output where you can see the second time round LEVEL2 is 0 it should have been 70.
Is there an obvious issue or not?
Regards
//Define strings
String readString, data;
//Define floats
float LEVEL; //Current dam level in percentage
void setup() {
Serial.begin(9600);
Serial.println("Serial port is open");
// Check to see if the serial port is operational
}
void loop() {
//String to be sent by user needs to be: "Level:60 Depth:300 end"
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ' ') { //Reads sting until the "space"
Serial.println(readString); //prints string to serial port out
if (readString.indexOf("Level:") >= 0) {
data = readString.substring(6); //Read everything after the 6th digit up to the space and save it to data
Serial.println(data); //Test print data
Serial.print("LEVEL1 : ");
Serial.println(LEVEL); //Test print LEVEL to see what is saved in LEVEL
LEVEL = data.toFloat(); //Change the string data read to an float & save it to LEVEL
Serial.println(data); //Test print data
Serial.print("LEVEL2 : ");
Serial.println(LEVEL); //Test print LEVEL to see if the value changed
}
readString = "";
//clears variable for new input
data = "";
//clears data for new input
}
else {
readString += c; //makes the string readString
}
}
}