read line by line float datas in SD Card

Hello everybody.

I have a .txt with many lines with something like this in a SD card:

36.5, 34.6, -2.3, 117.5
22.0, 12.4, 1.3, 290.1
..
.
.all lines has the same 4 float variables

I need to read all 4 variables of each line, after that to jump to the next line and to do the same using the same 4 variables to read.

Somebody has one sample with the same characteristics ? I really appreciate this.

Have a look at parseFloat() which reads the next float of a stream. If you know there are always 4, just do 4 successive myfile.parseFloat() into 4 variables.

(That link shows parseFloat() as being in Serial, but it works for the SD library too.)

The code below compiles but is not tested: can't find my SD card module right now.

Set your file name in the line marked ///<<<<<<<<<<<<<<<<<<<<<<<

It reads 4x floats into variables and prints them to the monitor. Then if there's more data it delay()s for a second then does the next line (edit: or more correctly the next 4 floats, regardless of lines), or else it ends.

(This test code is all in setup(). In real life you would probably want to move it into loop() and make it delay()-less.)

You didn't say what you want to do with the data, so you'll have to add that part yourself.

// https://forum.arduino.cc/index.php?topic=631096

// parse 4x floats out of an SD card file
// meltDown 12 august 2019

/*
  BASED ON SD card file dump

  This example shows how to read a file from the SD card using the
  SD library and send it over the serial port.

  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created  22 December 2010
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

int myVar1;
int myVar2;
int myVar3;
int myVar4;

void setup()
{
  Serial.begin(9600);
  Serial.println(".... parse floats out of sd file ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);


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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("theDataFile.txt"); //<<<<<<<<<<<<<<<<<<<<<< set file name here

  // if the file is available, read from it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      //parse the next 4 values out of the file:
      myVar1 = dataFile.parseFloat();
      myVar2 = dataFile.parseFloat();
      myVar3 = dataFile.parseFloat();
      myVar4 = dataFile.parseFloat();
      //print them to the monitor:
      Serial.println(myVar1);
      Serial.println(myVar2);
      Serial.println(myVar3);
      Serial.println(myVar4);

      delay(1000);
      //go back to the top for the next line...
      //or ....
    }//while data available
    Serial.println(".... no more data");
    dataFile.close();
  }// if datafile
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening the data file");
  }
}//setup

void loop()
{
}

Errata: these should be float of course...

int myVar1;
int myVar2;
int myVar3;
int myVar4;

Fusion de un reactor.

I ´ll study your code. Really I appreciate your effort to help me.

thank you !.

volveralfuturo:
Fusion de un reactor.

?

volveralfuturo:
Really I appreciate your effort to help me.

thank you !.

You're welcome: If I can find my SD card module in my "filing system" I will test the code.

It works btw, with the fix from #3 of course.

Here's the data in notepad:

1.1,2.2,3.3,-4.4
10.1,-20.1,30.1,40.1
9.9,8.8,7.7,-6.6

Here's the output:

.... parse floats out of sd file ....
Compiler: 4.9.2, Arduino IDE: 10805
Created: 10:44:35, Aug 13 2019
C:\Users\xxxx\Documents\Arduino\xxxxxxxx
Initializing SD card...card initialized.
1.10
2.20
3.30
-4.40
10.10
-20.10
30.10
40.10
9.90
8.80
7.70
-6.60
.... no more data