Issue writing to file

Hi,

I’ve been building a simple project with my daughter that takes two temp readings and writes the to a file on a Micro SD card.

The code is fairly simple (if not messy) and is creating the file, but only ever seems to write the same row of data twice (from the setup()) and then never writes again, although doesn’t seem to produce an error.

The code is:

#include <Wire.h> //Required for Realtime Clock
#include "RTClib.h" //Required for Realtime Clock
#include <SPI.h> //Required for SD card reader
#include <SD.h>  //Required for SD card reader

#include <dht11.h> //Required for Temp/humidity reader

#define DHTPIN 4  //Pin for temp sensor
#define LEDPIN 7  //Pin for led (status)
#define DHTTYPE DHT11 //define which temp sensor we are using (DHT11)

dht11 DHT11; //Create instance of DHT11 class to read sensor values
File myFile;
String filename;

uint32_t  start_min; //Store time the current loop started so we can work out how long we've been running for
RTC_DS3231 RTC;  //Create instance of real Time clock class

void setup() {

  Serial.begin(9600);  //start serial monitor for output
  Wire.begin();

  //begin the rtc, let us know if it fails
  if (! RTC.begin()) {
    Serial.println("Couldn't find RTC");
    abort();
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }

  Serial.println("initialization done.");
  //set RTC time and date to the time the app was compiled
  RTC.adjust(DateTime(__DATE__, __TIME__));

  //Get the current RTC time and sore it as a unix time
  DateTime now = RTC.now();
  start_min = now.unixtime();

  //Setup Filename
  String filename = String(start_min);
  int start = filename.length() - 8;
  int end = filename.length();
  filename = filename.substring(2,10) + ".csv";
  Serial.println(filename);

  //Write Header row
  myFile = SD.open(filename, FILE_WRITE);
  if(myFile) {
    myFile.println("timestamp|humidity|temperature1|temperature2");
    myFile.flush();
    myFile.close();
    delay(1000);
  }
  else {
    Serial.println("Error opening file");
  }
}

void loop() {
  
  //Get current RTC date and time
  DateTime now = RTC.now(); 

  //Convert to unix time
  uint32_t _now = now.unixtime();

 // work out if  x mins have passed
  if((_now - start_min) > (4*60)) {
    //Turn LED on
    digitalWrite(LEDPIN,HIGH);
    Serial.print("Looped - ");
    Serial.print(now.hour());
    Serial.print(":");
    Serial.println(now.minute());

    int chk = DHT11.read(DHTPIN);

    myFile = SD.open(filename, FILE_WRITE);
    if(myFile) {

      String output = String(now.getTimeStr());
      output += "|";
      output += String((float)DHT11.humidity,2);
      output += "|";
      output += String((float)DHT11.temperature, 2);
      output += "|";
      output += String(RTC.getTemperature());
      Serial.println(output);

      myFile.println(output);
      myFile.close();

      Serial.println("Write complete");
    } 
    else {
      Serial.println("Error opening file");
    }
    
    //reset start time
    start_min = now.unixtime();
   
  }
    
    delay(5000);
   //Turn LED off
   digitalWrite(LEDPIN,0);
   //wait 90 secs
   delay(5000);
}

I’m using an Arduino Uno, a DHT11 for temp/humidity and a DS3231 RTC (also grabbing temp there to).

Once compiled and uploaded. this is the output:

Sketch uses 16798 bytes (52%) of program storage space. Maximum is 32256 bytes.

Global variables use 1290 bytes (62%) of dynamic memory, leaving 758 bytes for local variables. Maximum is 2048 bytes.

any help appreciate.

Regards
Gavin

This is a problem:

  //Setup Filename
  String filename = String(start_min);

You have a global variable called filename. Here you’ve declared a local variable with the same name and set it up with the name of the file you’re going to write on. At the end of setup, that local will cease to exist and the next time you try to open the file, you’re doing so with a blank name.

Get rid of the word String in that snippet.