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.