Script only runs if it is connected with the computer

Hey my Script work if i upload it and have th arduino connected to my computer, but it does not run if i put it on a charging port.

This is the Script:

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <FastLED.h>
#include <DFRobotDFPlayerMini.h>

#define ANZAHL_LEDS 12
#define HELLIGKEIT 100
#define LED_PIN 6
#define CLOCK_PIN 13
CRGB leds [ANZAHL_LEDS];

int gelb = 3;                                 // LED Pin
int rot = 4;
int sensor=A0;                               // Der Signal Pin vom Sensor
int sensorstatus=0;                          // Status vom Sensor
int TasterPin=2;  
int val,x=0;
int kraft=analogRead(A0); 
int y = 0;  
uint8_t BlinkValue = 0;                      // Blink-Status
uint8_t TasterValue = 0;                     // Taster-Status
uint8_t status = 0;
uint8_t type;
unsigned long actTime = 0;
unsigned long remTime = 0;
const long interval = 1000;
bool is_blue = false;
int random_zaehler = 0;

SoftwareSerial mySoftwareSerial(10,11); // RX, TX, Lautsprecher
DFRobotDFPlayerMini myDFPlayer;
  uint8_t getDetail(uint8_t type){
    return type;
  }
  int random_int(int min, int max)
  {
    return min + rand() % (max+1 - min);
  }

void setup()
{
    pinMode(gelb, OUTPUT);                    // LED als Ausgang definieren
    pinMode(rot, OUTPUT);
    pinMode(sensor, INPUT);                  // Sensor als Eingang defnieren
    pinMode(TasterPin, INPUT);    
    Serial.begin(9600);
    FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, ANZAHL_LEDS);
    FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);
    FastLED.setBrightness(HELLIGKEIT);
    mySoftwareSerial.begin(9600);
    //Serial.begin(115200);
    if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
      while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
   }
  }
  Serial.println(F("DFPlayer Mini online."));
}

void loop()
{
kraft = analogRead(A0);                  //Auslesen der Sensormesswerte -> "helligkeit"
//Serial.println(analogRead(A0));               //Ausgabe der Sensormesswerte

if (kraft > 120 && y==0){                // wenn sich Bobbi hinsetzt....
    //Serial.println("Bobbi sitzt");
    delay(3000);                              // ... wartet Buzz 10 Minuten (hier 2 Sekunden)
    y = 1; 
     }

kraft = analogRead(A0); 
//Serial.println(analogRead(A0));

if (kraft > 120 && y==1){                //wenn der Messwert unter 50 ist (und Bobbi immer noch sitzt)... 
     y = 2;                                   // wird y auf 2 umgeschaltet und zeigt an, dass dies der zweite Messwert ist.
     //Serial.println("Bobbi sitzt jetzt 30 Minuten");
       
}
kraft = analogRead(A0);
//Serial.println(analogRead(A0));

if (kraft < 120 && y==1){                //wenn der Messwert über 200 ist (und Bobbi aufgestanden ist)... 
     y = 0;                                   // wird y auf 0 zurückgeschaltet und alles beginnt von vorne.
     //Serial.println("Bobbi ist aufgestanden");
       
}



if (y==2){                                   //wenn Bobbi seit 30 Minuten sitzt..


  unsigned long actTime = millis();               //Timer
                                                        // Anfang BlinkWithoutDelay
         if (digitalRead(2) == LOW && x==0) {
             // Serial.println("Blink");
              digitalWrite(3, HIGH);
              digitalWrite(4, HIGH);
        if (actTime - remTime >= interval){
        remTime = actTime;
        if(!is_blue){
          is_blue = true;
        fill_solid(leds, ANZAHL_LEDS, CRGB::Blue);
          FastLED.show();
          }
        else {
        is_blue = false;
        fill_solid(leds, ANZAHL_LEDS, CRGB::Black);
          FastLED.show();
   }
  }
}                                                        // Ende BlinkWithoutDelay....
           
        
          if (digitalRead(2) == HIGH && x==0) {         //Wenn Bobbi den Knopf drückt (/auf den Buzzer haut)---
          digitalWrite(3, HIGH);                        // turn the LED on by making the voltage HIGH 
          digitalWrite(4, HIGH);
          fill_solid(leds, ANZAHL_LEDS, CRGB::Blue);
          FastLED.show();

          random_zaehler = random_int(1, 7);
         
          myDFPlayer.play(random_zaehler);                       //Play the first mp3 HIER DIE MUSIK EINFÜGEN !!!! Ggf. muss hier noch eingefügt werden, dass das Licht nach Ende des Liedes automatisch ausgeht und y=0 und y=0 gesetzt werden.
          myDFPlayer.volume(25);
          while(true){
            if (myDFPlayer.available()) {
              type = getDetail(myDFPlayer.readType()); //Print the detail message from DFPlayer to handle different errors and states.
              if(type == DFPlayerPlayFinished){
                digitalWrite(3, LOW);
                digitalWrite(4, LOW);
                fill_solid(leds, ANZAHL_LEDS, CRGB::Black);
                 FastLED.show();
                kraft = 0;
                x=0;                                         //alles startet von vorne
                y=0;                                         //alles startet von vorne
                delay(500); 
                break;
               }
            }
            if (digitalRead(2) == HIGH && x==1) {         //Wenn Bobbi den Knopf einzweites Mal drückt (/auf den Buzzer haut)...
              digitalWrite(3, LOW);
              digitalWrite(4, LOW);                       // turn the LED OFF by making the voltage LOW
              fill_solid(leds, ANZAHL_LEDS, CRGB::Black);
                FastLED.show();
              myDFPlayer.pause();                                              // HIER DIE MUSIK AUS !!!! GGF. muss man sich hier noch um einen Abbruch der Musik kümmern
              delay (500);
              kraft=0;
              x=0;                                         //alles startet von vorne
              y=0;                                         //alles startet von vorne
              break;
             }
          x=1;
          delay (500);
        }
        
          
         
  }
  }
}

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Once you have corrected the first post, explain how the DF Mini is wired as this code is blocking and since you removed the Serial.begin() you won't see any feedback

  if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true) {
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }

DemenzDinge_Buzzer_Franzi.ino (6,1 KB)

I uploaded the Skript maybe it is better to read now.

No...
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

yeah I removed the Serial.begin because i read in another post that this could fix the problem...

please fix your first post.

Done.

thanks

so can you answer this?

It is directly connected to the Arduino Nano. RX is D11 and TX D10 and it is charged by the Arduino.

Any ground sharing?

it works i added a delay in the void loop and fade one serial.begin out and now it works.
Thank you

None of your last post makes any sense. What do you mean by "fade out one serial.begin" ?