Time limit on delay()?

I am new to arduino and also pressed for time on a project. I am building an automatic plant watering thingy that uses a 12vdc valve, a motor controller and an attiny85. Basically I want the arduino to wait for 7 days (604,800,000 milliseconds) and then open the valve for a few seconds and then cycle back again. Basically this is the basic Blink program extended to a 7 day delay). I am going to be away for a couple of weeks and don't want my plants to die. Yes I know this is not an elegant solution to the problem and would love to spend time integrating a real time clock and a program that waters on specific days and times, but again I am pressed for time and this is what I am going with.

You can use millis() which can handle up to 50 days with overflowing and even then its still possible
I think you need a simple blink without delay sketch but extended to 7 days

winner10920:
You can use millis() which can handle up to 50 days with overflowing

No, you can use millis() indefinitely.

Assuming the math is correct (for number of milliseconds), this code will toggle the LED on pin 13 every 7 days and handle the rollover event.

#define interval 604800000UL
unsigned long waitUntil=0;

void setup() {
  pinMode(13, OUTPUT);
  waitUntil += interval;
}

void loop() {
  if ((long)(millis() - waitUntil) >= 0) {
    // It's time to do something!
    digitalWrite(13, !digitalRead(13));
    waitUntil += interval;  // wait another interval cycle
  }
}

Yes, there is a time limit on delay(). The function takes an unsigned long, so the longest you can delay is 4,294,967,295 milliseconds. That's 1,193 hours or 49.7 days.

Thanks all for the help. Thanks James C4S for the code that is most appreciated and I think I understand it :slight_smile: I am building two different systems, the one mentioned and another for my outdoor plants using a 12vdc waterm pump from an RV. I will try both solutions and see which one works better.

The code I gave is the alternative to using delay().

Instead of (effectively) stopping everything and waiting for a specified amount of time, the if-statement keeps getting checked. This has the benefit of letting you do other things while waiting for that time to elapse.

Id suggest do it without delay, then you can build upon it later to include those other funvtions
with delay it wont be as easy

Thank you James for the code.
In my case , motor did not stopped for few secs.
To test i changed the 7 days to 1min, So basically whats happening is ... it on and off every 1 min ....thats good . But , it should stop for few secs after it on and then stop........how would you suggest to do that?

You just change what it does inside the if statement.

int onTime = 2000; //On time in ms

void loop() {
 if ((long)(millis() - waitUntil) >= 0) {
   // It's time to do something!
   digitalWrite(13, HIGH);
   delay(onTime);
   digitalWrite(13, LOW);
   waitUntil += interval;  // wait another interval

Assuming high is on.

SamIAm93:

void loop() {

if ((long)(millis() - waitUntil) >= 0) {

That line is slightly wrong. millis() returns an unsigned long value (and all of the variables associated with it should also be unsigned long). If you convert millis() to a signed long you may get strange results as the higher values will be treated as negative.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R