ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis
ติดตามเวลาที่ผ่านไปเพื่อทำงานตามกำหนดเวลา
ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
A Built-in Stopwatch
The millis function returns how many milliseconds your board has been running since it powered on. It is a free, always-counting stopwatch. ⏱️
unsigned long now = millis();Why unsigned long
Store millis in an unsigned long. The count grows huge over time, and a plain int would overflow in under a minute.
The Core Idea
Instead of pausing, you remember the last time you acted, then keep checking how much time has passed since then on every loop.
Remember the Last Moment
Keep a variable like previousMillis to store when you last toggled the LED. You will compare it against the current time each pass.
unsigned long previousMillis = 0;Set Your Interval
Choose an interval, say 1000 milliseconds, as the gap between blinks. Acting only after this much time passes gives a steady rhythm.
const long interval = 1000;Has Enough Time Passed
Each loop, subtract previousMillis from the current millis. If the gap is at least your interval, it is time to act.
if (millis() - previousMillis >= interval) {Reset the Timer
The instant you act, set previousMillis to the current time. This restarts the countdown so the next blink waits a full interval again.
previousMillis = millis();Flip the LED
Inside the timed block you simply toggle the LED, flipping it on if it was off and off if it was on, for a clean blink.
ledState = !ledState;
digitalWrite(ledPin, ledState);The Loop Never Stops
Between blinks the loop keeps spinning at full speed, free to read sensors or buttons. Nothing is ever frozen waiting.
Subtraction Handles Overflow
After about 49 days millis wraps to zero, but the subtraction trick still gives the right gap, so your timing keeps working.
A Reusable Pattern
This millis pattern works for any repeating task, not just blinking. Change the interval and the action and you can schedule anything.
Quick Check
Why do we store the result of millis in an unsigned long?
Recap
You learned the millis pattern: remember the last time, check the elapsed gap, then act and reset. Now your board blinks while staying fully alert. 🎉
คำถามที่พบบ่อย
บทเรียน “ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis”
ติดตามเวลาที่ผ่านไปเพื่อทำงานตามกำหนดเวลา คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- int, float, bool บน Arduino
- ทำไม delay() จึงบล็อกทุกอย่าง
- ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis
- ทำงานสองอย่างพร้อมกัน