How to do the loop logic when converting from char array to float array?

Hi,

I am kind of lost at the logic. I am reading chars from HTTP server which has my data.

The data example [34.45 33.21 32.12 35.42]. So after each data there is space.

This is how I read the data. i is just a counter.

while(client.available()){  
      char c = client.read();
      Serial.print(c);
      dataArray[i] = c;
      i++;
      if (c == ' '){
        Serial.println();
      }
}

What I want is I will read the data until space comes then that will be my first variable of an float array. I am lost in loops and couldn't figure out the logic.

Put each character into an array of chars as it is received. When it is complete split the array into individual variables or elements of an array of floats using the strtok() function

@UKHeliBob I tried to do that, but there is 35000 variables, and Arduino hangs after some time.

Are you trying to store the values of 35,000 float variables ? Which Arduino are you using ?

Yes if possible. Arduino Nano RP2040 Connect.

Why did you not supply the full details in your original post ?

Yes, I realized that, I was editing my post, but you replied so fast, so I cancelled and replied to you. I can do it without storing as well. So until space I will get as chars, convert it to float , use it for something, then delete it. This is also fine.

Please accept my apologies for replying so fast

1 Like

No worries I accept.

the following code recognizes and returns a float as it is received

int
readFloat (
    float &f )
{
    static char line [80];
    static int  idx = 0;

    if (Serial.available ())  {
        char c = Serial.read ();
        if ('\n' == c || ' ' == c)  {
            line [idx] = '\0';
            idx = 0;
            f = atof (line);
            return 1;
        }
        line [idx++] = c; 
    }
    return 0;
}

void
loop (void)
{
    float f;
    if (readFloat (f))
        Serial.println (f);
}

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

@gcjr Thank you so much. It is all clear now.

Except the fourth one which has a ']'?

You can probably use:
float value = client.parseFloat();
That will skip characters until it finds a number and read the number until it finds a character that isn't part of a number.

Just in case you were not aware, atof returns 0.0 if the line does not parse correctly.
May not be a problem in your case if 0.0 is not a valid input OR you can guarantee to never get any input data errors.
If you want a pre-packaged function that does test for a valid float, take a look at my SafeString library which has the
unsigned char toFloat(float& fResult);
method which returns non-zero and updates the fResult ONLY if the SafeString contains a valid float with optional leading and trailing white space but nothing more.

Hi @johnwasser. This is actually really useful. However there is some gibberish data coming. I couldn't find the reason. I already read if there is any data left in the buffer via

void serialFlush(){
  while(Serial.available() > 0) {
    char t = Serial.read();
  }
}

Then in the loop:

  serialFlush();
  while(client.available()){  
    char c = client.read();
    //Serial.print(c);
    float data = client.parseFloat();
    //dataArray[i] = c;
    //i++;
    Serial.println();
    Serial.println(data);
  }

However this is read from the serial:

1.00
200.00
0.60
3.70
3.00
3.00
2021.00
9.00
54.00
17.00
0.00
37.94

37.94 is where my actual data starts, then it goes until my data ends. What might be the reason? Something from the HTML maybe?

Yes. That looks like the numbers you might find in an HTML header. The '200' means success. The '2021' might be part of a date. Was the time 9:54:17?

Yes exactly, it makes all sense now. Thanks.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.