Getting CSV files from SD card into variables. Need help with the val part

Ok, so I have the values pasting doubles from my sd card. What I would like to do is where the serial prints are is to assign those values to variables. So basically what I want is

to make them x1,y1,z1,h1 then the next line = x2,y2,z2,h2 etc. Is that possible? Basically in my void look i would like to be able to do math based on the values read, so like x2+y3-h1= answer.

Anyone have any ideas of how to get this to work?

Thanks

#include <SD.h>
#include <SD_t3.h>
File file;

bool readLine(File &f, char* line, size_t maxLen) {
  for (size_t n = 0; n < maxLen; n++) {
    int c = f.read();
    if ( c < 0 && n == 0) return false;  // EOF
    if (c < 0 || c == '\n') {
      line[n] = 0;
      return true;
    }
    line[n] = c;
  }
  return false; // line too long
}

bool readVals(double* v1, double* v2, double* v3 , double* v4) {
  char line[40], *ptr, *str;
  if (!readLine(file, line, sizeof(line))) {
    return false;  // EOF or too long
  }
  *v1 = strtod(line, &ptr);
  if (ptr == line) return false;  // bad number if equal
  while (*ptr) {
    if (*ptr++ == ',') break;
  }
  *v2 = strtod(ptr, &str);
    while (*ptr) {
    if (*ptr++ == ',') break;
    }
  *v3 = strtod(ptr, &str);
    while (*ptr) {
    if (*ptr++ == ',') break;
    }
  *v4 = strtod(ptr, &str);
    while (*ptr) {
    if (*ptr++ == ',') break;
    }
     return str != ptr;
}

void setup(){
  double x, y, z , h;
  int counter =0;
  Serial.begin(9600);
  if (!SD.begin(BUILTIN_SDCARD)) {
    Serial.println("begin error");
    return;
  }
  file = SD.open("TEST.CSV", FILE_READ);
  if (!file) {
    Serial.println("open error");
    return;
  }
  while (readVals(&x, &y , &z , &h)) {

    Serial.print("x: ");
    Serial.println(x);
    Serial.print("y: ");
    Serial.println(y);
    Serial.print("z: ");
    Serial.println(z);
    Serial.print("h: ");
    Serial.println(h);
    Serial.println();
  }

  
  Serial.println("Done");
}
void loop() {}

to make them x1,y1,z1,h1 then the next line = x2,y2,z2,h2 etc. Is that possible?

No. You could assign the values to arrays, so you would have x[0], x[1], etc.

So when I go to readvals

while (readVals(&x, &y , &z , &h)) {…

how do I go about assigning them. Because right now if I type x[0] it pitches an error that

“invalid types ‘double[int]’ for array subscript”

I assume I would need a counter on the while so after it assigns on the first loop it goes from x[0] to x[1].

I’m not the most versed in arrays usage.

How would you call

x = &x
y = &y
z = &z
h = &h
i++
you cant do that in that while loop can you? I just dont know how the &x works

So when I go to readvals

You don’t “go to” readvals. You call readVals().

The readVals() function re-assigns values to the variables passed by reference. You can then assign those values to elements of an array.

double xs[10], ys[10], zs[10], hs[10];
double x, y, z, h;

byte i = 0;
while(readVals(&x, &y, &z, &h) && i < 10)
{
   xs[i] = x;
   ys[i] = y;
   zs[i] = z;
   hs[i] = h;

   i++;
}

Sorry I meant go to as like as I go to do them in code.

Ok so that makes sense and mostly works. Its now skipping the first column in my CSV

Any ideas? before with the Serial.prints it was showing the first line so its weird it would ignore it now.

Also I really appreciate the help, thank you!

Its now skipping the first column in my CSV

When the Romans built columns, they knew that they would trip over them if they laid them down. So, they stood them up. Smart move.

34 is in the first column, on the second row.

There must be something wrong with your code. If you had posted it, in code tags, we could help you, instead of just laughing at you.

Haha copy, yes i meant row!

the issue was a copy pasta mistake I just caught . I accidentally had 2

“while(readVals(&x, &y, &z, &h) && i < 10)”

somehow.

Thanks again for the help.