You can use the timer interrupts. For example:
#define F_CPU 16000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>
...
ISR(TIMER1_COMPA_vect)
{
//do something every seconds when interrupt is raised.
}
void setup()
{
/*the following code will setup timer register to generate interrupt
when 15624 is reached, at a clock frequency of 16 MHz and 1024 prescaler. This means that every second an interrupt is generated
*/
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 15624;
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
sei(); // enable global interrupts
}
The code works on Arduino Uno. You should calculate the value of OCR1A for your frequency.