Creating columns in a CSV file?

Hi everyone!

I am creating a CSV file using the Arduino and I would like to specify the order of the columns in which the data is placed. I have been able to do this previously by rearranging the order that the data is written in the code. However, now when I add in a counter (trigger) hoping to write to the last column, it writes to the first. I would like to add a counter to the last column when an event occurred (dataFile.print(trigger)) and leave blank otherwise.

How do I specifically tell Arduino to put this data in the last column? Below I have listed the code that is being used. I don’t have an example of how the CSV file looks at the moment but I will post that later when I am around the set-up.

Also, would there be a better way of accomplishing this than how I have it written below in order to achieve the desired results above?

Thanks

 // open the file:
    File dataFile = SD.open("datalog.csv", FILE_WRITE);

    // if the file is available, write to it:
    if (dataFile) {
      DateTime now = rtc.now();

      if (LDRValue <= light_sensitivity) {

        trigger++;

        dataFile.print(now.year(), DEC);
        dataFile.print('/');
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.day(), DEC);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
        dataFile.print(now.second(), DEC);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(humidity);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.println(temperature);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(trigger);
        dataFile.print(' ');
        dataFile.print("\t");

      }

      else {
        
        dataFile.print(now.year(), DEC);
        dataFile.print('/');
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.day(), DEC);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
        dataFile.print(now.second(), DEC);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(humidity);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.println(temperature);
        dataFile.print(' ');
        dataFile.print("\t");
      }

      dataFile.close();
}

So where are the commas?

I used tabs (\t) as the delimiter instead mainly for presentational purposes so it could easily be read without opening Excel and it was also used by the sample code I followed.

How do I specifically tell Arduino to put this data in the last column?

You write it last.

Pete

Thanks for the reply Pete.

I tried that above but it wrote it first instead. See code above.

LiquidSky: Thanks for the reply Pete.

I tried that above but it wrote it first instead. See code above.

Full code please

If you only want to write to the last column then in effect you are leaving the preceding columns empty. You can do that by writing the appropriate number of delimiters without any data between them. For example, if you were using a comma as a delimiter you would do it like this:

,,,,,foo

Hi Peter,

Well I do want to write to the last column but I don't want to leave the other columns blank. I have a feeling that the trigger++ is messing up the order somehow?

Thanks

Of course it is. With one record (with the trigger), you have 5 tabs, and in the other (without the trigger), you have only 4 tabs. How do you tell where one record ends and another starts? I use a cr/lf to separate records. Change the last dataFile.print() to a println().

        // change this
        dataFile.print("\t");
        // to this
        dataFile.println("\t");

Then I read until I get a \n character.

Awesome!

Thanks SurferTim! I will give that a try tomorrow and see if it works.

Much appreciated!

        dataFile.println(temperature);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.print(trigger);
        dataFile.print(' ');
        dataFile.print("\t");

The problem is that you are writing the temperature with a linefeed (println) and then following it with the trigger which will be on the next line. Do this:

        dataFile.print(temperature);
        dataFile.print(' ');
        dataFile.print("\t");
        dataFile.println(trigger);

Pete

BTW. If you really want to write one space and then a tab:

        dataFile.print(' ');
        dataFile.print("\t");

you can do it like this:

        dataFile.print(" \t");

Pete

LiquidSky: Well I do want to write to the last column but I don't want to leave the other columns blank.

You have to write the columns in the order that they appear in the file. If you want a value in a column, write the value before the corresponding separator; if you don't, then don't. Regardless of what values you want in each column, you should always write all the separators - otherwise, applications reading the data will typically treat this row as a break in the data.