int input_pin = 2;
volatile int value;
void setupTimer3() {
noInterrupts();
// Clear registers
TCCR3A = 0;
TCCR3B = 0;
TCNT3 = 0;
// 1 Hz (16000000 / ((15624+1)*1024))
OCR3A = 15624;
// CTC
TCCR3B |= (1 << WGM32);
// Prescaler 1024
TCCR3B |= (1 << CS32) | (1 << CS30);
// Output Compare Match A Interrupt Enable
TIMSK3 |= (1 << OCIE3A);
interrupts();
}
void setup() {
pinMode(input_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(input_pin), state, CHANGE);
DDRC = 0xFF;
setupTimer3();
Serial.begin(9600);
}
void loop() {
PORTC = (1 << PC4) | (1 << PC5) | (1 << PC6) | (1 << PC7);
delay(1000);
PORTC = (0 << PC4) | (0 << PC5) | (0 << PC6) | (0 << PC7);
delay(1000);
}
void state() {
value = digitalRead(input_pin);
Serial.print("Input reading: ");
Serial.println(value);
}
ISR(TIMER3_COMPA_vect) {
PORTC ^= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3);
}