ทำงานสองอย่างพร้อมกัน
ทำไฟกะพริบและอ่านปุ่มพร้อมกัน
ทำงานสองอย่างพร้อมกัน เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Multitasking Dream
Real devices do many things at once: blink a light while watching a button. With non-blocking timing your board can finally juggle both.
Each Task Gets Its Timer
The trick is one timer per task. Give the blink its own previousMillis and interval, separate from anything else you want to run.
Track Time Separately
Keep a dedicated previousMillis for the LED so its schedule never tangles with other timed jobs in the same loop.
unsigned long ledPrevious = 0;
const long ledInterval = 500;Blink Block
First, the blink block checks its own timer. When 500 milliseconds pass it toggles the LED and resets, then control moves on.
if (millis() - ledPrevious >= ledInterval) {
ledPrevious = millis();
}Read the Button Every Loop
Right after the blink block, you read the button on every single pass. Because nothing froze, no press is ever missed.
int state = digitalRead(buttonPin);Two Jobs, One Loop
Now the same loop handles both: it times the blink and checks the button thousands of times a second. They feel simultaneous.
Add a Third Task
Want more? Just add another timer and block, like reading a sensor every two seconds. The pattern scales to many tasks cleanly.
Keep the Loop Fast
The rule that makes it work: never block. Keep every part of the loop quick so it can return often and serve all tasks fairly.
Not True Parallelism
The board still does one thing at a time, just very fast. This is cooperative multitasking, where each task does a little and yields.
Banish the Big delay
One long delay anywhere breaks the whole illusion, since it freezes every task. Replace pauses with millis checks to keep things smooth.
The Heart of Responsive IoT
This state machine style, many small timed checks in one loop, is how responsive sensors, displays, and connected devices are really built.
Quick Check
How does one loop blink an LED and read a button at the same time?
Recap
You combined timers in one loop to multitask: blink and read a button together, with no blocking delay. That is the core skill behind real IoT devices. 🚀
คำถามที่พบบ่อย
บทเรียน “ทำงานสองอย่างพร้อมกัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ทำงานสองอย่างพร้อมกัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทำงานสองอย่างพร้อมกัน”
ทำไฟกะพริบและอ่านปุ่มพร้อมกัน คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ทำงานสองอย่างพร้อมกัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- int, float, bool บน Arduino
- ทำไม delay() จึงบล็อกทุกอย่าง
- ทำไฟกะพริบโดยไม่ใช้ delay ด้วย millis
- ทำงานสองอย่างพร้อมกัน