loop() ทำงานตลอดไป
จังหวะหัวใจที่ทำให้ตรรกะของคุณทำซ้ำไม่สิ้นสุด
loop() ทำงานตลอดไป เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Heartbeat
After setup finishes, the second block takes over. Meet loop(), the part of your sketch that never stops running. 🔁
Runs Over and Over
The key idea: loop() repeats endlessly. The board runs it, reaches the bottom, then jumps straight back to the top.
What loop() Looks Like
Just like setup, it returns void and holds your code inside curly braces. This is where your device's behavior lives.
void loop() {
// repeating code goes here
}Setup First, Then Loop
Order matters. The board runs setup() once, then hands control to loop forever. They are partners, not rivals.
A Blinking Example
Inside loop you might turn an LED on, wait, then off. The board repeats this, so the light blinks again and again.
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}Reading Inputs Live
Because loop runs constantly, it can check sensors thousands of times a second. That is how your device stays responsive to the world.
No while(true) Needed
You do not write your own forever-loop. Arduino already repeats loop() for you, so just describe one pass of behavior.
It Never Truly Ends
loop only stops when power is cut or the board resets. As long as it has power, your loop() keeps cycling.
Order Inside Matters
Lines in loop run top to bottom each pass. Rearranging them changes the sequence of actions your device performs.
Empty Loop Is Allowed
An empty loop() compiles fine, but your device just sits idle. The braces are still required even with nothing inside.
void loop() {
}Together They Are a Sketch
Every Arduino program needs both blocks: setup() to prepare and loop() to act. With these two, you can build anything.
void setup() {
}
void loop() {
}Quick Check
Let's check how loop() behaves.
Recap: loop()
You learned that loop() runs forever after setup, repeating top to bottom. It is where your device senses and reacts on every pass. 🎉
คำถามที่พบบ่อย
บทเรียน “loop() ทำงานตลอดไป” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “loop() ทำงานตลอดไป” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “loop() ทำงานตลอดไป”
จังหวะหัวใจที่ทำให้ตรรกะของคุณทำซ้ำไม่สิ้นสุด คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “loop() ทำงานตลอดไป” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- setup() ทำงานครั้งเดียว
- loop() ทำงานตลอดไป
- คอมเมนต์และสเก็ตช์ที่เป็นระเบียบ
- ข้อผิดพลาดขณะคอมไพล์และวิธีอ่าน