Erratic behaviour - Memory probs? - using 4 sensors, SD Shield, RTC

Hi!
I am a little confused. After working through the Elegoo manual with different sensors and motors and so on I tried something on my own:
I combined a photoresistor, a water sensor, an inclination sensor and a humidity/temperature sensor. These I use with the arduino uno and a SD card with a RTC.

I implemented the sensors sequentially and until I use the blue humidity/temperature sensor everything runs fine. But when using the sensor and the corresponding libraries I get a totally erratic behavior: it jumps from the loop part to the setup part and so on. The output is strange.

The sensor itself runs fine. I tested it separately.

Can this be a problem of the memory. I am at 65%?

Erratic output:
20-08-2/l&À454197,1534791454,721,0,1,26,58
20-08-2/l&À454197,1534791454,758,0,1,26,57
t�,0�����,26,57�97,1534791454,758,0,1,26,57
20-08-2/l&À654197,1534791456,759,0,1,26,58
20-08-2/l&À754197,1534791457,763,0,1,26,57
20-08-2/l&À865197,1534791568,768,0,1,26,57
20-08-2/l&À965197,1534791569,763,0,1,26,56
����������������������ÿÿÿ��������Z���ÿÿÿÿÿÿ20-08-2/l'K637197,1534791736,659,0,1,27,55
20-08-2/l'K837197,1534791738,685,0,1,27,55

Correct output without humidity sensor:
20-08-2018 17:16:48,1534781808,957,0,1
20-08-2018 17:16:49,1534781809,956,0,1
20-08-2018 17:16:51,1534781811,956,0,1
20-08-2018 17:16:52,1534781812,956,0,1
20-08-2018 17:16:53,1534781813,956,0,1

Here is the code.

#include <Wire.h>
#include <DS3231.h>

#include <SPI.h>
#include <SD.h>
#include <SimpleDHT.h>

DS3231 clock;
RTCDateTime dt;
File myfile;
const int chipSelect = 10;
const String filename = "data6.txt";

int pinDHT = 3;   //humidity sensor
SimpleDHT11 dht11;

void setup() {

  Serial.begin(9600);
  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);
  Serial.println("Initializing SD card");
  pinMode(SS, OUTPUT);
  
  pinMode(2, INPUT);    //inclination sensor
  digitalWrite(2, HIGH);

  if (!SD.begin(chipSelect)){
    Serial.println("Initialisation failed...");
    return;
  }

  myfile = SD.open(filename, FILE_WRITE);
  delay(5000);
 if (myfile) {
  Serial.println("File opened... and gets closed");
  myfile.close();
 } else {
  Serial.println("error creating "+ filename);
 }
}

void loop() {
  myfile = SD.open(filename, FILE_WRITE);
  
  String time = "";
  byte temperature = 0;
  byte humidity = 0;
  byte data[40]={0};

  dt = clock.getDateTime();
  time += clock.dateFormat("d-m-Y H:i:s", dt);
  time += "," + String(clock.dateFormat("U", dt));
  
  String dataString="";
  for (int analogPin=0;analogPin<2; analogPin++){   // Fotowiderstand, feuchtgkeitsensor
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin<1){
      dataString += ",";
    }
  }

  dataString = time + "," + dataString + "," + String(digitalRead(2));

  if (dht11.read(pinDHT, &temperature, &humidity, data)){
   Serial.println("Read DHT11 failed...");
   return; 
  }

  Serial.println((int)temperature,  (int) humidity);
  dataString += ","; 
  dataString += String((int)temperature) + "," + String((int) humidity);

  Serial.println(dataString);     //output to serial and SD card
  delay(500);
  myfile.println(dataString);
  
  Serial.flush();

  delay(1000);
  myfile.close();
}
const String filename = "data6.txt";

There is NO excuse for wrapping a string in a const String instance.

  Serial.println("Initializing SD card");

There is no excuse for pissing away SRAM uselessly.

Serial.println(F("Initializing SD card"));

There is NO excuse for creating one huge String instance, when it is just as easy to write one piece of data at a time to the file.

You're abusing String.

When you're printing to serial, never use String to build up one long string to print; string is bad in general, but starting with a small string and appending pieces onto it is the method of use that is most likely to cause memory fragmentation problems.

Just print the string piecewise with serial.print (and serial.println for the end of a line).

I'm not convinced that that's the entirety of the problem, but what you're doing invites weirdo memory issues, so you should fix that before proceeding.

Arduino really should never have created or included the String class - they act in their documentation like String is safe to use on AVRs, but in the types of situations that it's convenient and makes coding easier, it causes memory issues and is not safe to use.

Thank you very much for the comments on the code, esp. the string method. I didn't know that as I am new to C/C++ and arduino. And I was only extending the code I got in the small examples.

Maybe the library of several sensors has interfered, please do not output the file to TXT, and try to output Serial.println() directly via using the serial port.