Play Sketch when the system is turned on

Hello,
I would like to record sensor data autonomously on an SD card with an Adalogger M0 and a BME 280 and battery. The system should get a power switch. As soon as the switch is pressed, the sketch should run. Unfortunately, it only works if I upload the sketch directly from the PC. Then I plug in my battery and remove the USB cable. The data will continue to be recorded. But only as long as a power supply is available. If I disconnect and reconnect the power supply, the sketch no longer runs. Is there a way that the sketch restarts as soon as it gets suspense again? What needs to be changed in the sketch?

Here is my Code:

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <DS3231.h>
#include "RTClib.h"


#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

unsigned long delayTime = 5000;
File myFile;
String Temperature, Pressure, Altitude, Humidity, Messung;
String Date; 
RTC_DS1307 rtc;

//Adafruit BME 280
Adafruit_BME280 bme; // I2C

// Real Time Clock 
DS3231 clock;
RTCDateTime dt;


void setup() {
  
  Serial.begin(57600);
  pinMode(13, OUTPUT);        //Testzweck: Um eine vollständige Speicherung zu erkennen
  
  
//************************ RTC Initialisierung ************************\\

 if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  else
  {
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
 //******************************* BME280 Initialisierung ****************************\\
 
  if (! bme.begin(0x77, &Wire)) 
  {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
  }

    bme.setSampling(Adafruit_BME280::MODE_NORMAL,
                    Adafruit_BME280::SAMPLING_X8,  // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1,  // humidity
                    Adafruit_BME280::FILTER_X8,
                    Adafruit_BME280::STANDBY_MS_0_5 );
    
    // suggested rate is 25Hz
    // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
    // T_ovs = 2
    // P_ovs = 16
    // H_ovs = 1
    // = 40ms (25Hz)
    // with standby time that should really be 24.16913... Hz


 //***************************** SD card Initialisierung ****************************\\
 
    while (!Serial)
    {
      Serial.print("Initializing SD card...");

      if (!SD.begin(4)) 
      {
        Serial.println("initialisation failed!");
        while(1);
      }
      
      Serial.println("inistalisation done");
    }
   myFile = SD.open("Messung.txt", FILE_WRITE);
   
   if (myFile)
   {
    myFile.println("Date | Time | Temperature (°C) | Pressure (hPa) | Humidity (%) \r\n");
    myFile.close();
   }
   
   else 
   {
    Serial.println("error opening Messung.txt");
   }
   //delayTime = 5000;
}
void loop() 
{
 bme.takeForcedMeasurement(); // has no effect in normal mode
  printRTC();
  printBME280();
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13,LOW);
  
  delay(delayTime);
    
}
void printRTC()
{
 DateTime time = rtc.now();
  Serial.print (String("DateTime::TIMESTAMP_DATE:\t")+ time.timestamp(DateTime::TIMESTAMP_DATE));
  Serial.print(String("DateTime::TIMESTAMP_TIME:\t")+ time.timestamp(DateTime::TIMESTAMP_TIME));

  Serial.println("\n");
Serial.println("Beginne zu speichern");

  

myFile = SD.open("Messung.txt", FILE_WRITE);
if(myFile){
myFile.print(time.timestamp(DateTime::TIMESTAMP_DATE));
myFile.print(" | ");
myFile.print(time.timestamp(DateTime::TIMESTAMP_TIME));
myFile.print(" | ");
myFile.close();
Serial.println("gespeichert");
}
  else{
    Serial.println("error opening Sensordaten.txt nach RTC");
  }
  }

void printBME280() {

  String Temperature = String(bme.readTemperature(), 2);
  String Pressure = String(bme.readPressure() / 100.0F,2);
  String Humidity = String(bme.readHumidity(),2);

  Messung = Temperature + " | " + Pressure + " | " + Humidity;
  Serial.println("Save Sensordata");
  Serial.println(Messung);

  myFile = SD.open("Messung.txt", FILE_WRITE);
  if(myFile) {
    Serial.println("Writing to Messung.txt...");
    myFile.print(Messung);
    myFile.print("\n");
    myFile.close();
    Serial.println("done");
  }
  else{
    Serial.println("error opening Sensordaten.txt nach Sensor");
  }
}

If your arduino is powered correctly it will run your code as soon as powered on

The sketch will run as soon as power is provided from any source. What makes you think that it is not running ?

NOTE that the way that it is written the sketch will always set the current time on the RTC to the date and time that the sketch was compiled, not the actual current time which is probably not what you want

What does while(1); achieve? Infinite loop? Do you think you might get trapped in 1 when you are not connected to serial

I'm not familiar with your board but it has support for native USB as far as I know. In which case the below will apply.

Your sketch does not get past the while(!Serial) if you don't have a communication channel open (e.g. serial monitor).

There are strange things in your sketch that make me wonder

  while (!Serial) {
    Serial.print("Initializing SD card...");

    if (!SD.begin(4)) {
      Serial.println("initialisation failed!");
      while (1)
        ;
    }

    Serial.println("inistalisation done");
  }
  myFile = SD.open("Messung.txt", FILE_WRITE);

That reads:
if no communication channel is open, print something (that is acceptable although a bit silly as it will get lost) and next initialise the SD card; repeat till the communication channel is open. So you run hundreds of times SD.begin() which does not make sense.

I have no reason to believe that your board differs from other boards with native USB. In which case, if your communication channel was open and you disconnect your board (or even close serial monitor, eventually all the serial prints will start blocking the code.

You can load the below code for testing all of the above

//const uint8_t pinLED = 17;              // For SparkFun Pro Micro, the RX LED has a defined Arduino pin
const uint8_t pinLED = LED_BUILTIN;   // For Arduino Leonardo/Micro, use the built in LED

void setup()
{
  pinMode(pinLED, OUTPUT);  // Set LED as an output

  Serial.begin(57600);
  uint8_t LEDState = LOW;

  uint32_t lastUpdateTime = 0;
  while (!Serial)
  {
    if (millis() - lastUpdateTime >= 100)
    {
      lastUpdateTime = millis();
      LEDState = !LEDState;
      digitalWrite(pinLED, LEDState);     // toggle the LED
    }

  }
  Serial.println("Hello world");
}

void loop()
{
  digitalWrite(pinLED, LOW);     // set the RX LED ON
  delay(500);
  digitalWrite(pinLED, HIGH);    // set the RX LED OFF
  delay(500);
  
  Serial.println("..................");
}

You might have to adjust the pin of the built in LED.

Run the sketch with serial monitor closed; it will flash at a high rate. When you open serial monitor, it will change to a lower flash frequency and start printing dots.

Close serial monitor and observe the blink pattern; it will eventually slow down.

Now disconnect your board from the computer and power your board externally; you will never get past the fast flashing blink.

//Edit
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Thank you very much for the understandable explanations!!
So I now have to rewrite my program bit by bit so that no serial monitor is expected.
I always had this in it for "test purposes" to see if everything is stored as it should. I can now display the individual steps as an LED code to see if everything works.
I'll put the sketch in again afterwards.

You don't have to get rid of all the serial stuff. I will give some pointers once our power is back; two hours or so :frowning:

That's the program code so far. The system initializes the RTC DS1307, BME280 and SD card. Takes the date, time, and sensor data and stores it in the TEST.txt file. It also works when turning off and turning it on again.

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

File myFile;
RTC_DS1307 rtc;
unsigned long delayTime = 5000;       //define the Delay Time 
const uint8_t pinLED = LED_BUILTIN;   //define the onboard LED PIN (RED)
const uint8_t pinLED2 = 8;            //define the second onboard LED (Green)
const uint8_t BUTTON = 9;             //define the button for StateLED request
uint8_t LEDState = LOW;               //State of the LED is OUT
String Temperature, Pressure, Humidity;
String Messung, Date;
Adafruit_BME280 bme;
int z;                                //Variable for status request

void setup() 
{
  pinMode(pinLED, OUTPUT);            //Set LED as an output
  pinMode(BUTTON, INPUT_PULLUP);      //Set Button to input pull-up mode
  

  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));    //get Date from PC
 
  bme.begin();
  bme.setSampling(Adafruit_BME280::MODE_NORMAL,
                    Adafruit_BME280::SAMPLING_X8,  // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1,  // humidity
                    Adafruit_BME280::FILTER_X8,
                    Adafruit_BME280::STANDBY_MS_0_5 );
                    
  SD.begin(4);                        //Initialisierung SD-Karte   
                   
/*  if(!rtc.begin())
  {
    z = 1;
  }
*/
/*  if(!bme.begin())
  {
    z = 2;
  }
*/  
/*  if(!SD.begin())
  {
    z = 3;
  }
*/
  delay(1000);                               //Break between initialisation and write to data
  myFile = SD.open("TEST.txt", FILE_WRITE); //open the SD-Carte to "TEST"
                                            
  if (myFile)                               //if the File available...
  {
    myFile.println("Date | Time | Temperature | Pressure | Humidity");
    myFile.close();
    digitalWrite(pinLED, HIGH);             //only to show it write in there
    delay(3000);
    digitalWrite(pinLED, LOW);
  }
/*  else
  {
    z = 4;
  }
 */   
}

void loop() 
{
  printRTC();
  printBME280();
  Button();
  for (int i = 0; i <= 5; i++)          //to see that one messure is over
  {
    LEDState = !LEDState;
    digitalWrite(pinLED, LEDState);     // toggle the LED
    delay(500);
  }
  delay(delayTime);

}

void printRTC()
{
    DateTime time = rtc.now();          //take time from RTC and write to DateTime
    
    myFile = SD.open("TEST.txt", FILE_WRITE);

    if(myFile)
    {
      myFile.print(time.timestamp(DateTime::TIMESTAMP_DATE)); //write the Date
      myFile.print(" | ");
      myFile.print(time.timestamp(DateTime::TIMESTAMP_TIME)); //write the Time
      myFile.print(" | ");
      myFile.close();
    }
}

void printBME280()
{
  String Temperature = String(bme.readTemperature(), 2);
  String Pressure = String(bme.readPressure() / 100.0F,2);
  String Humidity = String(bme.readHumidity(),2);

  Messung = Temperature + " | " + Pressure + " | " + Humidity;

  myFile = SD.open("TEST.txt", FILE_WRITE);
  if(myFile)
  {
    myFile.println(Messung);
    myFile.close();
  }
}

void Button()
{
  if (digitalRead(BUTTON) == HIGH) // Button drücken und Status wird durch LEDs ausgegeben
  {
    switch (z)
    {
      case 0: //system works without error
        digitalWrite(pinLED2, HIGH);
        delay(2000);
        digitalWrite(pinLED2, LOW);
        delay(500);
        break;
      case 1: //RTC initialisation failed
        for (int i = 0; i <= 1; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 2: //BME280 initialisation failed
        for (int i = 0; i <= 3; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 3: //SD-Card initialisation failed
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 4: //check RTC connection
        for (int i = 0; i <= 7; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 5: //check BME280 connection
        for (int i = 0; i <= 9; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 6:  //check SD-Card Connection
        for (int i = 0; i <= 11; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 7: //exceeding the measured value limits
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle both the LED
          digitalWrite(pinLED2, LEDState);
          delay(500)
        }
      
    }
  }  
}

The next step should be, when pressing a button, the status LED should be displayed with the described cases 1-7.
The display should be possible at any time, even during the delay time.
You could use an attachInterrupt for this, right?

Another question would be, as soon as I interrupt the power supply for 5 min and reconnect, the RTC time stops and starts from the time of interruption. Can you help me with that?

I promised you some pointers. Sorry for the delay, ISP went down as well :frowning:

while(!serial waits for the communication channel to be opened. It is used if you do not want to miss the first serial messages in serial monitor; if you're not interested inthe first few messages, just leave the while(!serial out. If you want to keep the capability of debugging and don't want to miss the first messages, you can add a second condition for a timeout. In below code, you have 10 seconds to open serial monitor, after that the code continues.

const uint8_t pinLED = LED_BUILTIN;   // For Arduino Leonardo/Micro, use the built in LED

void setup()
{
  pinMode(pinLED, OUTPUT);  // Set LED as an output

  Serial.begin(57600);
  uint8_t LEDState = LOW;

  uint32_t lastUpdateTime = 0;
  while (!Serial && millis() < 10000)
  {
    if (millis() - lastUpdateTime >= 100)
    {
      lastUpdateTime = millis();
      LEDState = !LEDState;
      digitalWrite(pinLED, LEDState);     // toggle the LED
    }

  }
  Serial.println("Hello world");
}

Serial printing will not block your code if you have never opened the communication channel and worked around the while(!serial. But if you did open it and closed it again, it will start blocking. To prevent blocking, you will have to check if there is enough space for the message that you want to send. You can use Serial.availableForWrite() for that. The longest message that needs to fit seems to be Could not find a valid BME280 sensor, check wiring! (51 plus 2 characters).
So you can use

if(Serial.availableForWrite() > 53)
{
  Serial.println("Could not find a valid BME280 sensor, check wiring!");
}

If you want to print a date in YYYY-MM-DD format, it would be 10 characters. If you use println

if(Serial.availableForWrite() > (10 + 2))
{
  Serial.println(formattedDate);
}

Having to do that for every print is a bit of a slep but it's the only way to prevent the propblem.

I'm only familiar with 32U4 based boards where the maximum serial output buffer size is 64; you will have to test how big the buffer on your board is. E.g.

void setup()
{
  Serial.begin(57600);
  while(!Serial);
  Serial.print("Serial output buffer size = ");
  Serial.println(Serial.availableForWrite());
}

void loop()
{
}

@sterretje
Thank you for explaining the features of the first code listed above.
what do you think about the second code?

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

File myFile;
RTC_DS1307 rtc;
unsigned long delayTime = 5000;       //define the Delay Time 
const uint8_t pinLED = LED_BUILTIN;   //define the onboard LED PIN (RED)
const uint8_t pinLED2 = 8;            //define the second onboard LED (Green)
const uint8_t BUTTON = 9;             //define the button for StateLED request
uint8_t LEDState = LOW;               //State of the LED is OUT
String Temperature, Pressure, Humidity;
String Messung, Date;
Adafruit_BME280 bme;
int z;                                //Variable for status request

void setup() 
{
  pinMode(pinLED, OUTPUT);            //Set LED as an output
  pinMode(BUTTON, INPUT_PULLUP);      //Set Button to input pull-up mode
  

  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));    //get Date from PC
 
  bme.begin();
  bme.setSampling(Adafruit_BME280::MODE_NORMAL,
                    Adafruit_BME280::SAMPLING_X8,  // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1,  // humidity
                    Adafruit_BME280::FILTER_X8,
                    Adafruit_BME280::STANDBY_MS_0_5 );
                    
  SD.begin(4);                        //Initialisierung SD-Karte   
                   
/*  if(!rtc.begin())
  {
    z = 1;
  }
*/
/*  if(!bme.begin())
  {
    z = 2;
  }
*/  
/*  if(!SD.begin())
  {
    z = 3;
  }
*/
  delay(1000);                               //Break between initialisation and write to data
  myFile = SD.open("TEST.txt", FILE_WRITE); //open the SD-Carte to "TEST"
                                            
  if (myFile)                               //if the File available...
  {
    myFile.println("Date | Time | Temperature | Pressure | Humidity");
    myFile.close();
    digitalWrite(pinLED, HIGH);             //only to show it write in there
    delay(3000);
    digitalWrite(pinLED, LOW);
  }
/*  else
  {
    z = 4;
  }
 */   
}

void loop() 
{
  printRTC();
  printBME280();
  Button();
  for (int i = 0; i <= 5; i++)          //to see that one messure is over
  {
    LEDState = !LEDState;
    digitalWrite(pinLED, LEDState);     // toggle the LED
    delay(500);
  }
  delay(delayTime);

}

void printRTC()
{
    DateTime time = rtc.now();          //take time from RTC and write to DateTime
    
    myFile = SD.open("TEST.txt", FILE_WRITE);

    if(myFile)
    {
      myFile.print(time.timestamp(DateTime::TIMESTAMP_DATE)); //write the Date
      myFile.print(" | ");
      myFile.print(time.timestamp(DateTime::TIMESTAMP_TIME)); //write the Time
      myFile.print(" | ");
      myFile.close();
    }
}

void printBME280()
{
  String Temperature = String(bme.readTemperature(), 2);
  String Pressure = String(bme.readPressure() / 100.0F,2);
  String Humidity = String(bme.readHumidity(),2);

  Messung = Temperature + " | " + Pressure + " | " + Humidity;

  myFile = SD.open("TEST.txt", FILE_WRITE);
  if(myFile)
  {
    myFile.println(Messung);
    myFile.close();
  }
}

void Button()
{
  if (digitalRead(BUTTON) == HIGH) // Button drücken und Status wird durch LEDs ausgegeben
  {
    switch (z)
    {
      case 0: //system works without error
        digitalWrite(pinLED2, HIGH);
        delay(2000);
        digitalWrite(pinLED2, LOW);
        delay(500);
        break;
      case 1: //RTC initialisation failed
        for (int i = 0; i <= 1; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 2: //BME280 initialisation failed
        for (int i = 0; i <= 3; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 3: //SD-Card initialisation failed
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 4: //check RTC connection
        for (int i = 0; i <= 7; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 5: //check BME280 connection
        for (int i = 0; i <= 9; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 6:  //check SD-Card Connection
        for (int i = 0; i <= 11; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500)
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 7: //exceeding the measured value limits
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState
          digitalWrite(pinLED, LEDState);       //toggle both the LED
          digitalWrite(pinLED2, LEDState);
          delay(500)
        }
      
    }
  }  
}

I don't need a serial monitor because it's supposed to be a self-sufficient system where the SD card is taken out and read out after the measurement. The addition is the LED code with two LEDs (red and green), which should always work when the button is pressed.

You will have to check the return value of SD.begin(). Same for bme.begin() and rtc.begin().
I see that you're attempting to do so but it's commented out. Note that the way you do it you will only have an indication of the last failure. E.g. if SD.begin() and bme,begin() both fail, you will not know about the first one.

Don't use single letter variables whose names have no meaning as global variables; if your variable reflects a status request, call it that (e.g. statusRequest).

Thanks for the hint!! I have to see how I implement this or whether errors have to be processed after error.

I changed the int z to int statusRequest. Now I know exactly what is meant.

Here the Full Code:

///////////////////////// Integration of libraries /////////////////////////
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

///////////////////////// Variable definition /////////////////////////
#define SEALEVELPRESSURE_HPA (1013.25)
File myFile;
RTC_DS1307 rtc;
unsigned long delayTime = 5000;         //define the Delay Time 
const uint8_t pinLED = 9;               //define the onboard LED PIN (RED)
const uint8_t pinLED2 = 10;             //define the second onboard LED (Green)
const uint8_t BUTTON = 11;              //define the button for StateLED request
uint8_t LEDState = LOW;                 //State of the LED is OUT
String Temperature, Pressure, Humidity;
String Messung, Date;
Adafruit_BME280 bme;
String statusRequest;                   //Variable for status request
char Str[] = "statusRequest";

///////////////////////// Main program setup /////////////////////////
void setup() 
{
  pinMode(pinLED, OUTPUT);              //Set LED as an output
  pinMode(BUTTON, INPUT_PULLUP);        //Set Button to input pull-up mode
  

  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));    //get Date from PC
 
  bme.begin();
  bme.setSampling(Adafruit_BME280::MODE_NORMAL,
                    Adafruit_BME280::SAMPLING_X8,  // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1,  // humidity
                    Adafruit_BME280::FILTER_X8,
                    Adafruit_BME280::STANDBY_MS_0_5 );
                    
  SD.begin(4);                        //Initialisierung SD-Karte   
                   
  if(!rtc.begin())
  {
    statusRequest = 1;
  }

  if(!bme.begin())
  {
    statusRequest = 2;
  }
  
  if(!SD.begin())
  {
    statusRequest = 3;
  }

  delay(1000);                               //Break between initialisation and write to data
  myFile = SD.open("TEST.txt", FILE_WRITE); //open the SD-Carte to "TEST"
                                            
  if (myFile)                               //if the File available...
  {
    myFile.println("Date | Time | Temperature | Pressure | Humidity");
    myFile.close();
    digitalWrite(pinLED, HIGH);             //only to show it write in there
    delay(3000);
    digitalWrite(pinLED, LOW);
  }
  else
  {
    statusRequest = 6;
  }
    
}

///////////////////////// Main program loop /////////////////////////
void loop() 
{
  printRTC();
  printBME280();
  Button();
  for (int i = 0; i <= 5; i++)          //to see that one messure is over
  {
    LEDState = !LEDState;
    digitalWrite(pinLED, LEDState);     // toggle the LED
    delay(500);
  }
  delay(delayTime);

}

///////////////////////// Subroutine RTC ///////////////////////// 
void printRTC()
{
    DateTime time = rtc.now();          //take time from RTC and write to DateTime
    
    myFile = SD.open("TEST.txt", FILE_WRITE);

    if(myFile)
    {
      myFile.print(time.timestamp(DateTime::TIMESTAMP_DATE)); //write the Date
      myFile.print(" | ");
      myFile.print(time.timestamp(DateTime::TIMESTAMP_TIME)); //write the Time
      myFile.print(" | ");
      myFile.close();
    }
}

///////////////////////// Subroutine BME280 Sensor /////////////////////////
void printBME280()
{
  String Temperature = String(bme.readTemperature(), 2);
  String Pressure = String(bme.readPressure() / 100.0F,2);
  String Humidity = String(bme.readHumidity(),2);

  Messung = Temperature + " | " + Pressure + " | " + Humidity;

  myFile = SD.open("TEST.txt", FILE_WRITE);
  if(myFile)
  {
    myFile.println(Messung);
    myFile.close();
  }
  
///////////////////////// Measurement control /////////////////////////
  if((Temperature >= String (65)) || (Temperature <= String (-10.00)) || (Pressure < String (890.00)) || (Pressure > String (1055.00)) || (Humidity > String (75.00)))
  {
    statusRequest = 7; 
  }
}

///////////////////////// Subroutine button /////////////////////////
void Button()
{
  if (digitalRead(BUTTON) == HIGH) // if the button is pressed, 
                                   // compare the variable "statusRequest" 
                                   // and output the respective code
  {
    switch (statusRequest)
    {
      case 0: //system works without error
        digitalWrite(pinLED2, HIGH);
        delay(2000);
        digitalWrite(pinLED2, LOW);
        delay(500);
        break;
      case 1: //RTC initialisation failed
        for (int i = 0; i <= 1; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 2: //BME280 initialisation failed
        for (int i = 0; i <= 3; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 3: //SD-Card initialisation failed
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 4: //check RTC connection
        for (int i = 0; i <= 7; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 5: //check BME280 connection
        for (int i = 0; i <= 9; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 6:  //check SD-Card Connection
        for (int i = 0; i <= 11; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle the LED
          delay(500);
        }
        digitalWrite(pinLED2, HIGH);
        delay(500);
        digitalWrite(pinLED2, LOW);
        break;
      case 7: //exceeding the measured value limits
        for (int i = 0; i <= 5; i++)
        {
          LEDState = !LEDState;
          digitalWrite(pinLED, LEDState);       //toggle both the LED
          digitalWrite(pinLED2, LEDState);
          delay(500);
        }
      
    }
  }  
}

int statusRequest;
works

even something like
int blablaOhhUhh76543;

will work as the name of an integer-variable

Don't use two calls to the begin() method of each device, o e should be sufficient.

Why attempt to write to SD regardless of the result of SD.begin()? If SD.begin() fails, writing is useless.

Is there any need to continue if one of the devices fails?

But I have to initialize the devices with begin() or can I make a query about it and set the variable "stausRequest" for it?

No, the system could stop as soon as a device does not work. If the sensor does not work it is useless, if the RTC does not work I can not measure time. In addition, I wanted to put the controller in sleep mode and wake it up again through the RTC (every 15min)

Yes, but you do it twice for each device

void setup() 
{
  pinMode(pinLED, OUTPUT);              //Set LED as an output
  pinMode(BUTTON, INPUT_PULLUP);        //Set Button to input pull-up mode
  

  rtc.begin();
  ...
  ...
  if(!rtc.begin())
  {
    statusRequest = 1;
  }

statusRequest should be an int, the same z was.