# \[BASIC] 003 SET LED COLOR RGB (เปลี่ยนสีไฟ RGB LED)

สวัสดีครับ สำหรับหัวข้อบทเรียนพื้นฐานบนบอร์ด HONEYLemon ที่ใช้งานผ่าน arduino library โดยเป็นการใช้งานเกี่ยวกับแสงสี เพื่อให้ความง่ายในการแสดงสีสันนั้นใช้เพียงฟังก์ชั่นเดียวก็สามารถแสดงผลได้หลากหลายแสดงสีเลยทีเดียว

คำสั่งที่เกี่ยวข้อง\
lemon. setBrightnessLED() // เป็นการตั้งค่าความสว่างได้ตั้งแต่ 0-255 (0-100%)\
lemon. setColorLED() // เป็นการสั่งให้หลอดเปลี่ยนสีไฟตามต้องการ

สำหรับการตั้งค่าความสว่างโดยรวมเรามักจะใช้คำสั่งนี้ตั้งแต่ขั้นตอนของการ setup ฟังก์ชั่น ดังนี้

```
void setup(){
...
    lemon.setBrightnessLED(50); // ปรับความสว่างที่ 50 (0-255)
...
}
```

และเมื่อเราต้องการปรับสีของหลอดแต่ละหลอดเราจะใช้คำสั่ง

```
lemon.setColorLED( no_led , lv_red , lv_green , lv_blue );
// no_led เช่น 1,2,3,4,5,6
// lv_red เป็นการระบุว่าต้องการแสดงสีแดงเท่าไหร่ (0-255)
// lv_green เป็นการระบุว่าต้องการแสงสีเขียวเท่าไหร่ (0-255)
// lv_blue เป็นการระบุว่าต้องการแสงสีน้ำเงินเท่าไหร่ (0-255)
```

ตัวอย่างการใช้งาน ต้องการสีแดงที่หลอดไฟ LED 1

```
lemon.setColorLED( 1, 255, 0, 0); // ให้หลอดที่ 1 เป็นสีแดง
```

ดังนั้นถ้าต้องการให้สีแดงทุกหลอดจะเขียนได้อย่างไรบ้าง

```
// แบบที่ 1 สายถึก
lemon.setColorLED( 1, 255, 0, 0); // ให้หลอดที่ 1 เป็นสีแดง
lemon.setColorLED( 2, 255, 0, 0); // ให้หลอดที่ 2 เป็นสีแดง
lemon.setColorLED( 3, 255, 0, 0); // ให้หลอดที่ 3 เป็นสีแดง
lemon.setColorLED( 4, 255, 0, 0); // ให้หลอดที่ 4 เป็นสีแดง
lemon.setColorLED( 5, 255, 0, 0); // ให้หลอดที่ 5 เป็นสีแดง
lemon.setColorLED( 6, 255, 0, 0); // ให้หลอดที่ 6 เป็นสีแดง
```

```
// แบบที่ 2 สายลูบ
for(int i=1;i<=6;i++){
    lemon.setColorLED( i, 255, 0, 0); // ให้เป็นสีแดงทีละหลอดเริ่มจากหลอดที่ 1-6
}
```

ซึ่งจริงๆ แล้วหากต้องการทุกหลอดสีเดียวกันจริงๆ ไม่จำเป็นต้องเขียนเยอะขนาดนี้ เพียงใส่ no\_led เป็น 0 เท่านั้นเอง จะได้แบบนี้ ซึ่งจะให้ผลแบบเดียวกันเลย

```
lemon.setColorLED( 0, 255, 0, 0); // สีแดงทุกหลอดไปเลยจ้าาาา
```

และทั้งหมดนั้นถูกเขียนไว้เป็นตัวอย่างพื้นฐานแล้วตามตัวอย่าง source code ด้านล่างเลยครับ

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

```
/*
    Basic : การเขียนโปรแกรมกับการใช้งานไฟ LED RGB บนบอร์ดไมโครคอนโทรลเลอร์ HONEYLemon
*/
#include <HONEYLemon.h>

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

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

    lemon.setBrightnessLED(50); // สำหรับปรับความสว่าง 0-255
}

void loop()
{
    /*
        setColorLED(
            0,      // ใส่ 0 สำหรับทุกหลอด หรือระบุ 1,2,3,4,5,6 สำหรับสั่งงานเฉพาะหลอดนั้นๆ
            255,    // สำหรับตั้งค่าแสงสีแดง 0-255
            255,    // สำหรับตั้งค่าแสงสีเขียว 0-255
            255     // สำหรับตั้งค่าแสงสีน้ำเงิน 0-255
        );
    */
    lemon.setColorLED(0,255,0,0);   // สั่งให้ทุกหลอดแสดงสีแดง
    delay(1000);    // รอ 1000 มิลลิวินาที (1 วินาที)

    lemon.setColorLED(0,0,255,0);   //สั่งให้ทุกหลอดแสดงสีเขียว
    delay(1000);    // รอ 1000 มิลลิวินาที (1 วินาที)

    lemon.setColorLED(0,0,0,255);   //สั่งให้ทุกหลอดแสดงสีน้ำเงิน
    delay(1000);    // รอ 1000 มิลลิวินาที (1 วินาที)
}
```

&#x20;**VDO ตัวอย่างจาก source code**

{% embed url="<https://www.youtube.com/watch?v=aq5KyU8p9Ck&feature=emb_title>" %}


---

# 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-003-set-led-color-rgb-rgb-led.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.
