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
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?
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.