MKR1000 reproducibly resetting when switching for ca. 16s

Hi all,

I want to control a Peltier heating element connected via 2 relays to Pins 1 & 2 of my MKR 1000. I want to read the temperature via a DHT22 sensor and then cool or heat accordingly.

I have written 2 functions "cooling" and "heating" (see code) that take a duration of milliseconds and then cool and heat for the respective time.

Whenever the combined time of cooling or heating is more than 12000 ms, the board resets.

This is reproducible.

It has nothing to do with actually switching the relays. I have commented out the digitalWrites in both cooling and heating function. I have also disconnected the relays. The board still resets after calling the heating function.

At first I thought this would be because of some overflow in an int variable, but with unsigned longs this shouldn't happen, right?

If I change the duration for "heating" to 2000 it works. It it is 5000, the MKR1000 always resets after heating...

Can anybody please give me a tip?

Thanks!

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Tester"

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float temp;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#include "DHT.h"

#define DHTPIN 7     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);

// Set Pins for Relais
const int Relais1 = 1;
const int Relais2 = 2;


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.println(F("DHTxx test!"));

  dht.begin();
  
  delay(1500);
  
  pinMode(Relais1, OUTPUT);
  pinMode(Relais2, OUTPUT);
}

int i = 1;

void loop() {
  ArduinoCloud.update();
  
  digitalWrite(LED_BUILTIN, HIGH);
  cooling(10000, false);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  heating(5000, false);
  delay(1000);
  
  temp = dht.readTemperature();
  
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" i: ");
  Serial.println(i);
  
  
  i++;
  
  
  
}


void cooling (unsigned long durationMils, bool updt){
    /*
    Default state = both relais off/NC = normal polarity = cooling
    */
    
    Serial.println("This is cooling.");
    
    unsigned long now_millis = millis();
    unsigned long then_millis = now_millis + durationMils;
    
    Serial.println("Switching.");
    
    digitalWrite(Relais1, HIGH);      // switch Relais 1 OFF (High)
    digitalWrite(Relais2, HIGH);      // switch Relais 2 OFF (High)
    
    while (now_millis < then_millis){
      now_millis = millis();
      if (updt) {
        //update_all_vars();
      }
    }
    Serial.println("Cooling end.");
}

void heating (unsigned long durationMils, bool updt){
    /*
    ON State = both relais ON/NO = inverted polarity = heating
    */
    
    Serial.println("This is heating.");
    
    unsigned long now_millis = millis();
    unsigned long then_millis = now_millis + durationMils;
    
     Serial.println("Switching.");
    
    //digitalWrite(Relais1, LOW);       // switch Relais 1 ON (LOW)
    //digitalWrite(Relais2, LOW);       // switch Relais 1 ON (LOW)
    
    while (now_millis < then_millis){
      now_millis = millis();
      if (updt) {
        //update_all_vars();
      }
    }
    Serial.println("Heating end.");
}

void idling (unsigned long durationMils, bool updt){
    /*
    Mixed State = relais 1 OFF/NC, relais2 ON/NO = no polarity / both wires GND = idle
    */
    
    Serial.println("This is idling.");
    
    unsigned long now_millis = millis();
    unsigned long then_millis = now_millis + durationMils;
    
    digitalWrite(Relais1, LOW);       // switch Relais 1 ON (LOW)
    digitalWrite(Relais2, HIGH);      // switch Relais 2 ON (HIGH)
    
    while (now_millis < then_millis){
      now_millis = millis();
      if (updt) {
        //update_all_vars();
      }
    }
    
    
}

Can the Cloud Thing update method tolerate seconds-long delays? Update methods are usually expected to be called thousands or at least hundreds of times a second.