Trying to create a header for a csv file help please

I am trying to create a header for my CSV file before I start logging data. My data logs later when I want it to but when I attempt to open the file here and set the header it does not work. Can anyone see my mistake?

void setup() {
      pinMode(programpin, OUTPUT);
      Wire.begin();
      lcd.begin(16, 2);                   // initiate LCD                               
                                          // initialize serial communication at 9600 bits per second
      Serial.begin(9600);                 // baud rate for computer serial communication
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
      
  if (dataFile) {
  File dataFile = SD.open(filename);  
  dataFile.println("Event,Time,Date");
  dataFile.close();
 }
  else
  {
    Serial.println("Couldn't open log file");
    return;
  }
  RTC.begin();
  if (! RTC.isrunning()) {
    lcd.print("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  clearPrintTitle();
}

(deleted)

  if (dataFile)
  {
    File dataFile = SD.open(filename);
    dataFile.println("Event,Time,Date");
    dataFile.close();
  }

Shouldn't you be opening the file before you test whether it exists ?

The program compiles and works. Later a loop is called that opens the file logs data and closes. that works it opens logs closes but the header in setup doesn’t.

The code is exactly the same for the two I just figured in the set up it’ll only call once so it won’t make numerous lines of the header where as the loop based writing to the SD will do it over and over.

(deleted)

And put a timeout on the while-not-serial. Unless nothing else in the whole program can work without the USB serial.

while(!Serial && millis()<5000){
  //Wait 5 sec for serial monitor
}

The code is exactly the same for the two

I doubt that very much. Please post your whole program.

As has been pointed out more than once above, in setup() you appear to be checking for the existence of the dataFile object before you create it.

Yes you are right it was the wrong way around my mistake O_O

All OK now ?

yes thank you.

Trying to find a way to display info onto both lines of the LCD screen without any luck.

for the function to display I did this:

void SDread()
{
  lcd.setCursor(0,0);
  File dataFile = SD.open(filename2);
    if (dataFile) 
   {
      while (dataFile.available()) 
    {
      lcd.write(dataFile.read());
    }
    dataFile.close();
   }

  lcd.setCursor(0,1);
  File dataFile1 = SD.open(filename3);
    if (dataFile1) 
   {
      while (dataFile1.available()) 
    {
      lcd.write(dataFile1.read());
    }
    dataFile1.close();
    delay(500);
   }
    return;
}

it reads what's in flename2 but nothing is displayed on the second row for filename3.

if anyone has any suggestions would be greatly appreciated.

What do you see, if anything, if you Serial.println() the data ?
How about printing a character, say ">", before the data on the LCD. Do you see the ">" ?

How many rows of data and of what length are in the files ? There is not much room on the average LCD but we cannot tell how many rows and columns it has from the code snippet that you have posted.

Standard 16x2 LCD

I thought maybe what I could create two files and then call both to be read but set the cursor in between calling each one. That way it would display both of them. If I succeed I will pass this information on.

You need to either set the cursor position before printing anything or let the data be printed one item after another.

16 by 2 does not give you much room to print anything hence my questions in my previous reply. Did you try what I suggested ?