SD data formating

Good Morning, I would appreciate if you can help me in formatting the .csv data from SD card. My intent is to get the values in 4 columns (column 1 will have its value, column 2 and so on). But they are in row (datalog file below). Also attached the code. Would appreciate any help in formatting the data.

Column1Value1 0.1 uA
Column1Value1 1 uA
Column1Value1 1.4 uA
Column1Value1 0.5 uA
Column1Value1 0 uA
Column2Value2 0 uA
Column2Value2 0 uA
Column2Value2 0.4 uA
Column2Value2 1.3 uA
Column2Value2 1.4 uA
Column2Value2 0.2 uA
Column2Value2 0 uA
Column3Value3 0 uA
Column3Value3 0.1 uA
Column3Value3 0.6 uA
Column3Value3 1.1 uA
Column3Value3 0.7 uA
Column3Value3 0 uA
Column3Value3 0 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
Column4Value4 0.7 uA
{
  File dataFile = SD.open("datalog.csv", FILE_WRITE);

  
  // if the file is available, write to it:
if (dataFile) {
  if(!Relay1On)
  {
  currentBurner = 1; //Column 1
  dataFile.print("Column1Value");
  dataFile.print(currentBurner);
  dataFile.print(",");
  dataFile.println(dataString);
  
  } 
   
  else if(!Relay2On)
  {
  currentBurner = 2; //Column 2
  dataFile.print("Column2Value");
  dataFile.print(currentBurner);
  dataFile.print(",");
  dataFile.println(dataString);
  }
  else if(!Relay3On)
  {
  currentBurner = 3; //Column 3
  dataFile.print("Column3Value");
  dataFile.print(currentBurner);
  dataFile.print(",");
  dataFile.println(dataString);
  
   }
  
  else if(!Relay4On)
  {
  currentBurner = 4; // Column4
  dataFile.print("Column4Value");
  dataFile.print(currentBurner);
  dataFile.println(",");
  
  
   }

Thanks

Please post an example of how you would like the data to be presented

Below is my intent:

Column 1 Column 2 Column 3 Column 4
0.1 0 0 0.7
1 0 0.1 0.7
1.4 0.4 0.6 0.7
0.5 1.3 1.1 0.7

I suggest you familiarize yourself with fprintf(). That, to me, would be the best way to create properly formatted file data.

In your example are the values for all 4 columns available at the same time in the program ?

Hi Bob,

I am not familiar with fprint().

Regarding the values appearing at the same time, the answer is No. As I press switch 1, the values appear for switch 1 and I press switch 2 (switch 1 is off then), values appear for switch 2 and so on.

fprintf(), not fprint.

Sorry. You are right Ray.

As I press switch 1, the values appear for switch 1 and I press switch 2 (switch 1 is off then), values appear for switch 2 and so on.

How many items of data appear when you press each button ? Is it a fixed number for each button and are they all the same ?

What should the output look like if one of the buttons is not pressed ?

Hi Bob, as long as the switch is closed, the data will be recorded for that particular switch (may be many rows of data under particular switch/column). I am using Adafruit M0 for datalogging. Data length among switches may be similar may not be same.

Data length among switches may be similar may not be same.

So what should be output if say there are 2 readings for switch 1, 3 readings for switch 2, 5 readings for switch 3 and 1 reading for switch 4 ?

I can't help feeling that it would be better to accept the data as it is output currently and deal with transforming its presentation in Excel

Hi Bob, it will be like below. I was thinking anything can be done in the code without havin to format the data in Excel.

Column1 Column2 Column3 Column4
0.1 1.4 0.4 0.7
1 0.5 1.3
0 1.4
0.2
0

Now the data is recorded in rows with one column (please see my first post above)

What is the maximum number of readings for a single switch and is it OK to stop reading the data for a particular switch after a fixed number of readings, say 10 ?

Will the switches always be pressed in the same order and will they always all be pressed ?
Is there a chance that the switches may be operated in the order of say 1, 2, 1, 3, 4 ? If so then what should happen to the second set of data for switch 1 and how would the program know that data capture was complete ?

I know that I am asking a lot of questions but without the answers it is difficult to know how to suggest that you proceed.

It would help our understanding if you could elaborate on what the data is for.

Hi Bob, thank you very much for your help! Don't worry about your questions. Wish I could have clarified better.

Regarding data length for each switch/push button, it can be around 20 to 25 values.

At the beginning, the test technician will turn on the power to the measuring box (SD card recording will start as the power is applied to the Adafruit Feather M0). Normally and should always be, test technician should turn on the switch 1, and will record the instance LCD display values. After recording the LCD values, the test technician will turn off the switch 1 and will turn on switch 2. Will do the same thing for switch 2 and so on till switch 4. After that he will turn off the power to the measuring box (SD card will become off, no more measurement). The test technician will then take the SD card out of the Feather M0 and will download the data to the computer. This is a 30 days test and every day the test technician will do that.

If you put the data for each switch in its own row in the file then in Excel you can use the transpose() function rotate it through 90 degrees so that each row becomes a column and vice versa

This is much simpler that doing something similar in the sketch as the functionality is there already and you could write an Excel macro to automate it for the technician

Thanks Bob! Will do that. Was thinking if something can be done in Arduino.

That way, I will also learn more about new codes etc.

I was looking at following link (after google search); How to Write Data to Files on an SD Card With the Arduino - Circuit Basics.

He did in .txt file. where time, temperature, pressure, Altitude is reported in column format . My idea came from that link. But could not execute in my program. Here is the code he reported in that link

#include <SD.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; 
File myFile;

// change this to match your SD shield or module:
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
//mega pin = 53;
const int chipSelect = 10;
float QNH = 1022.67; //Change the "1022.67" to your current sea level barrometric pressure (https://www.wunderground.com)
const int BMP_address = 0x76;

float pressure;   
float temperature;  
float altimeter; 
char charRead;
char runMode;
byte i=0; //counter
char dataStr[100] = "";
 char buffer[7];
////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600);
  Serial.println("BMP280/SD Card Demo");
  bmp.begin(BMP_address); 
  if (SD.begin(chipSelect))
  {
    Serial.println("SD card is present & ready");
  } 
  else
  {
    Serial.println("SD card missing or failure");
    while(1); //halt program
  }
  //clear out old data file
  if (SD.exists("csv.txt")) 
  {
    Serial.println("Removing simple.txt");
    SD.remove("csv.txt");
    Serial.println("Done");
  } 

  //write csv headers to file:
   myFile = SD.open("csv.txt", FILE_WRITE);  
   if (myFile) // it opened OK
    {
    Serial.println("Writing headers to csv.txt");
    myFile.println("Time,Pressure,Temperature,Altitude");
    myFile.close(); 
    Serial.println("Headers written");
    }
  else 
    Serial.println("Error opening csv.txt");  
  Serial.println("Enter w for write, r for read or s for split csv");  
}// end setup()

////////////////////////////////////////////////////////////
void loop(void) 
{
 dataStr[0] = 0;
 pressure = bmp.readPressure()/100;  //and conv Pa to hPa
 temperature = bmp.readTemperature();
 altimeter = bmp.readAltitude (QNH); //QNH is local sea lev pressure
//----------------------- using c-type ---------------------------
 //convert floats to string and assemble c-type char string for writing:
 ltoa( millis(),buffer,10); //conver long to charStr
 strcat(dataStr, buffer);//add it onto the end
 strcat( dataStr, ", "); //append the delimeter
 
 //dtostrf(floatVal, minimum width, precision, character array);
 dtostrf(pressure, 5, 1, buffer);  //5 is mininum width, 1 is precision; float value is copied onto buff
 strcat( dataStr, buffer); //append the coverted float
 strcat( dataStr, ", "); //append the delimeter

 dtostrf(temperature, 5, 1, buffer);  //5 is mininum width, 1 is precision; float value is copied onto buff
 strcat( dataStr, buffer); //append the coverted float
 strcat( dataStr, ", "); //append the delimeter

 dtostrf(altimeter, 5, 1, buffer);  //5 is mininum width, 1 is precision; float value is copied onto buff
 strcat( dataStr, buffer); //append the coverted float
 strcat( dataStr, 0); //terminate correctly 
 //Serial.println(dataStr);
 //---------------------------------------------------  
 //create a loop to read from the keyboard a command character
 //this will be 'r' for read, 'w' for write and 'd' for delete.

  if (Serial.available()) //get command from keyboard:
     {
      charRead = tolower(Serial.read());  //force ucase
      Serial.write(charRead); //write it back to Serial window
      Serial.println();
     }
 
  if(charRead == 'w')  //we are logging
      runMode = 'W';
  if(charRead == 'r')  //we are reading
      runMode = 'R';
  if(charRead == 'd')  //we are deleting
      runMode = 'D';
 //----------------------------------------------------
  if(runMode == 'W') //write to file
  {     
   //----- display on local Serial monitor: ------------
   Serial.print(pressure); Serial.print("hPa  ");
   Serial.print(temperature); 
   Serial.write(0xC2);  //send degree symbol
   Serial.write(0xB0);  //send degree symbol
   Serial.print("C   ");  
   Serial.print(altimeter); Serial.println("m");
   //---------------------------------------------
   // open the file. note that only one file can be open at a time,
    myFile = SD.open("csv.txt", FILE_WRITE);     
    // if the file opened okay, write to it:
    if (myFile) 
    {
      Serial.println("Writing to csv.txt");
      myFile.println(dataStr); 
      myFile.println(dataStr); 
      myFile.close();
    } 
    else 
    {
      Serial.println("error opening csv.txt");
    }
    delay(1000);  
  }
//--------------------------------------------------
    if(runMode == 'R')  //we are reading
   {
    if (!SD.exists("csv.txt")) 
        Serial.println("csv.txt doesn't exist."); 
   Serial.println("Reading from csv.txt");
   myFile = SD.open("csv.txt");
    
   while (myFile.available()) 
  {   
   char inputChar = myFile.read(); // Gets one byte from serial buffer
    if (inputChar == '\n') //end of line (or 10)
    {
      dataStr[i] = 0;  //terminate the string correctly
      Serial.println(dataStr);
      i=0; //reset the counter
    }
    else
    {
      dataStr[i] = inputChar; // Store it
      i++; // Increment where to put next char
      if(i> sizeof(dataStr))  //error checking for overflow
        {
        Serial.println("Incoming string longer than array allows");
        Serial.println(sizeof(dataStr));
        while(1);
        }
    }
  }
}
  //------------------------------------------- 
  if(runMode=='D')
  {
   //delete a file:
   if (SD.exists("csv.txt")) 
      {
      Serial.println("Removing csv.txt");
      SD.remove("csv.txt");
      Serial.println("Done");
     } 
  }
  //-----------------------------------------
   
} //end main