# \[BASIC] 012 SERVO SWEEP (เซอร์โว)

สวัสดีครับ สำหรับหัวข้อบทเรียนพื้นฐานบนบอร์ด HONEYLemon ที่ใช้งานผ่าน arduino library โดยเป็นการใช้งานเกี่ยวกับการใช้งาน servo ( เซอร์โว มอเตอร์ )

สำหรับการใช้งาน servo motor กับการใช้งานบอร์ด HONEYLemon นั้นง่ายมาก เพราะไม่ต้อง include อะไรเพิ่มเลย การเตรียมการขั้นแรกให้ประกาศว่าจะใช้งาน servo กันก่อน

```
Servo myservo; // สร้าง object สำหรับการควบคุม servo
```

จากนั้นก็ทำงานตั้งค่าความถี่ที่ต้องการใช้ และตั้งค่าขาที่ต้องการใช้งาน

```
void setup(){
...
    myservo.setPeriodHertz(50);     // ตั้งค่าความถี่มาตรฐาน 50 Hz
    myservo.attach(servoPin);       // กำหนดคาบเวลา min/max ที่จะใช้ควบคุม Servo
...
}
```

สำหรับขั้นตอนของการ setup servo เสร็จเรียบร้อยแล้ว ต่อไปเราก็ทำการควบคุม servo ได้เลยด้วยคำสั่ง

```
myservo.write( pos ); // โดยที่ pos คือองศาของ servo
```

สำหรับการต้องการเปลี่ยนจากตำแหน่ง **a ไปตำแหน่ง b** อย่างช้าๆ เราสามารถใช้ loop ในการช่วงชลอการเคลื่อนที่ได้ ตัวอย่างเช่น

```
int a = 0;
int b = 180;
for(int pos=a;pos<=b;pos++){  // หมุนทีละ 1 องศาจากตำแหน่ง a ไป b ทุกๆ 5 มิลลิวินาที
    myservo.write(pos);  // สั่งให้ servo หมุนไปที่ตำแหน่ง pos
    delay(5);            // รอ 5 มิลลิวินาที แล้วค่อยหมุนไปตำแหน่งต่อไป
}
```

อยากให้ช้าหรือเร็วก็ปรับตรง delay เอาก็ได้นะ สำหรับวิธีนี้เป็นการเขียนอย่างง่าย หากต้องใช้งานร่วมกับการทำงานอื่นๆ การเขียนคำสั่งโดยใช้ delay จะทำให้ไม่สามารถทำงาน multi function ได้

**ตัวอย่าง source code**

```
/*
    Basic : การเขียนโปรแกรมควบคุม Servo บนบอร์ดไมโครคอนโทรลเลอร์ HONEYLemon
*/
#include <HONEYLemon.h>

Servo myservo;      // สร้าง object สำหรับการควบคุม servo
int servoPin = 4;   //สร้างตัวแปรควบคุม Servo ( ขาที่ใช้งานได้ 4,12,14,15,18,19,21,22,23,25,26 )

void setup()
{
    lemon.begin();          // เรียกใช้งานฟังก์ชั่นเริ่มต้นของบอร์ด HONEYLemon

    Serial.begin(115200);   // เรียกใช้งาน Serial
    lemon.debug(Serial);    // ขอดู debug ของบอร์ด HONEYLemon ผ่าน Serial

    myservo.setPeriodHertz(50);     // ตั้งค่าความถี่มาตรฐาน 50 Hz
    myservo.attach(servoPin);       // กำหนดคาบเวลา min/max ที่จะใช้ควบคุม Servo
}

void loop()
{
    for (int pos = 0; pos <= 180; pos++) {  // ทำการกำหนดมุมเริ่มต้นเป็น 0 และทำงานซ้ำไปจนถึงมุม 180 โดยที่แต่ละรอบมีการเปลี่ยนแปลงมุม +1 อาศา
        myservo.write(pos);     // สั่งให้ Servo หมุนไปที่ตำแหน่ง pos (position)
        delay(15);              // รอ 15 มิลลิวินาที
	}
    for (int pos = 180; pos >= 0; pos--) {  // ทำการกำหนดมุมเริ่มต้นเป็น 180 และทำงานซ้ำไปจนถึงมุม 0 โดยที่แต่ละรอบมีการเปลี่ยนแปลงมุม -1 อาศา
        myservo.write(pos);     // สั่งให้ Servo หมุนไปที่ตำแหน่ง pos (position)
        delay(15);              // รอ 15 มิลลิวินาที
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lemon.honey.co.th/blogs/basic/basic-012-servo-sweep.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
