-2

I am trying to make an incubator. I want it to run some intake and exhaust fans every couple of minutes. For that I decided to use the millis() function, but I also came to know its 50 day limit. Will the code that I have written below work despite that 50 day limit?

#define in_out_fan 5
unsigned long interval=1000*30;
unsigned long previousMillis=0;
void setup(void){
    Serial.begin(9600);
    pinMode(5, OUTPUT);}
void loop(void){
  unsigned long currentMillis = millis();
  if (currentMillis<previousMillis){
    previousMillis=0;    
  }  
  if ((currentMillis - previousMillis) >= interval) {
      digitalWrite(in_out_fan, HIGH);
      delay(15000);
      digitalWrite(in_out_fan, LOW);      
      previousMillis = currentMillis;
      Serial.println("cycle completed");}
}
  • 2
    you are good with currentMillis - previousMillis. there is nothing else to handle. the difference is always right. https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover – Juraj Apr 07 '23 at 18:22

2 Answers2

0

As long as your interval is smaller than 49 days, you just have to do it right:

static unsigned long prevMillis;
if (millis() - prevMillis >= interval) // handles rollover properly
{
    prevMillis += interval; // will rollover as well
    // do here what has to be done once per interval
}

If you need an interval bigger than 49 days you can use a convenient larger scale than millis (make sure your power supply never interrupts).

datafiddler
  • 1,755
  • 3
  • 17
  • 30
-1

You can use millis() to count one day (or maybe one week) and at that point of time reset the board programmatically.

Nino
  • 702
  • 2
  • 6
  • 12