i want to save a vector to a .csv file on a SD Card.
I watched this tutorial on how to do it and it describes it perfectly even though its kinda dated.
My Problem is that everything is saved into the first cell of the .csv file so it gets constantly overwritten. It should save only the first vector into the first cell, then the second vector into the second cell and so on. I just get all three vectors with the commas into the first vector like this:
8, 9, 10
Also the header which should appear at the top is also gone.
I would really appreciate some help. Thanks!
Here is my setup and loop:
#include <iostream>
#include <string>
#include <vector>
#include "SD.h"
#include "SPI.h"
#include "EKG_data_transmision.h"
#define CS_Pin 5 // Chip Select Pin
EKG_data_transmision EKG_data_transmision;
std::vector<unsigned short> vector_1 = {1,2,3,4,5,6,7,8,9,10};
std::vector<unsigned short> vector_2 = {2,3,4,5,6,7,8,9,10,11};
std::vector<unsigned short> vector_3 = {3,4,5,6,7,8,9,10,11,12};
unsigned int for_x = 0;
void setup() {
// open serial
Serial.begin(9600);
Serial.print("Init SD Card...");
pinMode(CS_Pin, OUTPUT);
if(!SD.begin(CS_Pin))
{
Serial.println("Card failed or not present");
return;
}
EKG_data_transmision.vectorData = SD.open("/data.csv", FILE_WRITE); //erstelle data.csv datei
if(EKG_data_transmision.vectorData)
{
EKG_data_transmision.vectorData.println(", , , ,");
String header = "ID, Value, Time, Whatever";
EKG_data_transmision.vectorData.println(header);
EKG_data_transmision.vectorData.close();
Serial.println(header);
Serial.println("card init sucess");
}
else
{
Serial.println("Couldnt open vector data file");
}
}
void loop() {
// put your main code here, to run repeatedly:
while(for_x <= 8){
for_x++;
EKG_data_transmision.write_vector_to_SD(vector_1, vector_2, vector_3);
delay(1000);
}
delay(10000);
}
Here is my function that should save everything to the SD Card:
bool EKG_data_transmision::write_vector_to_SD(std::vector<unsigned short> vector_ADC_1, std::vector<unsigned short> vector_ADC_2, std::vector<unsigned short> vector_ADC_3) // Übertrage
{
dataString = String(vector_loc) + "," + String(vector_ADC_1[vector_loc]) + "," + String(vector_ADC_2[vector_loc]) + "," + String(vector_ADC_3[vector_loc]); // + "\r\n"
vectorData = SD.open("/data.csv", FILE_WRITE);
if(vectorData){
//sd karte immer noch da und schau ob data.csv exisitiert
vectorData.println(dataString);
Serial.println("This is vector:");
Serial.println(vector_loc);
Serial.println("Im actually writing this:");
Serial.println(dataString);
vectorData.close(); // close the file
}
else{
Serial.println("Error writing to file !");
}
vector_loc++;
return 0;
}
1 Answer 1
In Arduino SD library FILE_WRITE constant instructs the function to open the file for append, but in the esp32 SD library it opens the file for rewrite. Use FILE_APPEND to open the file for append.
File file = SD.open(path, FILE_APPEND);
;
for csv, because we have,
as decimal separator;
it works perfectly.