Create .csv file with data in a column (table)

I’m writing a code that receives the value of a potentiometer at all times (float val_pot2)
I created a function to record these values in a table in .csv format
I have the following problem: I am not able to save a new value in each line.
What happens with my code is that the file is always saved with the last potentiometer value when I started recording.
How can I solve this problem so that the file is formatted:
Voltage
1.12
1.5
2.0
3.0
4.0
3.5
3.0

Table table;
void RECORD() {
   
  table = new Table();
  table.addColumn("Voltage");
  TableRow newRow = table.addRow();
  
   for(int k=0; k<10; k++) {
  table.addRow();
  newRow.setFloat("Voltage",val_pot2);
  saveTable(tabela, "data/new.csv");
  delay(10); 
   }
   
  }

Hi fsdonato,

Welcome to the forum.

Your function RECORD() creates each time a new table, so what ever was recorded on previous call is thrown away.

You don’t need a table object to store csv. CSV is a plain text file where you use a separator (usually comma but not always) to separate values on the row. In your case just opening a text file and adding new values to it might be enough.

You can do that with PrintWriter. Processing documentation has a nice example how to use it createWriter() / Reference / Processing.org. In your case closing the file is different, but commands and logic is same.

2 Likes