🧩 1. ความหมายของ Control Structure

Control Structure คือ “โครงสร้างที่ควบคุมการทำงานของโปรแกรม”
ว่าจะ ทำคำสั่งไหนก่อน, ทำซ้ำไหม, หรือ จะเลือกเส้นทางใด


⚙️ 2. ประเภทของ Control Structures

ในภาษา Arduino (C/C++) มีอยู่ 3 กลุ่มหลัก คือ

หมวดโครงสร้างหน้าที่
1️⃣ Sequenceลำดับทำคำสั่งเรียงจากบนลงล่าง
2️⃣ Selectionการเลือก (Decision)เลือกทำบางส่วนตามเงื่อนไข
3️⃣ Iteration (Looping)การทำซ้ำทำซ้ำจนกว่าจะจบเงื่อนไข

🧮 3. Sequence Structure (โครงสร้างลำดับ)

คือการทำงานตามลำดับบรรทัด เช่น

C++
void setup() {
  pinMode(2, OUTPUT);   // ขั้นที่ 1
  digitalWrite(2, HIGH); // ขั้นที่ 2
  delay(1000);          // ขั้นที่ 3
  digitalWrite(2, LOW);  // ขั้นที่ 4
}

โปรแกรมจะทำจากบนลงล่างตามลำดับโดยไม่มีการตัดสินใจ


⚖️ 4. Selection Structure (การเลือก)

คือการ “เลือกทางเดินของโปรแกรม” ตามเงื่อนไขที่กำหนด
(ใช้กับเซนเซอร์, ปุ่ม, ค่าอุณหภูมิ ฯลฯ)


🔹 4.1 if (เงื่อนไขเดียว)

C++
if (condition) {
  // ทำงานถ้าเงื่อนไขเป็นจริง
}

📘 ตัวอย่าง:

C++
int temp = 35;
if (temp > 30) {
  Serial.println("Hot!");
}

🔹 4.2 if…else (เลือก 2 ทาง)

C++
if (condition) {
  // ถ้าเป็นจริง
} else {
  // ถ้าเป็นเท็จ
}

📘 ตัวอย่าง:

C++
int light = analogRead(34);
if (light < 500) {
  Serial.println("Dark");
} else {
  Serial.println("Bright");
}

🔹 4.3 if…else if…else (หลายเงื่อนไข)

C++
if (temp < 20) {
  Serial.println("Cold");
} else if (temp <= 30) {
  Serial.println("Warm");
} else {
  Serial.println("Hot");
}

🔹 4.4 Nested if (ซ้อนกันได้)

C++
if (temp > 30) {
  if (humidity > 70) {
    Serial.println("Hot & Humid");
  }
}

🔹 4.5 switch…case (เลือกหลายทางชัดเจน)

ใช้แทน if หลาย ๆ ชั้น เมื่อต้องเปรียบเทียบค่าคงที่

C++
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 (วนซ้ำแบบนับรอบ)

C++
for (int i = 0; i < 5; i++) {
  Serial.println(i);
}

การทำงาน:

  1. เริ่มที่ int i=0
  2. ตรวจว่า i<5 → จริง → ทำในบล็อก
  3. จบหนึ่งรอบ → i++ → กลับไปเช็คใหม่

📘 ตัวอย่าง: เปิด LED ทีละดวง

C++
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 (วนซ้ำตามเงื่อนไข)

C++
while (condition) {
  // ทำซ้ำตราบใดที่ condition ยังเป็นจริง
}

📘 ตัวอย่าง:

C++
int count = 0;
while (count < 5) {
  Serial.println(count);
  count++;
}

🔹 5.3 do…while (ตรวจหลังทำ)

C++
do {
  // ทำก่อนตรวจ
} while (condition);

📘 ตัวอย่าง:

C++
int count = 0;
do {
  Serial.println(count);
  count++;
} while (count < 5);

จะทำอย่างน้อย 1 ครั้งเสมอ แม้เงื่อนไขเป็นเท็จก็ตาม


🔹 5.4 Nested Loop (ซ้อนลูป)

C++
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ออกจากฟังก์ชันทันทีใช้ในฟังก์ชันที่เรียกซ้ำ

📘 ตัวอย่าง:

C++
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) เอง

C++
void loop() {
  // คำสั่งในนี้จะวนซ้ำตลอดเวลา
}

ถ้าอยากหยุดถาวร → ใช้ while(true); หรือ while(1);
เช่น

C++
if (error) {
  Serial.println("Error!");
  while(true); // หยุดที่นี่
}

🧠 8. ตัวอย่างรวม Control Structures

🔸 ตัวอย่าง 1: การตัดสินใจเปิด/ปิดไฟ

C++
int light = analogRead(34);
if (light < 500)
  digitalWrite(2, HIGH);
else
  digitalWrite(2, LOW);

🔸 ตัวอย่าง 2: การนับจำนวนด้วย for loop

C++
for (int i=1; i<=10; i++) {
  Serial.println(i);
  delay(500);
}

🔸 ตัวอย่าง 3: เมนูเลือกโหมดด้วย switch

C++
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 ตรวจปุ่ม

C++
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วนซ้ำจนกว่าจะเท็จ