Arduino / Next step

Make one button press count once

Filter contact bounce with a small non-blocking state machine, then toggle the built-in LED on a stable press.

By Harry Avraamides10 October 202635 min activity

Before you start
Complete Read a button and understand the basic millis() timing pattern.

You will leave with
A button toggles an LED once per deliberate press without using delay().

A button can work perfectly when it controls a light while held, yet behave badly when each press is supposed to change a setting. Physical contacts may make and break repeatedly around the transition. This tutorial accepts a change only after the input has stayed consistent for a short interval. It is an untested educational procedure for the Arduino UNO R3.

Reuse a known circuit

Keep the normally open button between digital pin 2 and GND from the previous guide. Use INPUT_PULLUP; a pressed button reads LOW. No motor, relay, or external LED is needed. First run the simple button-reading sketch again to confirm the wiring. Debouncing should refine a sound signal, not compensate for a loose jumper.

Arduino’s debounce example explains contact bounce and timed checking. The implementation below separates the most recent candidate reading from the last accepted stable state.

1. Upload the state machine

const byte buttonPin = 2;
const unsigned long settleMs = 30;
bool candidate;
bool stable;
bool ledOn = false;
unsigned long changedAt;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  candidate = digitalRead(buttonPin);
  stable = candidate;
  changedAt = millis();
  Serial.begin(9600);
}

void loop() {
  const unsigned long now = millis();
  const bool sample = digitalRead(buttonPin);

  if (sample != candidate) {
    candidate = sample;
    changedAt = now;
  }

  if (candidate != stable && now - changedAt >= settleMs) {
    stable = candidate;
    if (stable == LOW) {
      ledOn = !ledOn;
      digitalWrite(LED_BUILTIN, ledOn ? HIGH : LOW);
      Serial.println(ledOn ? "LED on" : "LED off");
    }
  }
}

2. Understand each state

Every time the raw reading changes, it becomes the new candidate and restarts the settling timer. Only a candidate that remains unchanged for at least 30 milliseconds replaces the stable state. The LED toggles only when that accepted state becomes LOW. Repeated loops while the button remains held therefore do not cause repeated toggles.

The interval is a starting point for this exercise, not a specification for all switches. The unsigned subtraction measures elapsed time across the normal millis() wrap. Both timing variables use unsigned long values. Other non-blocking tasks can run while the input settles.

3. Check the behaviour deliberately

  1. Open Serial Monitor at 9600 baud. Press once, release, then wait. Expect one message and one LED change.
  2. Hold the button for several seconds. Expect no further toggles until you release and press again.
  3. Try ten separated presses and count the messages. Every intentional press should produce one accepted change.
  4. Reset while holding the button. This sketch treats the startup reading as the initial state, so it waits for release and a new press before toggling.
  5. Try very brief taps. A tap shorter than the settling interval may intentionally be ignored.

Troubleshooting and sensible limits

If slow presses still double-trigger, inspect the contacts and wires before increasing the interval slightly. A switch that takes unusually long to settle may be damaged or unsuitable. If presses feel delayed, measure the actual requirement instead of reducing the interval blindly. Keep a small record of your switch, chosen interval, and observed results.

This filter suits a human-operated interface. It is not an emergency-stop design, and it should not be copied into a safety-critical control. For a menu or robot mode selector, keep the debounce logic separate from the action it requests. That lets you test input reliability with the built-in LED before permitting any movement.

Reference exercise · Read the evidence and checking record.

Your privacy, your choice.

This site currently loads no advertising or optional analytics scripts. Hosting includes aggregate traffic statistics and essential site-delivery functions. Optional services are off by default; changing these preferences does not activate them.

EssentialSite delivery and your saved privacy choices.Always on

Read the privacy notice