RTC is not running

Hey,
got the problem that RTC on Arduino is not working well. I get the time right from Windows, but it does not count. So what i get from Arduino and RTC is always the time of uploading my sketch. I tried 3 RTCs … it seems to be a mistake in my code.

Please help me out.

Here is my Code:

// Einbindung der Bibliotheken
#include "DHT.h"
#include <Wire.h>
#include <rgb_lcd.h>
#include <SD.h>
#include <SPI.h>
#include "RTClib.h"



// Temperatur und Luftfeuchtigkeitssensor einbinden
DHT dht; 

// Echtzeituhr einbinden
RTC_DS1307 rtc;

// rgb Display definieren
rgb_lcd lcd;
  byte DEGREE_SYMBOL = 0;
  byte degree[8] = {
  0b00000,
  0b00010,
  0b00101,
  0b00010,
  0b00000,
  0b00000,
  0b00000,
  0b00000
  };

//Festlegung von Intervalen 
unsigned long WRITE_INTERVAL = 1000; // 1 Minuten
unsigned long lastWrite = WRITE_INTERVAL;

// Festlegung von Intervalen um auf LCD Display anzuzeigen
unsigned long DISPLAY_UPDATE_INTERVAL = 200; // 200 Millisekunden
unsigned long lastDisplayUpdate = DISPLAY_UPDATE_INTERVAL;


int SELECTED_CHIP = 4;
int count = 0;

//Definieren der Variable dayOfTheWeek um ggf. Wochentage anzuzeigen
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


void setup() {
  // Verbindungsaufbau
  Serial.begin(9600);

  // DataTime now tc.now zuwesien
  DateTime now = rtc.now();

  // Zeit und Datum der Kompilation aus Windows auslesen und übernehmen
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));



    // Aktivierung der Echtzeituhr
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");

  }

  
   // Verbindung zum Display herstellen und Display initialisieren 
   //2 Zeilen mit je 16 Zeichen
   lcd.begin(16, 2); 

   // Das neue "°"-Symbol beim Display registrieren
   lcd.createChar(DEGREE_SYMBOL, degree);  



  
  // SD Karte inizialisieren
  int selectedChip = 4;
    if (!SD.begin(selectedChip)) {
    Serial.println("SD-Card failed or is missing");
 } else {
    Serial.println("SD-Card initialized.");  
}


  // Verbindung zu DHT 22 auf Pin 2 herstellen
  dht.setup(2);

 String lineHeadTable = "Time;Temperature;Humidity;System was restarted";
  writeToSD (lineHeadTable);
     Serial.println(lineHeadTable);
 
}


// Schreiben der Daten in eine erstellte .csv Datei
void writeToSD(String lineContent) {

    // DataTime now rtc.now zuwesien
  DateTime now = rtc.now();
    // Zeit und Datum der Kompilation aus Windows auslesen und übernehmen
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  // Erstellung eines dynamischen Filename (1LOGMMDDHHMM), 
  // beginnend mit Ziffer n=[1;5] für die jeweilige Messstation (1, 2, 3, 4, 5)
  // folgend mit MM (Monat) da nur 8"."+3 (csv) Charakter zur Verfügung stehen um File zu benennen. 
  // Jahr und Sekunde trotzdem definiert um für andere Messungen anpassen zu können.
 int y = now.year();
 int m = now.month();
 int d = now.day();
 int h = now.hour();
 int mi = now.minute();
 int s = now.second();
 
 char filename[11] = {'\0'};      

 sprintf(filename,"%d%d%d.csv", d, m, y);

  
 File dataFile = SD.open(filename, FILE_WRITE);
 if (dataFile) {
  Serial.println (filename);
   dataFile.println(lineContent); // Auf die SD-Karte schreiben
   dataFile.close();
   Serial.println(lineContent); // Zusätzlich auf serielle Schnittstelle schreiben zum Debuggen
 }
 else {
   Serial.println("Error opening datafile");
 }


}



  
void loop() {
  
  delay(dht.getMinimumSamplingPeriod());

    // DataTime now tc.now zuwesien
  DateTime now = rtc.now();

    // Zeit und Datum der Kompilation aus Windows auslesen und übernehmen
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  // Variblenzuweisung
  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  // Überprüfung der Werte 
  if (isnan(temperature) || isnan(humidity)) {       
  Serial.println("Fehler beim lesen von DHT");
  return;
  }
  if (temperature > 37) {       
  Serial.println("Wert ungültig");
  return;
  }
  if (temperature < -20) {       
  Serial.println("Wert ungültig");
  return;  
  }
  if (humidity > 100) {       
  Serial.println("Wert ungültig");
  return;
  }
  if (humidity < 0) {       
  Serial.println("Wert ungültig");
  return;
  }

  //Delay zur Anzeige über LCD-Display (3 Farbstufen)
  if (millis() - lastDisplayUpdate > DISPLAY_UPDATE_INTERVAL) {
  const float coldest = 0;
  const float hottest = 30;
  int red = limit(255 * (temperature - coldest) / (hottest - coldest));
   
  // Farben von blau über lila nach rot
  int green = 0;
  int blue = limit(255 * (hottest - temperature) / (hottest - coldest));
  updateDisplay(red, green, blue, temperature, humidity);
  lastDisplayUpdate = millis();
 }

  // delay zur Auslesung der Werte von DHT22 und Echtzeituhr
  if (millis() - lastWrite > WRITE_INTERVAL) {
  String line = String(GetTime()) + ";" + String(temperature) + ";" + String(humidity);
  writeToSD(line);
  lastWrite = millis();

}
}

  // Abfragen der Zeit und Datum
  String GetTime() {
    
  // DataTime now rtc.now zuwesien
  DateTime now = rtc.now();

    // Zeit und Datum der Kompilation aus Windows auslesen und übernehmen
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  String t = String (now.day());
  t += String(".");
  t += String(now.month());
  t += String(".");
  t += String(now.year());
  t += String(" ");
  t += String(now.hour());
  t += ":";
  t += String(now.minute());
  t += ":";
  t += String(now.second());
  return t;
  }
  
  void updateDisplay(int red, int green, int blue, float temperature, float humidity) {
    lcd.setRGB(red, green, blue);

    lcd.setCursor(0, 0);
    lcd.print("T: ");
    lcd.print(temperature);
    lcd.write(DEGREE_SYMBOL);
    lcd.print("C");

    lcd.setCursor(0, 1);
    lcd.print("H: ");
    lcd.print(humidity);
    lcd.print("%");
  }

  float limit(float color) { // Farbwerte müssen im Bereich 0..255 liegen
    if (color < 0) {
    return 0;
 }
    else if (color > 255) {
    return 255;
 }
    else {
    return color;
 }
}

this line  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));is what sets the time of your RTC to the last compile time. My german is limited but you have a comment that should give you a clue...
Zeit und Datum der Kompilation aus Windows auslesen und übernehmen

so you should get rid of it once the RTC has been set to the right time... and you have that all over the place so it's not a smart idea...

-> run it once with it in setup, then comment the line out and upload your code again

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

We would not recommend to use the String class... will lead to disappointment after a while...