# Timer library

I would like to use a timer to run a motor for 15 seconds, every hour, and to do this for 19 days. Does anyone have a recommendation on a simple timer library? I tried RBD::Timer, but it does not work, and it appears not too many people use this timer library.

Thanks!
Ted

You don't need any library. You need the millis function and some basic elementary school math. The coding involved isn't hard, all you need to be able to understand is that when you subtract one time from another time you get the difference between them.

Is accuracy is important?
If not use the BWD BlinkWithoutDelay technique.

However, recommend you get a real time clock like DS3231.

.

accuracy is not that important.

I looked at the milles() function. It looks like I can use modulo (%) to divide millis() by 1 hour, and when the result is zero, I have the hour mark and can turn on the motor. Anytime modulo is 15000, I can turn off the motor.

https://www.arduino.cc/en/Reference/Modulo

Question, does that sound like a good strategy? any issues with modulo working with millis(); there is a note that it does not work with floats?

Is it sufficient to have an if-then statement of 'if time = 1 hour, do this' and 'if time = 15 seconds from hour do that'? or should I use greater than functions?

I understand the 15Mhz clock will see several cycles/loops when modulo = zero. so '=' should be adequate.

I will have to give it a try tonight.

Question, does that sound like a good strategy?

No.

Think about how YOU would turn the motor on and off on a regular schedule. Suppose that you first turn it on at 3:17:32 AM. It needs to run for 28 minutes and 12 seconds.

When do you turn it off?

unsigned long pumpTime = 0; // the time when the pump was turned on of off
unsigned long timeOn = /* however long the pump should be on */;
unsigned long timeOff = /* however long the pump should be off */;

unsigned long now = millis();
if(pumpRunning)
{
// The pump is running. Is it time to turn it off?
if(now - pumpTime >= timeOn)
{
// It is time to turn the pump off

pumpTime = now;
pumpRunning = false;
}
}
else
{
// The pump is not running. Is it time to turn it on?
if(now - pumpTime >= timeOff)
{
// It is time to turn the pump on

pumpTime = now;
pumpRunning = true;
}
}

Of course, you need to add the code to actually turn the pump on or off.

Thanks PaulS!!

Your approach compares the difference in two times to turn the motor on or off. I see how that will work.

I am interested in why the modulo approach would not be a good approach. Can you elaborate? Is there an issue with data types or processing time for operations?

Thanks!
Ted

Every 49 days millis hits the max value of an unsigned long and rolls over to zero. With PaulS code that is no concern. With the modulo approach you'd have one interval there that was off. You would also have to deal with the worry that you don't catch it right exactly at the right millisecond or that millis skips the number you're looking for with the modulo. Millis skips a number every so often to make up for the fact that it counts 1024us per ms.