const int buttonPin = D1; const int ledPin = D0; const int DEBOUNCE_TIME = 100; bool buttonPressed = 0; int debounceCount = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { bool buttonState = digitalRead(buttonPin); bool ledState = digitalRead(ledPin); if (buttonState && debounceCount < DEBOUNCE_TIME) debounceCount++; if (!buttonState) debounceCount--; if (debounceCount >= DEBOUNCE_TIME && !buttonPressed){ // button is officially pressed digitalWrite(ledPin, abs(ledState - 1)); buttonPressed = true; } else if (!buttonState && debounceCount < 0) { // button is officially not pressed debounceCount = 0; buttonPressed = false; } }