int input_pin = 2;
volatile int count = 0;
volatile bool buttonstate = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100;
unsigned long previousMillis = 0;
void setupTimer3(){
noInterrupts();
TCCR3A = 0;
TCCR3B = 0;
TCNT3 = 0;
OCR3A = 15624*2;
TCCR3B |= (1 << WGM32);
TCCR3B |= (1 << CS32) | (1 << CS30);
TIMSK3 |= (1 << OCIE3A);
interrupts();
}
void setupTimer1(){
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 15624;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
interrupts();
}
void setup(){
pinMode(input_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(input_pin), state, FALLING);
DDRC = 0xFF;
setupTimer3();
setupTimer1();
Serial.begin(9600);
}
void loop(){
PORTC = (1<<PC7);
delay(200);
PORTC = (0<<PC7);
delay(200);
}
void state() {
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonstate = !buttonstate;
count = buttonstate + count;
Serial.print("Button State: ");
Serial.print(buttonstate);
Serial.print(" | Press Count: ");
Serial.println(count);
lastDebounceTime = millis();
}
}
ISR(TIMER3_COMPA_vect){
if (count%3 != 0){
PORTC ^= (1<<PC0) | (1<<PC1) | (1<<PC2);
}
}
ISR(TIMER1_COMPA_vect){
if (count%3 == 2){
PORTC ^= (1<<PC3) | (1<<PC4) | (1<<PC5) | (1<<PC6) ;
}
}