#define PERIOD 500 // control loop period [ms] - max 4194 ms !!!
boolean state = LOW;
void setup() {
cli(); // disable interrupts
pinMode(0, OUTPUT);
digitalWrite(0, LOW);
// Configure Timer 4 to generate interrupt each PERIOD
// (CTC mode; 1024 prescaler; enable interrupt on compare match)
TCCR4A = 0;
TCCR4B = (1 << WGM42) | (1 << CS42) | (1 << CS40);
TCNT4 = 0;
// output compare register
OCR4A = (16000000 / 1024) * PERIOD / 1000;
// Output Compare A Match Interrupt Enable
TIMSK4 = (1 << OCIE4A);
sei(); // enable interrupts
}
void loop() {
// nothing to do
}
// Controll loop goes here
ISR(TIMER4_COMPA_vect)
{
state = !state;
digitalWrite(0, state);
}