0Pricing
Arduino & IoT Academy · 课时

使用 millis() 消抖

用短暂的稳定时间窗口过滤噪声

使用 millis() 消抖 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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! ✅

常见问题解答

「使用 millis() 消抖」课时是免费的吗?

是的 — 「使用 millis() 消抖」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「使用 millis() 消抖」这节课中我会学到什么?

用短暂的稳定时间窗口过滤噪声 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 millis() 消抖」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 按钮为什么会“抖动”
  2. 使用 millis() 消抖
  3. 将数值存入 EEPROM
  4. 复位后保留计数器数值
← 返回 Arduino & IoT Academy