How write sd card cvs in the next column

1.ino (188 Bytes)

Hi , I need help.

I would like write in the next column but I don't know wher i can to get this information.

The board is ESP32

void loop() {
writeFile(SD, "/Dados_machine.csv", "first column");
appendFile(SD, "/Dados_machine.csv", "second column");
readFile(SD, "/Dados_machine.csv");
delay(10000);
}

Thank youSD_vida.ino (5.6 KB)

Post moved to a more suitable location. Use Portuguese if you post in the Portuguese language section


Do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).


To your question, a csv file is laid out so that all the columns are organised by line in the file and a line has to be consecutive characters with separators for each column (a comma in a true csv file). So if you want to fill in the second column and already have a file with the first column you need to rewrite everything.

What is the result that you see from the readFile() function on the Serial Monitor?

I get write and read the data, but now I need turn this txt in a variable, It's possible?
This code work wel to ESP32.

type or paste code here
```#include "SD.h"
#include "SPI.h"

File file;
long dados ;
void setup() {

  Serial.begin(115200);
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  writeFile();
}

void loop() {
  //writeFile(SD, "/Dados_machine.csv", "first column");
  readFile();
  delay(3000);
}
void writeFile() {

  file = SD.open("/Dados_machine.txt", FILE_WRITE);
  file.println("55");

  file.close();
}

void readFile() {
  file = SD.open("/Dados_machine.txt", FILE_READ);
  if (file) {
    while (file.available()) {
      Serial.write(file.read());
      dados = (file.read());
      Serial.println("Conteudo");
      Serial.println(dados);
      Serial.println("fim");
    }
    file.close();
  } else {
    Serial.println("falhou");
  }
}

There are a few things wrong with your code in post #4.

  1. Filenames on an SD card need to adhere to the 8.3 naming convention; Dados_machine is 13 characters long, you'll need to cut it down to 8 characters.
  2. while (file.available()) checks if there is one character available. There is no guarantee that there is more than one character available; you however read 2 characters, the first one with Serial.write(file.read()) and the second one with dados = (file.read()).
  3. dados is a long integer, file.read() returns one character of a text. So dados will only contain '5', not "55".

To store the data in a variable, you can use file.readBytesUntil().

If you change writeFile() to only write '\n' instead of "\r\n", readBytesUntil() can read till \n.


  file = SD.open("/Dados_machine.txt", FILE_WRITE);
  file.print("55");
  file.print("\n");
  file.close();
}

And readFile() could be

void readFile() {
  file = SD.open("/Dados_machine.txt", FILE_READ);
  if (file) {
    if (file.available()) {
      // clear the buffer
      memset(buffer, '\0', sizeof(buffer));
      // read till '\n' (line feed)
      file.readBytesUntil('\n', buffer, sizeof(buffer));
      // print
      Serial.println(buffer);
    }
    file.close();
  } else {
    Serial.println("falhou");
  }
}

buffer needs to be big enough to hold the longest line that you want to read plus one additional character for a terminating '\0'. So if your line is 20 characters, you need to declare buffer[21].

Note: not compiled nor tested.