data.toFloat() Problem

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
    }
  }
}

Capture.JPG

The serial input basics tutorial shows how to receive of different data types into a null terminated character array (string). Using Strings can cause memory fragmentation.

Thank you for the reply

I think this is a better approach. I will test it.

Example 5 - Receiving and parsing several pieces of data

Regards

1. Send message from the InputBox of Serial Monitor in a meaningful way and then extract the components of the received message in logical way. Your message could be sent in the following format:

Level:60:300,Depth

Wehre:
< (left angle) marks the beginning of message.
, (comma) is placed as a separator between data items of the message.

(right angle) marks the end of message.

2. Let us develop the sketch without using String ClassName which creates memory holes in the flash space of 8-bit AVR.
Algorithm: Check that '<' has arrived and then receive all the characters until '>' arrives. After that isolate the data items: Level:60 and Depth:300 and show them on Serial Monitor. If you want to save the numerical values of Level/Depth into integer type variables, then you have to add codes with the sketch so that you can apply this instruction: int Level = atoi(myLevel);.

The sketch: (tested on UNO)

bool flag = false;
char myData[50] = "";
char myLevel[25];
char myDepth[25];
int i = 0, j = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    if (flag == false)
    {
      char x = Serial.read();
      if (x == '<') //< is detected
      {
        flag = true;
      }
    }
    else
    {
      byte m = Serial.readBytesUntil('>', myData, 20);
      myData[m] = '\0';   //insert null character
      Serial.println(myData);
      do
      {
        myLevel[i] = myData[j];
        i++;
        j++;
      }
      while (myLevel[i - 1] != ','); //
      myLevel[i - 1] = '\0';
      Serial.println(myLevel);  //shows: Level:60
      //----------------------
      i = 0;
      do
      {
        myDepth[i] = myData[j];
        i++;
        j++;
      }
      while (myDepth[i - 1] != '\0');
      myDepth[i - 1] = '\0';
      Serial.println(myDepth); //shows: Depth:300
    }
  }
}