Main loop halts due to for loop function.

I'm fairly new to arduino and have run into a problem on my current project.
I have a loop that basically is a thermostat function and sends values with pubsubclient.
This part works fine.

However, i have also a function (ledfadeon())that is called on sunrise to start fading in a LED light.
This function uses a for loop. My problem is when i call this function, nothing else can happen in the main loop until the for loop of the dimming function ends.

How can i run this dimming loop in "parallel" to the main loop? Or any way to avoid this problem?

Any help is highly appreciated!

Here are the for loop functions:

void ledfadeON(){
 for (int i=0; i < 1024; i++){
  for (j=0; j<sunrisespeed; j++)
  {
      analogWrite(pulsepin, i);
      delay(1);
 
   } 
  Serial.println(i);
 }
  dimmedon = true;
  dimmedoff = false;
}


void ledfadeOFF(){
 for (int i=1023; i > 0; i--){
  for (j=0; j<sunrisespeed; j++)
  {
      analogWrite(pulsepin, i);
      delay(1);
 
   } 
  Serial.println(i);
 }
   dimmedoff = true;
   dimmedon = false;
   digitalWrite(pulsepin, LOW);
}

And here is my main loop:

void loop() {

    DS18B20.requestTemperatures();
    temp = (DS18B20.getTempCByIndex(0)*1000)+273150;

    if (OnOff == 1) {                                //Enable / Disable thermostat function
      thermostat();
    }
    else {
      digitalWrite(relayPin,LOW);
    }
    
    timer.run();
    today[3] = day(epochtime);
    today[4] = month(epochtime);
    today[5] = year(epochtime);   // store today's date (at noon) in an array for TimeLord to use
    tardis.SunRise(today);        // calculate sunrise time
    sunrisehour = today[tl_hour];
    sunriseminute = today[tl_minute];
    mSunrise = today[2] * 60 + today[1];
    tardis.SunSet(today);         // calculate sunset time
    sunsethour = today[tl_hour];
    sunsetminute = today[tl_minute];
    mSunset = today[2] * 60 + today[1];
   
    minOfDay = hour(epochtime) * 60 + minute(epochtime);
    if (minOfDay < mSunrise || minOfDay >= mSunset){
      isDay = 0;
      isNight = 1;
      if (!dimmedoff){
       [b]ledfadeOFF();[/b]

        Serial.println("Started Sunset Sequence");
        client.publish(mqttDimmingTopic, String("Started Sunset Sequence").c_str(), true);
      }
      
    }
    else {
      isDay = 1;
      isNight = 0;
      if (!dimmedon){
       [b]ledfadeON()[/b];

        Serial.println("Started Sunrise Sequence");
        client.publish(mqttDimmingTopic, String("Started Sunrise Sequence").c_str(), true);
      }
    }


    
    if(wifiMulti.run() != WL_CONNECTED) {
        Serial.println("WiFi not connected!");
        delay(1000);
        return;
    }
    else {
    }


    if (!client.connected()) {
        Serial.println("Connecting to MQTT server");
        if (client.connect(sensorHostname)) {
          client.subscribe(inTopic);
          
        } else {
          Serial.println("Could not connect to MQTT server");
          digitalWrite(2,HIGH);                         //Disable onboard LED when mqtt server connection has failed to initialize.
          delay(1000);
          return;
        }
    }

    if (client.connected()) {
        client.loop();
        digitalWrite(2,LOW);                            //Enable onboard LED when mqtt server connecton is present.    
        if (millis() - lastUpdate >= updateInterval) {
            if (measureAndPublish()) {
                lastUpdate = millis();
            } else {
                // retry earlier on error
                lastUpdate = millis() - updateInterval + retyInterval;
            }
        }
    }
   
    
}

You need to study "blink without delay" and more details are provided in Robin's post Demonstration code for several things at the same time

You will have to restructure a bit the architecture of your code

I had seen that example but was not sure if it would help me. I'll try to understand it some more.

Should I change the dimming function to not use a for loop, or just the way these functions are called in the main loop?

I'm sorry but my experience with coding is very little still.

Should I change the dimming function to not use a for loop, or just the way these functions are called in the main loop?

yes - that's the idea. once you are in your for loop, you are stuck there and whatever else you want to do in the main loop() has to wait.

I have now changed my fading code.

When i run it in a standalone sketch with interval 30ms it works fine.
But when i add it in my main sketch the LED starts flickering during the fade. I'm not sure where the problem is. But i guess it's due to the fact that the other things in the pool are messing with the timing?

I have attached my sketch.

ds18b20-node.ino (11.9 KB)