0Pricing
Arduino & IoT Academy · บทเรียน

ตัดการเด้งด้วย millis()

ละเลยสัญญาณรบกวนด้วยช่วงเวลาสั้น ๆ ที่สัญญาณต้องคงที่

ตัดการเด้งด้วย millis() เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัดการเด้งด้วย millis()”

ละเลยสัญญาณรบกวนด้วยช่วงเวลาสั้น ๆ ที่สัญญาณต้องคงที่ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัดการเด้งด้วย millis()” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำไมปุ่มจึงเกิดการเด้ง
  2. ตัดการเด้งด้วย millis()
  3. เก็บค่าใน EEPROM
  4. จดจำตัวนับหลังรีเซ็ต
← กลับไปที่ Arduino & IoT Academy