Here is a slightly modified program I made to easily set up timed interrupts. I tested its accuracy and it works consistently down to the microsecond.
The interrupt gets called every 1 second and adds 1 to 'seconds'. Simply check if 'seconds' is 60 or whatever, if it is, then execute your code. This program toggles an LED.
void setup() {
int frequency = 1; // in hz
//Interupt Service Routine and timer setup
noInterrupts();// kill interrupts until everybody is set up
//We use Timer 1 b/c it's the only 16 bit timer
TCCR1A = B00000000;//Register A all 0's since we're not toggling any pins
// TCCR1B clock prescalers
// 0 0 1 clkI/O /1 (No prescaling)
// 0 1 0 clkI/O /8 (From prescaler)
// 0 1 1 clkI/O /64 (From prescaler)
// 1 0 0 clkI/O /256 (From prescaler)
// 1 0 1 clkI/O /1024 (From prescaler)
TCCR1B = B00001100;//bit 3 set for CTC mode, will call interrupt on counter match, bit 2 set to divide clock by 256, so 16MHz/256=62.5KHz
TIMSK1 = B00000010;//bit 1 set to call the interrupt on an OCR1A match
OCR1A = (unsigned long)((62500UL / frequency) - 1UL);//our clock runs at 62.5kHz, which is 1/62.5kHz = 16us
interrupts();//restart interrupts
Serial.begin(115200);
pinMode(13, OUTPUT);
}
volatile int seconds = 0; //make it volatile because it is used inside the interrupt
void loop() {
}
ISR(TIMER1_COMPA_vect){ //Interrupt Service Routine, Timer/Counter1 Compare Match A
seconds++;
if(seconds >= 1) { //set to however many seconds you want
Serial.println(micros()); // This code is what happens
seconds = 0; // after 'x' seconds
digitalWrite(13, !digitalRead(13)); //
}
}