30s Timer Interrupt on the Mega2560

I am trying to make an interrupt that will run every 30s. Is this even possible, if so how?

I see from other posts that to setup the correct registers (assuming 16 bit timer):
16,000,000 / 1,024 (max pre-scalar?) = 15,625
15,625 / 65,536 = 0.23 Hz = 4.2s

Thank you.

Why? Blink-without-delay is a perfect choice for runs-every-30-seconds.

Lets say your MCU has a 16 bit timer that counts in milli seconds, how many milliseconds will the timer count up too? With a 16 bit int that would be a lot less then 30 seconds. So what to do?

Well lets say I use the MsTimer2 - Arduino Reference to generate a timer pulse once a second.

volatile int timerPulseCount;

void myTimerISR()
{
timerPulseCount++;
}

void setup()
{
If (timerPulseCount ==30 )
{
do some things
timerPulseCount = 0;
}
}

Yes, you can't get to 1/30th Hz with a 16 MHz clock. You can slow down the processor clock to run at 8 MHz to get about 8.4 seconds or run at 1 MHz to get about 67 seconds per cycle. I don't think millis() or delay() will work properly at that rate.

You could set up an interrupt every 3 seconds and do nothing for 9 out of 10 interrupts.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.