ARDUINO MEGA + ETH - Custom Timer with more than 33 seconds never stops

Hi all

I’m having some troubles with my project. I’ve a greenhouse with some motors that can be remotely opened / closed or put in auto mode for rain detection and auto opening/closing.

On my board I’ve some custom times that archived in an array


BLYNK_WRITE(V34)
{
   windows_time[0] = param.asInt(); // example 

}

Then in my function :


 time1 = windows_time1[(slide1-1)];
  time2 = windows_time2[(slide2-1)];
 
 if ( opening_requested==1 && motor1_moving==0 && motor2_moving==0 && windows_open==0){            //IF OPENING REQUESTED AND MOTORS NOT MOVING
   
  
  Serial.println("Opening...");
  
  finecorsa1=digitalRead(finecorsa1C);
  finecorsa2=digitalRead(finecorsa2C);

  opening_start=millis();
  digitalWrite(arcata1C, HIGH);
  digitalWrite(arcata2C, HIGH);
  digitalWrite(arcata1A, LOW);
  digitalWrite(arcata2A, LOW);                        
                                                    
  
  motor1_moving=1;                                 // 1 -  > motor is opening
  motor2_moving=1;
  opening_request==0;
  
  }



  
  if( motor1_moving==1 && millis()-opening_start >= (time1*1000)){                                                 

            digitalWrite(arcata1A, HIGH);
            motor1_moving=0;
            Serial.println("--- Motor1 STOP");
                
        }
    
  if( motor2_moving==1 && millis()-opening_start >= (tempo2*1000)){                               
                       
                                    
            digitalWrite(arcata2A, HIGH);
            motor2_moving=0;
            Serial.println("--- Motor2 Stop");
                
        }
   
}

With that code, if I choose a time greater than 32 seconds, it never stops and I really don’t understand why. It works perfectly with shorter time.

Any suggestion?

Presumably because your windows_time variable is a 16-bit signed integer, with a maximum value range of -32768 to 32767 so 32000ms is the largest value it can store.

If you swap to a long integer it should solve the problem.

Pete.