Skip to content
ThinkNMake — Don't just think. Make it.

Coding · community

Code & Sketches

Arduino sketches, Python scripts and the logic behind them.

7
Members
1
Builds

Log in to share what you’re building.

1 build

Sort
  • Sneha Rai

    Sneha RaiMember

    @sneha_codes

    Debouncing buttons without delay(), so the rest of the loop keeps running. This replaced about thirty lines of tangled flags in my robot's control code. #code #arduino
    arduino
    // Non-blocking debounce — no delay(), the loop stays responsive.
    class Button {
      uint8_t pin; bool stable = HIGH, last = HIGH; unsigned long changedAt = 0;
    public:
      explicit Button(uint8_t p) : pin(p) { pinMode(p, INPUT_PULLUP); }
    
      // Returns true once per physical press.
      bool pressed() {
        bool now = digitalRead(pin);
        if (now != last) { last = now; changedAt = millis(); }
        if (millis() - changedAt > 25 && now != stable) {
          stable = now;
          return stable == LOW;       // active-low with INPUT_PULLUP
        }
        return false;
      }
    };