Pulsante: Debounce e seriale

Ragazzi vorrei utilizzare arduino per connettere dei pulsanti che mi comandano dei relè in modo bistabile.
Ho realizzato uno sketch con anche debounce in questo modo:

//PULSANTE 0
  // read the state of the switch into a local variable:
  int reading0 = digitalRead(buttonPin0);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading0 != lastButtonState0) {
    // reset the debouncing timer
    lastDebounceTime0 = millis();
  }

  if ((millis() - lastDebounceTime0) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading0 != buttonState0) {
      buttonState0 = reading0;

      // only toggle the LED if the new button state is HIGH
      if (buttonState0 == HIGH) {
        ledState0 = !ledState0;
      }
    }
  }


  digitalWrite(ledPin0, ledState0);
  
  lastButtonState0 = reading0;

Fin qui tutto bene; il problema nasce quando lo stesso relè voglio comandarlo da seriale.
Il comando, con un piccolo protocollo per indirizzo e attivazione/disattivazione su 485 di per se funziona, ma inserito nello sketch con il debounce non funziona più perchè il loop blocca il comando.
Come potrei risolvere il problema di avere sia denbounce che comando da seriale?
Ciao, grazie.

Usando debounce hardware, come andrebbe sempre fatto :wink: ... una resistenza da 100 ohm ed un condensatore da 100n, e ti levi dalle scatole qualsiasi problema dato dalle routine di debounce software ...