2 week timer delay

Hey there, I'm trying to make a 2-week timer delay in my code (it turn on a pump for a few seconds). The problem is that I think that that delay will be counted as another delay (like an led is turned on for 2 seconds) and that will have a setback on other input/outputs.

Is there a way to make it a timer to itself without reading other delays?

Any feedback would be much appreciated!! :slight_smile:

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thank you, the first part really helped :).

Hello
Thats all what the sketch shall do ?
if YES, than you can code it using two delay()´s simply.
Have a nice day and enjoy coding in C++.

I am glad it helped

Come back with any code that you have problems with

Use Real Time Clock, built in, if your board has one, or as an extra module. 2 weeks is too long of a period to use millis, it will be quite inaccurate, and you seem to want accuracy from your OP

No, it has multiple inputs and outputs. Which makes me think I need more delays for this to not get counted with other delays, right?

Hello
Yes, in this case you have to design a time manager to process the desired time jobs.
Have a nice day and enjoy coding in C++.

It would help if you provided details of what you are doing and how accurate the timing, particularly the 2 week timer, needs to be

I would tend to agree that an RTC would be the best solution for the 2 week period, but using that does not preclude the use of millis() for timing shorter periods during the long period

I'm doing a greenhouse and this is for a fertilizer pump, so it doesn't need any sensors, and the timing doesn't need to be 100% accurate. It just needs to have a long period of time where it doesn't work and after that time period is over it turns on the pump for a few seconds.

Alright, thank you!!

Is the time of the day critical? because your hand rolled clock may drift and then your pump would operate at night instead of day for example

no, really it can work whenever.

Then using delay() s would probably be enough

See what you make of this

unsigned long currentTime;
unsigned long longPeriodStartTime;
unsigned long shortPeriodStartTime;
unsigned long longPeriod = 10000; //10 seconds
unsigned long shortPeriod = 1000; //1 second
boolean pumping = false;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  currentTime = millis();
  if (currentTime - longPeriodStartTime >= longPeriod)
  {
    pumping = !pumping;
    longPeriodStartTime = currentTime;
  }
  if (pumping)
  {
    if (currentTime - shortPeriodStartTime >= shortPeriod)
    {
      Serial.println("pumping");
    }
  }
}