Layout of columns

Hi all,

just a basic question today, please. I’ve tried a few different variations, but the items keep going down to the next line.

I have in the setup;

// Write coloum headers to file
File dataFile = SD.open("DHTDATA.csv", FILE_WRITE);
if (dataFile) {

    dataFile.print("Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7, Humid1, Humid2, Humid3, Humid4, Humid5, Humid6, Humid7, Date, Time");
    dataFile.println();
}

dataFile.close();

Which helps me layout the titles. But ideally, I want the date and the time first. I just can’t figure out how to get them to layout.

The code for the data entry (which isn’t determined by the title blocks) is;

//// COLLECTIVE SENSOR DATA COLLECTION FOR EXCEL OUTPUT

Serial.println("PRINT SD CARD VALUES & SAVE TO CSV");

hmids[0] = dht0.readHumidity();
temps[0]= dht0.readTemperature();

hmids[1] = dht1.readHumidity();
temps[1]= dht1.readTemperature();

hmids[2] = dht2.readHumidity();
temps[2]= dht2.readTemperature();

hmids[3] = dht3.readHumidity();
temps[3]= dht3.readTemperature();

//hmids[4] = dht4.readHumidity();
//temps[4]= dht4.readTemperature();

//hmids[5] = dht5.readHumidity();
//temps[5]= dht5.readTemperature();

//hmids[6] = dht6.readHumidity();
//temps[6]= dht6.readTemperature();

//Serial print values

Serial.print("Temps ");
for (int i=0;i<7;i++)
{
Serial.print(temps[i]);
Serial.print(", ");
}
Serial.println();

Serial.print("humids ");
for (int i=0;i<7;i++)
{
Serial.print(hmids[i]);
Serial.print(", ");
}
Serial.println();

//Write data to SD card
File dataFile = SD.open("DHTDATA.csv", FILE_WRITE);
if (dataFile) {

for(int i=0;i<7;i++)
{
dataFile.print(temps[i]);
dataFile.print(", ");
}


for(int i=0;i<7;i++)
{
dataFile.print(hmids[i]);
dataFile.print(", ");
}


//Enter date and time
{
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.println();
}

dataFile.close();
}

Here is what the excel spreadsheet looks like - capture image.

Ideally, it would look like capture 2 image.

For that to happen I would just change my setup to;

// Write coloum headers to file
File dataFile = SD.open("DHTDATA.csv", FILE_WRITE);
if (dataFile) {

    dataFile.print("Date, Time, Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7, Humid1, Humid2, Humid3, Humid4, Humid5, Humid6, Humid7");
    dataFile.println();
}

dataFile.close();

Can someone help on the main body text please. I’ve tried moving it around in order, but the temperature values just write to the next line leaving the date and time on their own row.

Use datafile.print() when you print the date and time and when you print the temps. Only when the line of data is complete use datafile.println()

That way the date, time and temps will all be on the same line

Thanks for the help! I’ve now cleaned up the data which makes it easier for charting.

Just a note for anyone else learning about the datafile.print functions, it also works on the serial output. As I used that to understand and redefine the text output.

Thanks for your help mate!

For future readers learning basic code as I was, here was the amended code in my loop file;

//write data to SD card
File dataFile = SD.open("DHTDATA.csv", FILE_WRITE);

{
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.print(',');

}

if (dataFile) {

for(int i=0;i<7;i++)
{
dataFile.print(temps[i]);
dataFile.print(", ");
}


for(int i=0;i<7;i++)
{
dataFile.print(hmids[i]);
dataFile.print(", ");
}


dataFile.close();
}

use code tags.. See your code turning into italic ? this is because there is a [i] somewhere we can't really see... (although we can guess it's in your for loop)

mrevolve:
For future readers learning basic code as I was, here was the amended code in my loop file;

//write data to SD card

File dataFile = SD.open(“DHTDATA.csv”, FILE_WRITE);

{
    dataFile.print(now.year(), DEC);
    dataFile.print(’/’);
    dataFile.print(now.month(), DEC);
    dataFile.print(’/’);
    dataFile.print(now.day(), DEC);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(’:’);
    dataFile.print(now.minute(), DEC);
    dataFile.print(’:’);
    dataFile.print(now.second(), DEC);
    dataFile.print(’,’);

}

if (dataFile) {

for(int i=0;i<7;i++)
{
dataFile.print(temps[i]);
dataFile.print(", ");
}

for(int i=0;i<7;i++)
{
dataFile.print(hmids[i]);
dataFile.print(", ");
}

dataFile.close();
}

Not quite right. You want to only write the date and time to the file if the file was successfully opened:

  //write data to SD card
  File dataFile = SD.open("DHTDATA.csv", FILE_WRITE);
  if (dataFile)
  {
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.print(',');
    for (int i = 0; i < 7; i++)
    {
      dataFile.print(temps[i]);
      dataFile.print(", ");
    }
    for (int i = 0; i < 7; i++)
    {
      dataFile.print(hmids[i]);
      dataFile.print(", ");
    }
    dataFile.close();
  }

Thanks guys. All updated. Thanks for the amendment John.

I really do wish I'd come here sooner to get everyone's advice.