Finally found a solution that polls in the ISR of Timer2, wchich fires in a ~20ms rate. Seems to work fine and the loop stays free for other stuff.
volatile int pin_LED = 13;
volatile int pin_button = 2;
volatile boolean oldSwitchState = HIGH;
volatile boolean newSwitchState1 = HIGH;
volatile boolean newSwitchState2 = HIGH;
volatile boolean newSwitchState3 = HIGH;
volatile boolean LEDstatus = LOW;
void setup()
{
//***Timer2***
pinMode(13, OUTPUT);
TCCR2A = 0;
TCCR2B = 0;
TCCR2A |= (1<<WGM21); //CTC Mode (Timer zählt bis auf OCR2A und wird dann automatisch 0 gesetzt)
TIMSK2 |= (1<<OCIE2A); //enable OCR1A Interrupt
TCCR2B |= (1<<CS22) | (1<<CS21) | (1<<CS20); //prescaler 1024 (Alle 1024 CPU Takte zählt der Timer einen Tick weiter)
OCR2A = 200; // OCR1A = f_clock / (prescaler * f_target) - 1 (Wert bis zu dem gezählt wird)
//***LED***
pinMode(pin_LED, OUTPUT);
digitalWrite(pin_LED, LOW);
//***Button***
pinMode(pin_button, INPUT_PULLUP);
}
ISR(TIMER2_COMPA_vect)
{
newSwitchState1 = digitalRead(pin_button);
delay(1);
newSwitchState2 = digitalRead(pin_button);
delay(1);
newSwitchState3 = digitalRead(pin_button);
if (newSwitchState1 == newSwitchState2 && newSwitchState1 == newSwitchState3) {
if ( newSwitchState1 != oldSwitchState ) { //hatt sich der zustand des schalter geändert?
if ( newSwitchState1 == LOW ) { //ist der schalter gedrückt (soll nur beim drücken nicht beim auslassen reagieren
if ( LEDstatus == LOW ) {
digitalWrite(pin_LED, HIGH);
LEDstatus = HIGH;
}
else {
digitalWrite(pin_LED, LOW);
LEDstatus = LOW;
}
}
oldSwitchState = newSwitchState1;
}
}
}
void loop()
{
}