0Pricing
Arduino & IoT Academy · Lektion

Mit millis() entprellen

Ignorieren Sie Störungen mit einem kurzen Stabilitätszeitfenster.

Mit millis() entprellen ist eine kostenlose Arduino & IoT Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Arduino & IoT Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Why Not Just delay()?

You could pause with delay() after a press, but that freezes your whole board. The smarter tool for clean buttons is millis().

millis() Is a Stopwatch

millis() returns how many milliseconds your board has been running. You compare two readings to measure elapsed time without stopping.

unsigned long now = millis();

The Debounce Plan

The idea is simple: when the pin changes, start a timer. Only trust the new state if it holds steady past a short window.

Pick a Debounce Window

A delay of about 50 milliseconds is the classic choice. It's long enough to outlast bounce, yet far too fast for you to notice.

const unsigned long DEBOUNCE = 50;

Remember the Last Change

You need a variable to store when the pin last flipped. Compare it to the current time on every loop pass.

unsigned long lastChange = 0;

Track Two States

Keep the raw reading and the confirmed stable state separately. The raw one is noisy; the stable one is what your logic trusts.

int lastRaw = HIGH;
int stable = HIGH;

Detect a Flip

Each loop, read the pin. If it differs from the last raw value, the signal just moved, so reset your timer.

int raw = digitalRead(BTN);
if (raw != lastRaw)
  lastChange = millis();

Wait for Stillness

If enough time has passed since the last flip, the reading has settled. Now it is safe to accept it as the real state.

if (millis() - lastChange > DEBOUNCE) {
  stable = raw;
}

Act on the Edge

To fire once per press, watch for the stable state changing to LOW. That single edge is your clean, debounced press.

if (stable == LOW && lastStable == HIGH)
  Serial.println("clean press");

Update for Next Time

At the loop's end, save the current readings into your last variables. This keeps the comparison fresh on every pass.

lastRaw = raw;
lastStable = stable;

Non-Blocking Wins

Because millis() never pauses the board, your sensors and outputs keep running. You debounce while everything else stays alive. 🚀

Quick Check

Let's check how millis() debouncing decides a press is real.

Recap: Clean Presses

You used millis() to ignore bounce without freezing the board, accepting a press only once it settles. Solid, professional input! ✅

Häufig gestellte Fragen

Ist die Lektion „Mit millis() entprellen“ kostenlos?

Ja — der vollständige Text von „Mit millis() entprellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Arduino & IoT Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit millis() entprellen“?

Ignorieren Sie Störungen mit einem kurzen Stabilitätszeitfenster. Du übst Arduino & IoT Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Arduino & IoT Academy zu starten?

Keine Vorkenntnisse erforderlich. Arduino & IoT Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Mit millis() entprellen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Arduino & IoT Academy-Lektion Code schreiben und ausführen?

Ja. Jede Arduino & IoT Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum Taster „prellen“
  2. Mit millis() entprellen
  3. Einen Wert im EEPROM speichern
  4. Einen Zähler nach dem Reset behalten
← Zurück zu Arduino & IoT Academy