Arduino Resets/Blocks itself.

Hi there Arduino Comunity, I am actually using Arduino Mega2560 for temporizing lights and sensing temp/humidity (DHT22). For the temporizer I use the millis function, printing on Serial every 1 min so I can watch it in case there is a reset or to verify if the reset was executed in time. I tested the program with low times (Example: 30min ON and 30min OFF with a reset at 60min(Ton+Toff)) and it works wonderfully . The problem is that on long times (Example: 12hs ON/ 12hs OFF) it sometimes reset itself, or doesnt print the time on Serial. I thought it would be some memory problems but the program itself is very lightweight. I’m gonna post some images and the code to show as examples:

#include <DHT.h>
#include <LiquidCrystal.h>



//Librerias


//   Definicion de Variables

//Reseteo y temporizador
int RESETPIN=7;
int control=9;
unsigned long time;
int i=1;

int TON=12*3600; // TIEMPO DE ENCENDIDO EN SEGUNDOS 14 HORAS=50400 SEGUNDOS
int TOFF=12*3600; //TIEMPO DE APAGADO EN SEGUNDOS 10 HORAS=36000 SEGUNDOS
int RESETTIME=TOFF+TON; //TIEMPO DE RESETEO EN SEGUNDOS 24HS=86400 SEGUNDOS

// DHT y Pantalla LCD
int columnas=16;
int filas=2;
LiquidCrystal lcd(12,11,5,4,3,2);
#define DHTTYPE DHT22
#define DHTPIN 8
DHT dht(DHTPIN, DHTTYPE);


void setup() {
  //Seteo Temporizador y Reset
  
digitalWrite(RESETPIN, HIGH);
Serial.begin(57600);
Serial.println("RESETED");
pinMode(RESETPIN, OUTPUT);
  
//Seteo LCD y DHT
lcd.begin(columnas,filas);
  dht.begin();
    // Print a message to the LCD.
  lcd.print("Temp:  Humidity:");
}

void loop() {
  //Pasaje de millis a Segundos.
  time=(millis())/1000;
  
  // Tiempo de control
  
  if(time<TON){
    digitalWrite(control,HIGH);
  }
    else if(time>TON){
    digitalWrite(control,LOW);
  }
  else{
    delay(50);
  
}
// Mostrar tiempo transcurrido desde el ultimo reseteo en SEGUNDOS.
  if(time>=i*60){
  Serial.print("Time: ");
  Serial.println(time);
  i=i+1;
  }
  
  
//Muestro en el LCD Temperatura y Humedad
 delay(25);
  // read humidity
  float h = dht.readHumidity();
  //read temperature in Fahrenheit
  float f = dht.readTemperature(true);
  float  t=(f-32)*5/9;

  if (isnan(h) || isnan(f))
  {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(0, 1);
  lcd.print(t);
  lcd.setCursor(7,1);
  lcd.print(h);
        

//Tiempo de RESETEO
  if (time>RESETTIME){
    digitalWrite(RESETPIN,LOW);
  delay(50);
  }
}

PD: There are no problems on the analogic part (relays, transistors, etc). The main problem is Arduino reseting itself. Power supply is via USB of a notebook.

PD2: I set the reset pin to high at the beggining of the script so it doesnt reset loop. After the reset time is met, Arduino will reset itself by setting a LOW on the reset pin(via a cable from pin 9 to RESET).

PD3: In my opinion the problem might be of memory, but due to the low data printing and millis is up for just a day at max I’m really out of answers for this problem. (I understand that millis can run 5days max due to memory problems)

This is my first post on the forum as an “active member”, sorry if this is out of place, i managed myself to read the rules before posting this. Sorry if there are some grammar mistakes as english isn’t my native tongue.

Waiting for an answer I salute all of you.

time=(millis())/1000;

WHY ? ?

Do all math with millis.

(deleted)

Solved, did all the math with millis() and used long for times since the numbers were too big. Thanks guys.

(deleted)

:sweat_smile: Ups, haha thanks spycatcher2k ;) .