Trouble understanding float.

I can't seem to figure out the float command. I want to create an array of ten locations, but it seems like I need the println command to reveal the contents? Please advise. Cnt is the loop counter.

if I use

 My_Array[Cnt][Serial.println(inString.toFloat(),2)];

it prints to the serial monitor with the proper value. but when I run :

// Serial.println(FL_Array[1][5]);

the result is whacked? Please advise?

 float FL_Array[10][10];
 float FL_Sample;

...
{
         Serial.print("Input string: ");
         Serial.print(inString);
         inString  = (inString.substring(10,15)); 
         Serial.print("\tAfter conversion to float:");
         Serial.println(inString.toFloat(),2);  //,2 places no rounding
         
        //My_Sample = Serial.println(inString.toFloat(),2);
        //Serial.println(My_Sample,3 );

      //  My_Array[Cnt][My_Sample,2)];    

        My_Array[Cnt][Serial.println(inString.toFloat(),2)];
        // Serial.println(My_Array[1][5]);

What is it that you expect this line to do?

My_Array[Cnt][Serial.println(inString.toFloat(),2)];

Whatever you think it does, that's not what it actually does, and can't possibly compile as you've presented it.

What you've written is pretty much nonsense. So what is it you're really trying to achieve?

Regards,
Ray L.

Look at the documentation for the Serial.println() function. It returns the number of bytes send to the serial buffer. That is USELESS information to store in the array.

float theFloat = inString.toFloat();
// Store theFloat in the array.

and can't possibly compile as you've presented it.

Did you try? There is nothing wrong with the syntax of that line. The logic is stupid, but the syntax is fine.

PaulS:
Did you try? There is nothing wrong with the syntax of that line. The logic is stupid, but the syntax is fine.

Seriously???? Where do you see ANY code that stores anything to an array there??? That line ACCESSES one element of an array that is not defined anywhere in the code he posted, but then does nothing with the value retrieved, so it is completely useless, and WILL NOT COMPILE.

Regards,
Ray L.

int My_Array [2][10];
const int Cnt = 0;
String inString;

void setup()
{
  My_Array[Cnt][Serial.println(inString.toFloat(),2)];
}

void loop()

{}

Doesn't compile, you say?

Hmmm.

retexas:
I can’t seem to figure out the float command. I want to create an array of ten locations, but it seems like I need the println command to reveal the contents?

‘float’ is not a command, it is a data type, like int and char. Data types tell the compiler how much memory needs to be reserved.

To declare an array you use the syntax
[];

So, to declare 10 floats in an array named myArray
float myArray[10];

To assign a value to an element in an array, you must use the assignment operator ‘=’
myArray[0] = 1.414;

float myArray[10];
for( uint8_t i =0; i < 10 ; i++ ) {
  float aFraction = (float) i / 3.0;
  myArray[i] = aFraction;  //assign aFraction to an element in the array
}

Note the variable ‘i’ is declared as an integer type. The compiler has to be told to treat ‘i’ as a float in the calculation of aFraction, by ‘casting’ the value as, (float) i.

The println function is used to output strings of characters. An array of floats does not contain strings of characters. So, you need to convert the float value in the array, to a string of characters, which you can pass to println.

for(uint8_t i =0; i < 10; i++) {
  char lineBuffer[40];
  sprintf(lineBuffer, "%d / 3.0 = %.2f", i, myArray[i]);
  Serial.println(lineBuffer);
}

Note. The String object (you are using) includes methods to carry out various conversions. However, although the learning curve is a little steeper, I recommend using simple character arrays as it forces you to learn the fundamental programming concepts, of data type and storage.

The sprintf() function offers a standard function for formatting data values into strings of characters. The sprintf format specifier syntax can seem painful but is found in every implementation of C and several other programming languages.

The sprintf() function offers a standard function for formatting data values into strings of characters. The sprintf format specifier syntax can seem painful but is found in every implementation of C and several other programming languages.

Except that the Arduino implementation disables the %f format specifier...

Thank you Paul.

Good job you are here to point these details out.

How would you suggest the OP get around the limitation.

How would you suggest the OP get around the limitation.

The way everyone else does, by using dtostrf().

PaulS:
The way everyone else does, by using dtostrf().

Everyone! A rather rash generalisation.

    uint16_t bcdFraction = 1.414 * 100;
    char lineBuffer[40];
    sprintf(lineBuffer, "%d.%02d\n", (bcdFraction / 100), bcdFraction % 100);
    Serial.print(lineBuffer);

Everyone! A rather rash generalisation.

Well, everyone that wants to do it right, I guess. What does your output look like, if 1.414 changes to 1.417?

Thanks MattS-UK, PaulS for your adult response.

Reaching out on this forum can be just brutal! I’m not a programmer never calimed to be, but a simple hobbist just beginning in Arduino, and yes it compiles thank you very much! Arduino 1.6.6 Mega

Here is the big picture incase all you code wizards have a better way, than me just banging thru this module at a time.

I have an instrument where I read serial port, wait for /n, then parse the string to eliminate the text using substring to get to my float value, (1.234). That all seems to work fine, and now I’m at the point, what to do with the data collected. The big goal is to create an array (I assume) with a depth of ten values, and then find the average of the collection. I will save the unparsed collection to an Sd card and display the average on a touch screen. My adventures are just beginning, so be kind!

I believe I’m at the point where I needed to build an array with the data that I get coming acrost the serail1 read. I was able to see the proper result using println, just could not move it into a valid variable.

I probably need a better understanding of Char, as I was thinking more along the lines of an ascii char. Where I was receiving char from the serial port and adding it to a string, and then using substring to get the desired data. Once I moved data into the string, I thought I was done with “Char”. I was also thinking I needed a 2d array.

I will go play with your approach and certainly appreciate the detail of the response.
Attached is the code block albeit filled with errors…

#include <SPI.h>


 float My_Array[10][10];
 float My_Sample;
 int Cnt;
 char character; //Single character
 String dataString = ""; 
 String inString = "";    // string to hold input

void setup() {
   // Open serial communications and wait for port to open:
   Serial.begin(9600);
   while (!Serial) {
     ; // wait for serial port to connect. Needed for Mega only
   }
    Serial1.begin(4800,SERIAL_7E2 );
    Serial.println("S1 4800,SERIAL_7E2");
    Serial.println("End Setup");
}

void loop() 
{
     for (int Cnt = 0; Cnt < 10; Cnt++) //ten samples
  { 
    Serial1.write("Grab");        //CMD
    Serial1.write(13);           //CR
    Serial1.write(10);           //LF
    delay (3000);                // Delay for Instrument read
      
    // Read serial input:
   while(Serial1.available()>0)  //Instrument Available
   {

     int character = Serial1.read();
     if (character != '\n') 
     { 
       // As long as the incoming byte is not a newline, "/n"
       // convert the incoming byte to a char and add it to the string
       inString += (char)character;
     }
       // if you get a newline, print the string, then the string's value as a float:
     else 
       {
         if(inString !="ER00"); //Not Instrument read Error
        {
         Serial.print("Input string: ");
         Serial.print(inString);
         inString  = (inString.substring(10,15)); 
         Serial.print("\tAfter conversion to float:");
         Serial.println(inString.toFloat(),2);  //,2 Places no rounding
         
        //My_Sample = Serial.println(inString.toFloat(),2);
        //Serial.println(My_Sample,3 );
       
        My_Array[Cnt][Serial.println(inString.toFloat(),2)];
        // Serial.println(My_Array[1][5]);
         
         // My_Sample=0;
         //  BuildArc();
         // clear the string for new input:
         inString = "";
        }
      }
     }
     Serial.print("Count: ");
     Serial.println(Cnt);
     if(Cnt == 9) {My_Sample =0;}
   }
}
if(inString !="ER00");

Looks like floats aren't the only thing you're having trouble with

My_Array[Cnt][Serial.println(inString.toFloat(),2)];

That's crazy.
This has already been stated.

         Serial.println(inString.toFloat(),2);  //,2 Places no rounding

Do you understand what this code is doing? How many functions are being called?

       My_Array[Cnt][Serial.println(inString.toFloat(),2)];

The value returned by Serial.println() is being used as an index into the array. That value is the number of characters written to the serial port. You might as well make that statement read:

       My_Array[Cnt][6];

That code doesn’t actually do anything. It is missing an assignment operator.

Funny @ Groove! (you have no Idea!)

 My_Array[Cnt][Serial.println(inString.toFloat(),2)];

I thought I was looping thru using my loop counter, and at my last attempt before reaching out to the boards, was that I know println, was getting me the desired result on the monitor, but inString.toFloat(),3 was not getting me a usable variable result the best I could determine

but inString.toFloat(),3 was not getting me a usable variable result the best I could determine

Try this:

float crap = inString.toFloat();
Serial.print("crap: ");
Serial.println(crap);

Serial.print("crap to 25 places: ");
Serial.println(crap, 25);

Change numbers around, and see what happens. LEARN what each argument to each function call ACTUALLY does. QUIT GUESSING!

Funny, a wizard with a sense of humor!

Will give it a go!

Thank you very much, makes perfect sense!

I actually went down this road, but the key for me was never getting the array built correctly, thinking it was 2d (count/value) when actually I was failing to assign the value to the count. Still needed the substring to parse. Works as expected now!

My_Array[Cnt]=Crap,3; // Loop counter, value @ 3 decimal pl.

[/
       {
          inString  = (inString.substring(10,15)); 
          float Crap = inString.toFloat();
          Serial.print("Crap to 3 places: ");
          Serial.println(Crap,3);
          My_Array[Cnt]=Crap,3;
          inString = "";
        }
code]
My_Array[Cnt]=Crap,3;  // Loop counter, value @ 3 decimal pl.

No, that is NOT setting MyArray[Cnt] to Crap @ 3 decimal places. You don't get to make up your own syntax in c++. That line is setting MyArray[Cnt] to 3, with however many decimal places it has (which is always 7-8 for a float). the "Crap," is accomplishing absolutely nothing, and is effectively ignored by the compiler.

You won't get very far with programming if you don't take the time to study and learn actual c++ syntax, and stop making up your own....

Regards,
Ray L.