// debounce GPIO input void getPin(gpio_num_t pin, bool* state, const unsigned long dur) { unsigned long cur = millis() / 1000; static unsigned long lastRead = 0; static unsigned long lastHigh = 0; static unsigned long lastLow = 0; static bool high = false; static bool low = false; if (cur == lastRead) { return; } else { lastRead = cur; } if (digitalRead(pin) == HIGH) { if (!high) { lastHigh = cur; high = true; } if ((cur - lastHigh) > dur) { *state = true; low = false; } } else { if (!low) { lastLow = cur; low = true; } if ((cur - lastLow) > dur) { *state = false; high = false; } } } // usage void loop() { getPin(PIN, &state, 5); if (state) { // do something } }