Multiple files writing.

Helo i am trying to write temperature, humidity and pressure into 3 diferent files, but data are writen into tempText only. pressText and humidText doesn’t work. Files are created but arduino do not want to open and write data into them.
Thanks for any help.

#include <SD.h>
#include <SPI.h>
#include <Wire.h> 
#include <TimeLib.h>
#include <Adafruit_BMP280.h>
#include <SimpleDHT.h>
#include <DS1307RTC.h>

File pressText, tempText, humidText;
int pinCS = 4; 
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp;

void setup() {
  Serial.begin (9600);  
  pinMode (pinCS, OUTPUT);

   while (!Serial) ; // wait for serial RTC
  delay(200);
  
  
  if (SD.begin()){
    Serial.println("SD card is ready");
    tempText = SD.open("tempText.txt", FILE_WRITE);
    tempText.close();
  }
  else{
    Serial.println("SD card initialization failed");
    return;  
  }
//-------------------------------------------------

  if (SD.begin()){
    Serial.println("SD card is ready");
    pressText = SD.open("press.txt", FILE_WRITE);
    pressText.close();
  }
  else{
    Serial.println("SD card initialization failed");
    return;  
  }
//-------------------------------------------------

    if (SD.begin()){
    Serial.println("SD card is ready");
    humidText = SD.open("humid.txt", FILE_WRITE);
    humidText.close();
  }
  else{
    Serial.println("SD card initialization failed");
    return;  
  }
  
  
  bmp.begin(0x76);
  
  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500);
     
    
  }

void loop() {
  tmElements_t tm;  

  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
   Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" °C");
    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");


  tempText = SD.open("tempText.txt", FILE_WRITE);    
  if (tempText) {
    Serial.println("Writing to file...");
    //BMP temp.
    tempText.print(bmp.readTemperature());
    tempText.print(",");
    //DHT temp.
    tempText.print((int)temperature);
    tempText.print(",");
      if (RTC.read(tm)) {
         //RTC time
         //RTC hour
         tempText.print(tm.Hour);
         //RTC minute
         tempText.print(tm.Minute);
         //RTC second
         tempText.println(tm.Second);
        }else {
           if(RTC.chipPresent()) {
              Serial.println("Time wasn't set");
              tempText.print("000000");
              }else{
                   Serial.println("Couldn't get data from RTC");
                   tempText.print("000000");
                   } 
               }
                  
    tempText.close();
    Serial.println("Temp written successfully");
  }
  else {
    Serial.println ("Error opening tempText.txt");
  }
  delay(100);
//----------------------------------------------------------
  pressText = SD.open("pressText.txt", FILE_WRITE);    
  if (pressText) {
    Serial.println("Writing to file...");
    pressText.print(bmp.readPressure());
    pressText.print(",");
      if (RTC.read(tm)) {
         //RTC time
         //RTC hour
         pressText.print(tm.Hour);
         //RTC minute
         pressText.print(tm.Minute);
         //RTC second
         pressText.println(tm.Second);
        }else {
           if(RTC.chipPresent()) {
              Serial.println("Time wasn't set");
              pressText.print("000000");
              }else{
                   Serial.println("Couldn't get data from RTC");
                   pressText.print("000000");
                   }
              }
    
    pressText.close();
    Serial.println("Press written successfully");
    }
    else {
    Serial.println ("Error opening pressText.txt");
  }
 // ----------------------------------------------------
    humidText = SD.open("humidText.txt", FILE_WRITE);    
  if (humidText) {
    Serial.println("Writing to file...");
    humidText.print((int)humidity);
    humidText.print(",");
      if (RTC.read(tm)) {
         //RTC time
         //RTC hour
         humidText.print(tm.Hour);
         //RTC minute
         humidText.print(tm.Minute);
         //RTC second
         humidText.println(tm.Second);
        }else {
           if(RTC.chipPresent()) {
              Serial.println("Time wasn't set");
              humidText.print("000000");
              }else{
                   Serial.println("Couldn't get data from RTC");
                   humidText.print("000000");
                   }
              }
    
    humidText.close();
    Serial.println("Press written successfully");
  }
  else {
    Serial.println ("Error opening humidText.txt");
  }
  delay(5000);
}

Why the return; in the end of this piece of code?

if (SD.begin()){
    Serial.println("SD card is ready");
    tempText = SD.open("tempText.txt", FILE_WRITE);
    tempText.close();
  }
  else{
    Serial.println("SD card initialization failed");
    return;  
  }

Oh, that is probbably cause of copying code from older one. But it doesn’t change anything if i delete it. :o

Did You delete the other two “return;” in the same positions in the open/create file parts?
After the SD.begin make a more informative printout telling what file it is creating and se what’s happening. I can’t se any reason why it will not create the other 2 files.

Yes i did. Actually it creates this files, but no data are writen into them. Program has problem opening this file. :smiley:

So now the code creates those 3 files but no file has any content. Is that correct?
Can You make more clear Serial.print messages?
    Serial.println("Writing to file..."); into
    Serial.println("Writing to tmpText"); etc. and look at Serial monitor.

You have status messages like

    Serial.println("Press written successfully");
    }
    else {
    Serial.println ("Error opening pressText.txt");

Does any of them show up? Which one? For all, none or which of the 3 files?

I don't kwow SD libraries so I have to guess. Is Open.... FileWrite the proper opening? Not something like Open FileAppend?

Attach Your latest code, to be sure, and attach a copy of the Serial Monitor messages.

Hey i finally figured out. Problem was that i cerated files: press.txt and humid.txt but i was trying to open pressText.txt and humidText.txt.
So it was just my dummy fault. Ty for all your help anyway. :smiley:

Haha. Anyway, using debug texts like "Opening press.txt". Then "press.txt opened" etc. is of good help very often!