# \[BASIC] 002 Button (ปุ่มกด)

สวัสดีครับ สำหรับหัวข้อบทเรียนพื้นฐานบนบอร์ด HONEYLemon ที่ใช้งานผ่าน arduino library ในเรื่องนี้จะนำพาท่านไปพบกับการใช้งาน Button หรือปุ่มกดบนบอร์ด HONEYLemon ซึ่งจะช่วยให้ท่านพัฒนา code ได้อย่างรวดเร็ว และเป็นระเบียบมากขึ้น ใช้งานง่าย ปุ่มเดียวใช้ได้ ถึง 3 ฟังก์ชั่น

ฟังก์ชั่นที่เกี่ยวข้อง\
lemon.**onClick()** // สำหรับการกดปุ่มครั้งเดียว\
lemon.**onLongClick()** // สำหรับการกดปุ่มค้าง\
lemon.**onDoubleClick()** // สำหรับการกดปุ่มสองครั้งติดต่อกัน

วิธีการใช้เราจะสร้างฟังก์ชั่นสำหรับการใช้งานเมื่อกดปุ่มนั้นกันก่อน

```
void BTN_Click(){
    // ตัวอย่างสำหรับการเขียน Function เพื่อเอาไว้ทำงานเวลามีการกดปุ่มหนึ่งครั้ง
}
```

หลังจากนั้นเราจึงมาเพิ่มในฟังก์ชั่น setup() ให้มีการ callback function เมื่อมีการกดปุ่มหนึ่งครั้ง ไปที่ฟังก์ชั่น BTN\_Click() โดยใช้คำสั่งต่อไปนี้

```
void setup(){
...
    lemon.onClick(BTN_Click); // หากกดปุ่มครั้งเดียวให้เรียกใช้งานฟังก์ชั่น BTN_Click()
...
}
```

เราจะได้แล้ว 1 ฟังก์ชั่น หลังจากนั้นทำแบบเดียวกันกับอีก 2 ฟังก์ชั่น จะได้ตามตัวอย่างแล้วละครับ

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

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

void BTN_Click(){       // ฟังก์ชั่นสำหรับการกดปุ่มครั้งเดียว
    Serial.println("Button Click!");        // แสดงผลที่หน้าจอมอนิเตอร์
}
void BTN_LongClick(){   // ฟังก์ชั่นสำหรับการกดปุ่มค้าง
    Serial.println("Button Long Click!");   // แสดงผลที่หน้าจอมอนิเตอร์
}
void BTN_DoubleClick(){ // ฟังก์ชั่นสำหรับการกดปุ่มสองครั้ง
    Serial.println("Button Double Click!"); // แสดงผลที่หน้าจอมอนิเตอร์
}

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

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

    /*
        Function Event Callback

        onClick( __function__ );        // สำหรับกดปุ่มครั้งเดียว
        onLongClick( __function__ );    // สำหรับกดปุ่มค้าง
        onDoubleClick( __function__ );  // สำหรับกดปุ่มสองครั้ง
    */

    lemon.onClick(BTN_Click);               // หากกดปุ่มครั้งเดียวให้เรียกใช้งานฟังก์ชั่น BTN_Click()
    lemon.onLongClick(BTN_LongClick);       // หากกดปุ่มค้างให้เรียกใช้งานฟังก์ชั่น BTN_LongClick()
    lemon.onDoubleClick(BTN_DoubleClick);   // หากกดปุ่มสองครั้งให้เรียกใช้งานฟังก์ชั่น BTN_DoubleClick()
}

void loop()
{
}
```

หลังจากนั้นดู Serial Monitor และกดปุ่ม 1 ครั้ง, 2 ครั้ง, กดค้าง ตามลำดับ จะได้ผลดังรูป

![](/files/-MA5q0r-b-MMqc1s6SER)


---

# 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-002-button.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.
