Sketch causes the arduino not to be recognized anymore

Hello,

When uploading the following sketch everything works as expected

#include "DHT.h"
#include <MKRWAN.h>
DHT dht;
LoRaModem modem(Serial);

#define SECRET_APP_EUI "XXXXXXXXXXXXXXXXX"
#define SECRET_APP_KEY "XXXXXXXXXXXXXXXXXXXXXXXX"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;


void setup()
{
  

  Serial.begin(9600);
  while (!Serial);
  // change this to your regional band (eg. US915, AS923, ...)
  if (!modem.begin(EU868)) {
    Serial.println("Failed to start module");
    while (1) {}
  };
  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  modem.minPollInterval(60);

  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");

  dht.setup(2); // data pin 2
}

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

    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    int16_t temp = (int16_t) (temperature * 100);
    int16_t hum = (int16_t) (humidity * 100);
  

    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print(humidity, 1);
    Serial.print("\t\t");
    Serial.print(temperature, 1);
//  Serial.print("\t\t");
//  Serial.println(dht.toFahrenheit(temperature), 1);

    byte data[4];
    data[0] = (temp >> 8) & 0xFF;
    data[1] = temp;
    data[2] = (hum >> 8) & 0xFF;
    data[3] = hum;
  
    int err;
    modem.beginPacket();
    modem.write(data,sizeof(data));
    err = modem.endPacket(true);
    if (err > 0) {
      Serial.println("Message sent correctly!");
    } else {
      Serial.println("Error sending message :(");
      Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
      Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
    }
    delay(10000);
    if (!modem.available()) {
      Serial.println("No downlink message received at this time.");
      return;
    } 
}

I then decided to add the RTC library so that I can put the arduino to sleep when I do not take any measurements. I took parts of code from this forum. However when I upload the sketch not only does it not work but it also seems impossible to upload any other sketch

The port section in IDE is greyed out and when I try to upload a sketch I get the following

"Couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."

The only way to recover is by pressing the reset button twice while uploading another sketch. But then if I upload the same sketch I run into the same issue. So I assume there is something wrong with the code i am using.

Can somebody see what am I doing wrong ? Here's the code

#include <RTCZero.h>
#include "DHT.h"
#include <MKRWAN.h>
RTCZero rtc;
DHT dht;
LoRaModem modem(Serial);

#define SECRET_APP_EUI "XXXXXXXXXXXXXXXXX"
#define SECRET_APP_KEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 42;
const byte hours = 00;

/* Change these values to set the current initial date */
const byte day = 13;
const byte month = 06;
const byte year = 18;
bool matched = false;


void setup()
{
  rtc.begin();

  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);
  rtc.setAlarmTime(00,50, 10);
  rtc.enableAlarm(rtc.MATCH_MMSS);
  rtc.attachInterrupt(alarmMatch);
  rtc.standbyMode();

  Serial.begin(9600);
  while (!Serial);
  // change this to your regional band (eg. US915, AS923, ...)
  if (!modem.begin(EU868)) {
    Serial.println("Failed to start module");
    while (1) {}
  };
  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  modem.minPollInterval(60);

  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");

  dht.setup(2); // data pin 2
}

void loop() {
  if (matched) {
    matched = false;
    int alarmMinutes = rtc.getMinutes();
    alarmMinutes += 1;
    if (alarmMinutes >= 60) {
      alarmMinutes -= 60;
    }
    delay(dht.getMinimumSamplingPeriod());

    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    int16_t temp = (int16_t) (temperature * 100);
    int16_t hum = (int16_t) (humidity * 100);
  

    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print(humidity, 1);
    Serial.print("\t\t");
    Serial.print(temperature, 1);
//  Serial.print("\t\t");
//  Serial.println(dht.toFahrenheit(temperature), 1);

    byte data[4];
    data[0] = (temp >> 8) & 0xFF;
    data[1] = temp;
    data[2] = (hum >> 8) & 0xFF;
    data[3] = hum;
  
    int err;
    modem.beginPacket();
    modem.write(data,sizeof(data));
    err = modem.endPacket(true);
    if (err > 0) {
      Serial.println("Message sent correctly!");
    } else {
      Serial.println("Error sending message :(");
      Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
      Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
    }
    delay(10000);
    if (!modem.available()) {
      Serial.println("No downlink message received at this time.");
      return;
    } 
    rtc.setAlarmTime(rtc.getHours(), alarmMinutes, rtc.getSeconds());
    rtc.standbyMode();    // Sleep until next alarm match
  }
}
void alarmMatch() {
  matched = true;
}

how much program storage and global variable memory are being used?
how much dynamic memory is left for local variables?
you could be running out of dynamic memory corrupting the system

loosing connection is a common problem I usually

  1. open the Control Panel > System > Device manager > Ports so I can watch the COM ports appear/disappear
  2. double cllick the reset button - after a few tries a COM port appears
  3. upload the Blink example code so I have a operational program

sometimes when compiling/uploading I have to double click the reset again
I have also found that the COM port can change after upload

I think the problem is with the RTC library or maybe the way I am using it ?

I have found the offending line. Whenever I comment the following from the non-working sketch

rtc.setAlarmTime(23,46, 10);

The sketch uploads just fine. When I uncomment it, it does not work. Windows no longer recognizes the arduino and in the device manager I can clearly see that the arduino is marked as an unknown device with an exclamation mark next to it.

Here is a test sketch I used to troubleshoot this

#include <RTCZero.h>

RTCZero rtc;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 45;
const byte hours = 23;

/* Change these values to set the current initial date */
const byte day = 15;
const byte month = 06;
const byte year = 18;

bool matched = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  rtc.begin();

  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);
  rtc.setAlarmTime(23,46, 10);  //when I comment this line the sketch works just fine
  rtc.enableAlarm(rtc.MATCH_SS);
  rtc.attachInterrupt(alarmMatch);
  rtc.standbyMode();
}

void loop() {
  // put your main code here, to run repeatedly:
  int alarmSeconds = rtc.getSeconds();
  int alarmMinutes = rtc.getMinutes();
  Serial.println(alarmMinutes);
  Serial.println(alarmSeconds);
  if(matched) {
     matched = false;
     Serial.println("test");
  }

}

void alarmMatch() {
  matched = true;
}

Can you see any error in the above sketch ?

Edit:I get the same issue if I run the sleepRTCalarm sketch provided as an example in the RTC Library examples. On the other hand, the sketch simpleRTCAlarm runs without issues but I never see anything printed in the serial console when the 10seconds counter elapses as per the sketch code

At this point I strongly believe that this should be reproducible by everybody. If not, could it be that I have a faulty module ?

Many thanks