#include "util/atomic.h" unsigned long mymillis = 0; int main (void) { const unsigned long eventInterval = 1000; unsigned long previousTime = 0; DDRB |= (1 << PB5); TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt OCR1A = 7999; // Set CTC compare value to 1 Hz at 8 MHz AVR clock , with a prescaler of 256 TCCR1B |= (1 << CS00); // Start timer at F_CPU /256 sei (); // Enable global interrupts while (1) { // Updates frequently unsigned long currentTime; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) currentTime = mymillis; // This is the event if (currentTime - previousTime >= eventInterval) { // Event code PINB = (1 << PB5); // toggle // Update the timing for the next time around previousTime = currentTime; } } } ISR (TIMER1_COMPA_vect) { mymillis++; }