"array-float" conversion problem

I have two text files saved on an SD memory. In one of them I have values of an X variable and in the other of a Y variable. The values of each variable are separated by commas. So what I want is to read each new value of X and Y and use it in another function to do calculations. For this it has occurred to me to make two functions to read a new value of X and Y in each call to the function. Basically, I read each digit (or byte) and store it in an array until there is a comma. Then, so that it can be used as a number to do calculations, I convert the byte array to a float type. However, I get a conversion error or something similar (you can see it in the screenshot below). Does anyone know what the error is? Thank you.

UPDATE
In the function "next_Y_Z" my idea is to numerically integrate the data "Y" and "f_X". In this way, we get the data "Z" which is needed together with Y to calculate "A" in the "void loop()" function.

#include <SPI.h>
#include <SD.h>

#define SSpin 10

File X_file;
File Y_file;

int i;

float X[2], Y[2] = {0};
float Z = 0;



float X_values() {
  byte digits_X[20];                           // Stores the digits of the new value of X
  float X;                                     // New value of X
  int read_point_X = 0;
  X_file = SD.open("X_values.txt");
  if (X_file) {
    i = 0;
    X_file.seek(read_point_X);                 // We set the current reading point to that of the last reading
    while (X_file.read() != ",") {
      digits_X[i] = X_file.read();
      i++;
    }
    read_point_X = X_file.position();          // We save the last reading point
    X = float(digits_X);
    X_file.close();
  }
  return X;
}

float Y_values() {
  byte digits_Y[20];
  float Y;
  int read_point_Y = 0;
  Y_file = SD.open("Y_values.txt");
  if (Y_file) {
    i = 0;
    Y_file.seek(read_point_Y);
    while (Y_file.read() != ",") {
      digits_Y[i] = Y_file.read();
      i++;
    }
    read_point_Y = Y_file.position();
    Y = float(digits_Y);
    Y_file.close();
  }
  return Y;
}


float next_Y_Z () {

  int n_samples = 0;
  const float X1 = 100e-6;
  float f_X[2];

  if (n_samples < 2) {
    for (int j = 0, j < 2, j++) {
      X[j] = X_values();
      Y[j] = Y_values();
    }
    n_samples = 2;
  } else {
    X[1] = X_values();
    Y[1] = Y_values();
  }

  f_X[0] = (X[0] - X1) / X1;
  f_X[1] = (X[1] - X1) / X1;
  Z = Z + (Y[1] - Y[0]) / 2 * (f_X[1] + f_X[0]);
  X[0] = X[1];
  Y[0] = Y[1];

}


void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(SSpin)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}


void loop() {
  float A;

  A = Y[0] + Z;
  Serial.println(X[0]);
  Serial.println(Y[0]);
  Serial.print(A);
  next_Y_Z ();

}
Arduino:1.8.19 (Windows 10), Tarjeta:"Arduino Uno"





















C:\Users\Alfonso\Documents\SD_test\SD_test.ino: In function 'float X_values()':

SD_test:29:23: error: invalid cast from type 'byte* {aka unsigned char*}' to type 'float'

     X = float(digits_X);

                       ^

C:\Users\Alfonso\Documents\SD_test\SD_test.ino: In function 'float Y_values()':

SD_test:48:23: error: invalid cast from type 'byte* {aka unsigned char*}' to type 'float'

     Y = float(digits_Y);

                       ^

C:\Users\Alfonso\Documents\SD_test\SD_test.ino: In function 'float next_Y_Z()':

SD_test:62:23: error: expected ';' before '<' token

     for (int j = 0, j < 2, j++) {

                       ^

SD_test:62:23: error: expected primary-expression before '<' token

SD_test:62:31: error: expected ';' before ')' token

     for (int j = 0, j < 2, j++) {

                               ^

exit status 1

invalid cast from type 'byte* {aka unsigned char*}' to type 'float'



Este informe podría contener más información con
"Mostrar salida detallada durante la compilación"
opción habilitada en Archivo -> Preferencias.

Use the atof() function to convert a string representation of the float number to a float number.

Screenshots of code are nearly worthless. If you want more help read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please fix your original post.

1 Like

I just added all the code with proper formatting and error message. Thanks for the tips!

1 Like

Thanks, that makes it easier to help you.

C++ uses semicolons in the for structure not commas. Coming from basic that threw me a few times, too.
for (int j = 0; j < 2; j++)

You need to call a function to convert a string of characters into a float value. An easier method is file.parseFloat() which will accumulate the digits and do the conversion in one step. For example, your X_values() function becomes:

float X_values()
{
  float X;                                     // New value of X
  static int read_point_X = 0;
  X_file = SD.open("X_values.txt");
  if (X_file)
  {
    X_file.seek(read_point_X);                 // We set the current reading point to that of the last reading
    X = X_file.parseFloat();
    read_point_X = X_file.position();          // We save the last reading point
    X_file.close();
  }
  return X;
}

Note: You can speed up your sketch quite a bit by opening the files in setup() and leaving them open. then you don't have to re-open the file for every value and re-position for each read. This is an example of how that might be done:

#include <SPI.h>
#include <SD.h>

#define SSpin 10

File X_file;
File Y_file;

float X[2], Y[2];
float Z = 0;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(SSpin))
  {
    Serial.println("SD initialization failed!");
    while (1) ;
  }
  Serial.println("SD initialization done.");

  File X_file = SD.open("X_values.txt");
  if (!X_file)
  {
    Serial.println("Failed to open X_values.txt");
    while (1) ;
  }

  File Y_file = SD.open("Y_values.txt");
  if (!Y_file)
  {
    Serial.println("Failed to open Y_values.txt");
    while (1) ;
  }


  X[0] = X_file.parseFloat();
  Y[0] = Y_file.parseFloat();

  while (X_file.available())
  {
    const float X1 = 100e-6;

    X[1] = X_file.parseFloat();
    Y[1] = Y_file.parseFloat();

    float f_X[2];


    f_X[0] = (X[0] - X1) / X1;
    f_X[1] = (X[1] - X1) / X1;
    Z = Z + (Y[1] - Y[0]) / 2 * (f_X[1] + f_X[0]);

    float A = Y[0] + Z;
    Serial.print(X[0]);
    Serial.print(", ");
    Serial.print(Y[0]);
    Serial.print(", ");
    Serial.println(A);

    X[0] = X[1];
    Y[0] = Y[1];
  }

  Serial.println("Reached end of file.");
  X_file.close();
  Y_file.close();
}

void loop() {}
1 Like

Thank you very much for the help and the example, now I understand better. From what I see, the .parseFloat() function converts to a float value only the next number in the file and not every number in the file. That is, it identifies commas as separators between numbers and they help you know where each one ends. Is this how it would work?

Yes. It looks for the next stretch that looks like a 'float' value and returns that.

Perfect, thanks!

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