SOLVED - Problem logging data into files

Hey guys!
I'm new here and in Arduino's world in general. I'm doing a project for university and a part of it includes creating new files (using a Datalogging Shield with RTC) every minute where supposedly data from an analog sensor would be written and saved into an SD Card 2GB.
The problem is that the files (.txt) are created but there is nothing wrote.
Could you please help me?
Thanks a lot!

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

RTC_Millis rtc;
int Solar = A1;    // Sensor
const int chipSelect = 4;  // CS
int ano=0, mes=0, dia=0, hora=0, minuto=0, segundo=0;
char filename[]="SolarM0000_00.txt";
File dataFile;

void setup() {
  Serial.begin(57600);
    Wire.begin();
    rtc.adjust(DateTime(__DATE__, __TIME__));

  Serial.print("Initializing SD card...");
  pinMode(10, 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.");
  
}



void loop() {
  
DateTime now = rtc.now();
if(now.minute() != minuto) {
  minuto = now.minute();
  sprintf(filename,"%04d_%02d.txt",now.year(), now.minute());
  dataFile = SD.open(filename); 
  dataFile = SD.open(filename, FILE_WRITE); 
}
else{
  dataFile = SD.open(filename); 
  dataFile = SD.open(filename, FILE_WRITE); 
}


// read the input on analog pin 0:
  int sensorValue = analogRead(Solar);
  
// print out the value you read:
  Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  Serial.println(sensorValue);


// make a string for assembling the data to log:
if (dataFile){
  String dataString = "";
  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(" ");
  dataString += String(sensorValue);
  dataFile.println(sensorValue);
  dataFile.close(); 
}

// Take 1 measurement every 5 seconds
delay(5000);
}
  dataFile = SD.open(filename); 
  dataFile = SD.open(filename, FILE_WRITE);

Why are you opening the file twice?

if (dataFile){
  String dataString = "";

Shitcan that String. Just write to the file!

Thanks a lot PaulS! It works perfectly with that modifications :smiley:

Just a guess, perhaps I'm wrong. I have something similar going on but like you were advised, I write directly to the file w/o strings, and it works just fine.

However, some functions don't like String variables, preferring old-fashioned character arrays. It's a moot point now that you write directly, but you might want to try that to see if that works.