A little curiosity. A whole lot of making.Robotics, CNC & connected ideas
robodatech.
Arduino sample library

Button Debounce 20

Filter switch bounce with clean state transitions.

Open in compiler →
/*
  Roboda verified Arduino sample 0494
  Feature: Button Debounce
  Generated for online build, classroom practice, and hardware upload testing.
*/

const byte buttonPin = 2;
const byte ledPin = 13;
bool stableState = HIGH;
bool lastReading = HIGH;
unsigned long lastChange = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  bool reading = digitalRead(buttonPin);
  if (reading != lastReading) {
    lastChange = millis();
    lastReading = reading;
  }
  if (millis() - lastChange > 39) {
    stableState = reading;
  }
  digitalWrite(ledPin, stableState == LOW ? HIGH : LOW);
}

How it works

Practice this Arduino feature with a verified Roboda sketch.

  1. Read the constants at the top of the sketch.
  2. Wire the pins exactly as listed in the pin map.
  3. Upload or compile once before changing values.
  4. Change one value at a time and watch Serial Monitor or the output device.

Connections

PinConnect toNote
D2Button to GNDPressed reads LOW.
D13Built-in LEDShows the debounced button state.

Good ideas start here.

Robotics lessons, build notes and useful updates. Subscribe by entering your email.