Arduino Nano Every/Atmega4809 Watch Dog

Hi,
I have a pcb made for using the sim800l and arduino nano every.
I tried using delays for the arduino to start communicating with the sim module because it would not work otherwise. I found out that i need to wait/reset the arduino in order for it to work.
Because the pcb was already made i added a 10 second delay.
When i turn it on, most times it does not work.
I tried to use the watchdog with the Adafruit Watchdog library but the example code would not compile for the Arduino Nano Every.
The delay() function does not wait the set amount of time. If i set it to 5 seconds for example, it sometimes waits 3 seconds sometimes 6 sometimes 4.
The bigger the value the more variation there is.
How can i make this work?
Is there a better way of doing this?
Here is the code:

#include <SoftwareSerial.h>
#include <avr/wdt.h>
SoftwareSerial sim(10, 11);
String broj1 = "MY NUMBER GOES HERE";
String fault = "Imam greska Vlez 1";
String pozar = "Ima pozar Vlez 1";
String fault2 = "Imam greska Vlez 2";
String pozar2 = "Ima pozar Vlez 2";
String faultkraj = "Vo red sum. Vlez 1 ";
String pozarkraj = "Nema pozar Vlez 1";
String faultkraj2 = "Vo red sum. Vlez 2 ";
String pozarkraj2 = "Nema pozar Vlez 2";
bool imasignal3 = 0;
bool imasignal4 = 0;
bool imasignal5 = 0;
bool imasignal6 = 0;
bool simstart = false;
unsigned long time_now = 0;
void setup() {
  for (int i = 3; i <= 6; i++)
  {
    pinMode(i, INPUT);
  }
  time_now = millis();
  while (millis() < time_now + 10000) {
    delay(1);
  }
  Serial.begin(9600);
  Serial.println("Zapocnata programa.");
}


void loop() {
  if (simstart == false ) {
    delay(1000);
    sim.begin(9600);
    delay(100);
    sim.print("AT");
    delay(100);
    simstart = true;
  }
  //DOBIEN SIGNAL
  if (digitalRead(3) == 1) {
    izvestifault(1, broj1);
  }
  if (digitalRead(4) == 1) {
    izvestifault(2, broj1);
  }
  if (digitalRead(5) == 1) {
    izvestipozar(1, broj1);
  }
  if (digitalRead(6) == 1 ) {
    izvestipozar(2, broj1);
  }

  //KRAJ NA SIGNAL
  if (digitalRead(3) == 0) {
    izvestifaultkraj(1, broj1);
  }
  if (digitalRead(4) == 0) {
    izvestifaultkraj(2, broj1);
  }
  if (digitalRead(5) == 0) {
    imasignal5 = 0;
  }
  if (digitalRead(6) == 0) {
    imasignal6 = 0;
  }
}

/////////////////////////////////////////////////////////////
void izvestifault(int vlez, String broj)
{
  if (vlez == 1) {
    if (imasignal3 == 0 ) {
      sim.print("AT");
      delay(200);
      Serial.println("Registriran signal za Fault, Vlez 1");
      Serial.println("Pocnuva dojavuvanje za Fault, Vlez 1");
      Serial.print("Pocnato prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za Fault, Vlez 1");
      poraka(broj, fault);
      delay(1000);
      Serial.print("Zavrseno prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za Fault, Vlez 1");
      Serial.println("Zavrseno dojavuvanje za Fault, Vlez 1");

    }
    imasignal3 = 1;
  }
  else if (vlez == 2) {
    if (imasignal4 == 0 ) {
      sim.print("AT");
      delay(200);
      Serial.println("Registriran signal za Fault, Vlez 2");
      Serial.println("Pocnuva dojavuvanje za Fault, Vlez 2");
      Serial.print("Pocnato prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za Fault, Vlez 2");
      poraka(broj, fault2);
      delay(1000);
      Serial.print("Zavrseno prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za Fault, Vlez 2");
      Serial.println("Zavrseno dojavuvanje za Fault, Vlez 2");

    }
    imasignal4 = 1;
  }
}


void izvestipozar(int vlez, String broj)
{
  if (vlez == 1) {
    if (imasignal5 == 0 ) {
      sim.print("AT");
      delay(200);
      Serial.println("Registriran signal za Pozar, Vlez 1");
      Serial.println("Pocnuva dojavuvanje za Pozar, Vlez 1");
      Serial.print("Pocnato prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za Pozar, Vlez 1");
      povik(broj);
      delay(1000);
      Serial.print("Zavrseno prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za Pozar, Vlez 1");
      Serial.println("Zavrseno dojavuvanje za Pozar, Vlez 1");

    }
    imasignal5 = 1;
  }
  else if (vlez == 2) {
    if (imasignal6 == 0 ) {
      sim.print("AT");
      delay(200);
      Serial.println("Registriran signal za Pozar, Vlez 2");
      Serial.println("Pocnuva dojavuvanje za Pozar, Vlez 2");
      Serial.print("Pocnato prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za Pozar, Vlez 2");
      povik(broj);
      delay(1000);
      Serial.print("Zavrseno prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za Pozar, Vlez 2");
      Serial.println("Zavrseno dojavuvanje za Pozar, Vlez 2");

    }
    imasignal6 = 1;
  }
}


void izvestifaultkraj(int vlez, String broj)
{
  if (vlez == 1) {
    if (imasignal3 == 1 ) {
      sim.print("AT");
      delay(200);
      Serial.print("Izguben signal za Fault, Vlez 1");
      Serial.println("Pocnuva dojavuvanje za kraj na Fault, Vlez 1");
      Serial.print("Pocnato prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Fault, Vlez 1");
      poraka(broj, faultkraj);
      delay(1000);
      Serial.print("Zavrseno prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Fault, Vlez 1");
      Serial.println("Zavrseno dojavuvanje za kraj na Fault, Vlez 1");

    }
    imasignal3 = 0;
  }
  else if (vlez == 2) {
    if (imasignal4 == 1 ) {
      sim.print("AT");
      delay(200);
      Serial.print("Izguben signal za Fault, Vlez 2");
      Serial.println("Pocnuva dojavuvanje za kraj na Fault, Vlez 2");
      Serial.print("Pocnato prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Fault, Vlez 2");
      poraka(broj, faultkraj2);
      delay(1000);
      Serial.print("Zavrseno prakjanje poraka na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Fault, Vlez 2");
      Serial.println("Zavrseno dojavuvanje za kraj na Fault, Vlez 2");
    }
    imasignal4 = 0;
  }
}



void izvestipozarkraj(int vlez, String broj)
{
  if (vlez == 1) {
    if (imasignal5 == 1 ) {
      sim.print("AT");
      delay(100);
      Serial.print("Izguben signal za Pozar, Vlez 1");
      Serial.println("Pocnuva dojavuvanje za kraj na Pozar, Vlez 1");
      Serial.print("Pocnato prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Pozar, Vlez 1");
      povik(broj);
      delay(1000);
      Serial.print("Zavrseno prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Pozar, Vlez 1");
      Serial.println("Zavrseno dojavuvanje za kraj na Pozar, Vlez 1");
    }
    imasignal5 = 0;
  }
  else if (vlez == 2) {
    if (imasignal6 == 1 ) {
      sim.print("AT");
      delay(100);
      Serial.print("Izguben signal za Pozar, Vlez 2");
      Serial.println("Pocnuva dojavuvanje za kraj na Pozar, Vlez 2");
      Serial.print("Pocnato prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Pozar, Vlez 2");
      povik(broj);
      delay(1000);
      Serial.print("Zavrseno prakjanje povik na broj ");
      Serial.print(broj);
      Serial.println(" za kraj na Pozar, Vlez 2");
      Serial.println("Zavrseno dojavuvanje za kraj na Pozar, Vlez 2");
    }
    imasignal6 = 0;
  }
}

void povik(String broj)
{
  sim.print("AT");
  delay(100);
  sim.print(F("ATD"));
  sim.print(broj);
  sim.print(F(";\r\n"));
}

/////////////////////////////////////////////////////////////

void poraka(String broj, String tekst) {
  sim.print("AT+CMGF=1");
  delay(200);
  sim.print("AT+CMGS=\"" + broj + "\"\r");
  sim.print(tekst);
  delay(200);
  sim.print((char)26);
}



/////////////////////////////////////////////////////////////

The code is certainly not the best but it should work.
Also: The antenna should not be a problem because i stretched the one that came with it from (maybe)2cm to 6-7cm.
I am using an authentic arduino nano every.
The if(simstart == false) statement is there because it worked worse when the sim.begin() function was in the setup.(after the delays of course)

I think you have to proceed like this:

  1. Develop a test function to see if the connection to the sim800l is alive. Maybe simply issue sim.print("AT") and test if there is a response, but make sure that suc a command works if there is a call in progress.
  2. Set the watchdog timer to expect a reset within X ms. (timeout up to 8 seconds).
  3. Periodically perform the test function (within its timeout period) and reset the watch dog timer is all is OK.

The watchdog timer is described here: http://ww1.microchip.com/downloads/en/DeviceDoc/megaAVR0-series-Family-Data-Sheet-DS40002015B.pdf Chapter #18. The code you found for a ATMega328p processor will not work for an ATMega4809.

I have an "AT" at command 500ms after sim.begin().
And thank you for the link. i opened microchips atmega4809 page and didnt find anything about the watchdog. Just a special type of it called a windowed watch dog.
Thanks for the help!

HobbyMen:
I have an "AT" at command 500ms after sim.begin().
. . .

But to make it effective, you have to test that it returned a message , I guess something like this but maybe you have to look for "OK" in the message :

sim.print("AT");
. . .
if ( sim.available() > 0 ) {
  // The command got a response so we set the flag true
      simOK = true ;
  // now clean up
  while ( sim.available() > 0 ) {
     char dummy = sim.read()  ;
  }
}
else simOK = false ;

Ok. I will implement that.
I cant find anything online about the commands and functions needed to start the watch dog for the nano every.
Are they the same as other arduinos?
Can you send me a piece of sample code?

wdt_enable(WDT_PERIOD_8KCLK_gc);
to setup and
wdt_reset();
to feed the dog

from iom4809.h:
typedef enum WDT_PERIOD_enum
{
WDT_PERIOD_OFF_gc = (0x00<<0), /* Off /
WDT_PERIOD_8CLK_gc = (0x01<<0), /
8 cycles (8ms) /
WDT_PERIOD_16CLK_gc = (0x02<<0), /
16 cycles (16ms) /
WDT_PERIOD_32CLK_gc = (0x03<<0), /
32 cycles (32ms) /
WDT_PERIOD_64CLK_gc = (0x04<<0), /
64 cycles (64ms) /
WDT_PERIOD_128CLK_gc = (0x05<<0), /
128 cycles (0.128s) /
WDT_PERIOD_256CLK_gc = (0x06<<0), /
256 cycles (0.256s) /
WDT_PERIOD_512CLK_gc = (0x07<<0), /
512 cycles (0.512s) /
WDT_PERIOD_1KCLK_gc = (0x08<<0), /
1K cycles (1.0s) /
WDT_PERIOD_2KCLK_gc = (0x09<<0), /
2K cycles (2.0s) /
WDT_PERIOD_4KCLK_gc = (0x0A<<0), /
4K cycles (4.1s) /
WDT_PERIOD_8KCLK_gc = (0x0B<<0), /
8K cycles (8.2s) */
} WDT_PERIOD_t;

Thanks. I found on the datasheet for the Atmega 4809 that i can use the timing adresses so i just typed in 0x1 for an 8ms timer.
I now have another problem.
It does not work with EEPROM.
I have it set up so that when i turn it on, it waits a bit and then resets. After the reset it is supposed to ignore the watchdog.
The problem is that it does not even enter the IF statement.
When i run the eeprom read example it gives me every value 0 except index 0 at 255.
I chose index 1 to use and it does not work. if i run the example it gives me a value of 0 for index 1 and if i run my sketch it gives me a value of 255. what am i doing wrong?

I changed it a bit but it still does not work. The Serial Monitor results are different but it still does not work.
Edit: I added a 500ms delay before the watchdog and some other changes and now it prints everything. Thanks for all the help!