int input_pin = 2;
volatile int valume;
int total = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // หน่วยเป็น milliseconds
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, CHANGE);
DDRC = 0xFF;
setupTimer3();
Serial.begin(9600);
}
void loop(){
PORTC = (1<<PC7);
delay(500);
PORTC = (0<<PC7);
delay(500);
}
void state() {
if ((millis() - lastDebounceTime) > debounceDelay) {
valume = digitalRead(input_pin);
Serial.print("Input reading: ");
Serial.println(valume);
lastDebounceTime = millis();
}
}
ISR(TIMER3_COMPA_vect){
PORTC ^= (1<<PC0) | (1<<PC1) | (1<<PC2) | (1<<PC3);
}