Usage of timers t get longe time delay

Hi All,

I am new to Arduino. I am doing a project using atmega328 uC. In this i want to on the system based on the input from user. (for example 5hr). For this case i am planning to use TIMER1, but this timer is 16 bit and if i am setting the prescaler value as 16MHz/1024 also i can obtain the maximum interval as 4S only. So if i need to run for 5hr continuously i have to get 4500 timer overflow interrupts. As far as i think it is not an efficient way of handling. Can anyone guide me is there any other way of implementation to accomplish this behavior.

Thanks in advance

Arduino has millis() function (see. the Learning->Reference here on web), it provides milliseconds in 32bit unsigned format so you can simply use this to evaluate time difference up to 50 days, far more than you need.

So note - you never need to interfere with the timer in any way whatsoever.

You just read and take a note of the the millis() value (an unsigned long integer), then in your main loop(), read the current value, subtract from that current value the previously recorded value and if the result is longer than (not merely equal to) the number of milliseconds for which you were waiting, then your interval has expired.

Note that specific order of operations - it is critically important.

millis() is an unsigned long integer, which is also critically important.

Is there any sample codes available for this implementation?

Also i have tried the example code for RTC, which is available in the arduino1.7.7 compiler. During the compilation i am getting error for each and every sample. May i know why this is happened? To get rid of this issue do we need to install any supporting file? I am using atmega328 uC, which is not having the internal RTC.

Also i have tried as per the explanation given by PAUL__B. But if the multiplication value as 4000*15 the expected output for the value is 60000, it has to be stored in unsingned long int variable, but for my case i am getting the maximum value of the data type i.e.42949617, why?

Well, to implement an internal RTC with an ATmega you have to do certain things such as provide a RTC crystal. The RTC libraries however presume you are using an RTC module.

Now the maximum value of a "long" (32 bit) integer is in fact 4294967295 (don't know where you got that other number) which will count you 1193 hours or 49.71 days - seven weeks. But you can easily chain the counting to longer periods.

Of course, unless you post your code (by following the instructions), we clearly cannot even imagine why it would not compile. :roll_eyes:

available in the arduino1.7.7 compiler.

@prakesh2025

I can see that you are new to this forum, but you need to be aware that there are two organizations calling themselves "arduino". You can read about it here http://makezine.com/2015/03/19/massimo-banzi-fighting-for-arduino/

This forum is associated with arduino.cc, and the latest release of the ide available is 1.6.6. https://www.arduino.cc/en/Main/Software

1.7.7 is an IDE available from arduino.org. People on this board will not help you with problems related to the software from arduino.org .

Ensure you are using 1.6.6 from arduino.cc.

What you need to now about millis(0) based timers can be found in the "Blink Without Delay" example in the 02 Digital examples available with the ide. You can also take a look at Demonstration code for several things at the same time - Project Guidance - Arduino Forum which is a sticky post in the "Project Guidance" section of this forum which demonstrates multiple use of these timers.