🧩 1. ความหมายของ Control Structure
Control Structure คือ “โครงสร้างที่ควบคุมการทำงานของโปรแกรม”
ว่าจะ ทำคำสั่งไหนก่อน, ทำซ้ำไหม, หรือ จะเลือกเส้นทางใด
⚙️ 2. ประเภทของ Control Structures
ในภาษา Arduino (C/C++) มีอยู่ 3 กลุ่มหลัก คือ
| หมวด | โครงสร้าง | หน้าที่ |
|---|---|---|
| 1️⃣ Sequence | ลำดับ | ทำคำสั่งเรียงจากบนลงล่าง |
| 2️⃣ Selection | การเลือก (Decision) | เลือกทำบางส่วนตามเงื่อนไข |
| 3️⃣ Iteration (Looping) | การทำซ้ำ | ทำซ้ำจนกว่าจะจบเงื่อนไข |
🧮 3. Sequence Structure (โครงสร้างลำดับ)
คือการทำงานตามลำดับบรรทัด เช่น
void setup() {
pinMode(2, OUTPUT); // ขั้นที่ 1
digitalWrite(2, HIGH); // ขั้นที่ 2
delay(1000); // ขั้นที่ 3
digitalWrite(2, LOW); // ขั้นที่ 4
}
โปรแกรมจะทำจากบนลงล่างตามลำดับโดยไม่มีการตัดสินใจ
⚖️ 4. Selection Structure (การเลือก)
คือการ “เลือกทางเดินของโปรแกรม” ตามเงื่อนไขที่กำหนด
(ใช้กับเซนเซอร์, ปุ่ม, ค่าอุณหภูมิ ฯลฯ)
🔹 4.1 if (เงื่อนไขเดียว)
if (condition) {
// ทำงานถ้าเงื่อนไขเป็นจริง
}
📘 ตัวอย่าง:
int temp = 35;
if (temp > 30) {
Serial.println("Hot!");
}
🔹 4.2 if…else (เลือก 2 ทาง)
if (condition) {
// ถ้าเป็นจริง
} else {
// ถ้าเป็นเท็จ
}
📘 ตัวอย่าง:
int light = analogRead(34);
if (light < 500) {
Serial.println("Dark");
} else {
Serial.println("Bright");
}
🔹 4.3 if…else if…else (หลายเงื่อนไข)
if (temp < 20) {
Serial.println("Cold");
} else if (temp <= 30) {
Serial.println("Warm");
} else {
Serial.println("Hot");
}
🔹 4.4 Nested if (ซ้อนกันได้)
if (temp > 30) {
if (humidity > 70) {
Serial.println("Hot & Humid");
}
}
🔹 4.5 switch…case (เลือกหลายทางชัดเจน)
ใช้แทน if หลาย ๆ ชั้น เมื่อต้องเปรียบเทียบค่าคงที่
switch (mode) {
case 1:
Serial.println("Manual Mode");
break;
case 2:
Serial.println("Auto Mode");
break;
default:
Serial.println("Unknown Mode");
break;
}
ต้องมีคำสั่ง
break;เพื่อหยุดไม่ให้ไหลต่อไปยัง case ถัดไป
🔁 5. Iteration Structure (โครงสร้างวนซ้ำ / Loop)
คือการ “ทำซ้ำคำสั่ง” จนกว่าเงื่อนไขจะเป็นเท็จ
ใช้สำหรับควบคุม LED, อ่านค่าเซนเซอร์, นับเวลา ฯลฯ
🔹 5.1 for Loop (วนซ้ำแบบนับรอบ)
for (int i = 0; i < 5; i++) {
Serial.println(i);
}
การทำงาน:
- เริ่มที่
int i=0 - ตรวจว่า
i<5→ จริง → ทำในบล็อก - จบหนึ่งรอบ →
i++→ กลับไปเช็คใหม่
📘 ตัวอย่าง: เปิด LED ทีละดวง
int leds[] = {2, 4, 5, 18};
void setup() {
for (int i=0; i<4; i++) pinMode(leds[i], OUTPUT);
}
void loop() {
for (int i=0; i<4; i++) {
digitalWrite(leds[i], HIGH);
delay(200);
digitalWrite(leds[i], LOW);
}
}
🔹 5.2 while Loop (วนซ้ำตามเงื่อนไข)
while (condition) {
// ทำซ้ำตราบใดที่ condition ยังเป็นจริง
}
📘 ตัวอย่าง:
int count = 0;
while (count < 5) {
Serial.println(count);
count++;
}
🔹 5.3 do…while (ตรวจหลังทำ)
do {
// ทำก่อนตรวจ
} while (condition);
📘 ตัวอย่าง:
int count = 0;
do {
Serial.println(count);
count++;
} while (count < 5);
จะทำอย่างน้อย 1 ครั้งเสมอ แม้เงื่อนไขเป็นเท็จก็ตาม
🔹 5.4 Nested Loop (ซ้อนลูป)
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
Serial.print("i=");
Serial.print(i);
Serial.print(", j=");
Serial.println(j);
}
}
🧱 6. คำสั่งควบคุมภายในลูป
| คำสั่ง | ความหมาย | ตัวอย่าง |
|---|---|---|
break | หยุดลูปทันที | ออกจาก for, while, switch |
continue | ข้ามไปยังรอบถัดไป | ข้ามคำสั่งที่เหลือในรอบนั้น |
return | ออกจากฟังก์ชันทันที | ใช้ในฟังก์ชันที่เรียกซ้ำ |
📘 ตัวอย่าง:
for (int i=0; i<10; i++) {
if (i==5) continue; // ข้าม 5
if (i==8) break; // หยุดเมื่อ i==8
Serial.println(i);
}
⚡ 7. โครงสร้างพิเศษของ Arduino
Arduino มี “ลูปหลัก” อยู่แล้วคือ loop()
ทำให้ไม่ต้องเขียน while(true) เอง
void loop() {
// คำสั่งในนี้จะวนซ้ำตลอดเวลา
}
ถ้าอยากหยุดถาวร → ใช้ while(true); หรือ while(1);
เช่น
if (error) {
Serial.println("Error!");
while(true); // หยุดที่นี่
}
🧠 8. ตัวอย่างรวม Control Structures
🔸 ตัวอย่าง 1: การตัดสินใจเปิด/ปิดไฟ
int light = analogRead(34);
if (light < 500)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);
🔸 ตัวอย่าง 2: การนับจำนวนด้วย for loop
for (int i=1; i<=10; i++) {
Serial.println(i);
delay(500);
}
🔸 ตัวอย่าง 3: เมนูเลือกโหมดด้วย switch
int mode = 2;
switch (mode) {
case 1: Serial.println("Manual"); break;
case 2: Serial.println("Auto"); break;
default: Serial.println("Unknown"); break;
}
🔸 ตัวอย่าง 4: while loop ตรวจปุ่ม
while (digitalRead(15) == HIGH) {
Serial.println("Waiting for button...");
delay(200);
}
Serial.println("Button Pressed!");
🧾 9. ตารางสรุป Control Structures
| หมวด | คำสั่ง | ลักษณะการทำงาน | ตัวอย่าง |
|---|---|---|---|
| ลำดับ | (ไม่มี) | ทำตามลำดับ | A → B → C |
| การเลือก | if, if…else, switch | ทำบางส่วนตามเงื่อนไข | if(temp>30) |
| การทำซ้ำ | for, while, do…while | ทำซ้ำจนกว่าเงื่อนไขเท็จ | for(i=0;i<10;i++) |
| ควบคุมลูป | break, continue, return | จัดการการไหลในลูป | break; continue; |
🧩 10. แนวทางการใช้
✅ อธิบายด้วย “แผนภาพลำดับการไหล (Flowchart)” เช่น
- if → สองทาง
- for/while → วนลูกศรกลับ
- switch → กิ่งหลายทาง
✅ เขียนตัวอย่างจริงเชื่อมกับ LED / ปุ่มกด / เซนเซอร์
เช่น
- กดปุ่ม → LED Toggle
- อ่านค่า LDR → if แสดง “สว่าง/มืด”
- for loop → กระพริบไฟ 10 ครั้ง
- while → รอให้กดปุ่มก่อนเริ่มใหม่
✅ ให้ฝึก Debug ด้วย Serial Monitor เพื่อเห็นลำดับการทำงาน
📚 11. แบบฝึกหัดท้ายบท (แนะนำ)
1️⃣ เขียนโปรแกรมอ่านค่า LDR แล้วตัดสินใจเปิดไฟ (ใช้ if…else)
2️⃣ เขียนโปรแกรมนับเลข 1–10 แล้วแสดง “Odd/Even” (ใช้ for + %)
3️⃣ เขียนโปรแกรมเลือกโหมดด้วย switch
4️⃣ เขียนโปรแกรมตรวจปุ่มกดจนกว่าจะกดจริง (while)
5️⃣ เขียนโปรแกรมกระพริบไฟ 5 ดวงแบบวนซ้ำ (for ซ้อน for)
📘 12. สรุปแนวคิดสำคัญ
| แนวคิด | คำอธิบาย |
|---|---|
| Sequence | ทำงานตามลำดับ |
| Selection | ตัดสินใจเลือกเส้นทาง |
| Iteration | ทำซ้ำตามเงื่อนไข |
| Break / Continue | ควบคุมการไหลในลูป |
| Switch | เหมาะกับการเลือกหลายค่า |
| While / Do While | วนซ้ำจนกว่าจะเท็จ |
