# \[BASIC] 005 SOUND PLAY JINGLE BELLS (เล่นเพลง จิงเกิลเบลส์)

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

อ่านข้อมูลเพิ่มเติมในการประยุกต์ใช้คำสั่งเกี่ยวกับเสียงได้ที่นี่&#x20;

{% content-ref url="/pages/-MA5qS-Y71xgIYb9OqPZ" %}
[\[BASIC\] 004 SOUND TONE BUZZER (เสียงดนตรี)](/blogs/basic/basic-004-sound-tone-buzzer.md)
{% endcontent-ref %}

บทความนี้เป็นการประยุกต์ใช้คำสั่ง lemon.tone() และ lemon.noTone()\
ในกระบวนการแรกของการสั่งให้เล่นดนตรีเป็นเพลงนั้นจะต้องมีข้อมูลตัวโน๊ตก่อนว่า ตัวโน๊ตแต่ละตัวมีความถี่อะไรและเราจะเอามาต่อกันเป็น Melody

![](/files/-MA5qwP8nwiht-wWevrH)

จากตารางข้างต้นเราจะได้ตัวโน๊ตและความถี่ของเสียงแล้ว หลังจากนั้นเราก็แปลงโน๊ตเป็นความถี่กันครับ เช่น C จะได้ความถี่ 523 เป็นต้น

เมื่อครบทุกตัวอักษรแล้วเราก็มาสร้างอาเรย์เก็บตัวโน๊ตต่างๆ กันครับ

```
...
    // สร้างอาเรย์สำหรับเก็บข้อมูลตัวโน๊ต
    int notes[]={
        659, 659, 659, -1,  659, 659, 659, -1,  659, 784, 523,
        587, 659, -1,  698, 698, -1,  698, 698, -1,  698, 659,
        659, 659, 659, -1,  659, 587, 587, 659, 587, 784
    };
...
```

เมื่อได้เป็นเพลงแล้วเราก็เริ่มสั่งให้โปรแกรมทำงานเล่นเพลงนี้กันเลย โดยให้เล่นทีละตัวโน๊ตไปเรื่อยๆ จนกว่าจะครบทุกตัว

```
...
    // วนอ่านข้อมูลตัวโน๊ตแต่ละตัวในอาเรย์ notes[]
    for(int i=0;i<(sizeof(notes)/sizeof(int *));i++){
        lemon.tone(notes[i],400);   // สั่งให้เล่นตัวโน๊ต
    }
...
```

อธิบายคำสั่งเพิ่มเติม

```
for(
    int i=0;  // สร้าง i เป็นตัวแปร int เพื่อเป็น index ให้ array ของตัวโน๊ต
    i<(sizeof(notes)/sizeof(int *)); // ตรวจสอบว่าตอนนี้ที่เล่นอยู่เป็นตัวสุดท้ายหรือยัง
    i++)  // ถ้าไม่ใช่ตัวสุดท้ายให้เล่นโน๊ตตัวถัดไป
```

ภายในคำสั่งวนลูป หรือทำซ้ำ ( for ) นั้นใช้คำสั่ง lemon.tone() เพื่อให้เล่นตัวโน๊ตนั้นๆ

```
lemon.tone(notes[i],400);
// อธิบายเพิ่มเติม
lemon.tone(    // คำสั่งสำหรับเล่นตัวโน๊ตตามความถี่ต่างๆ
    notes[i],  // เลือกตัวโน๊ตจาก array โดยอ้างอิงจากตัวที่ i เช่น i=0 หมายถึงตัวแรก
    400        // เล่นนานเป็นระยะเวลา 400 มิลลิวินาที (0.4 วินาที) แล้วหยุดเล่น
);
```

ดังนั้นเมื่อทำงานร่วมกันแล้วสิ่งที่เกิดขึ้นคือ…

```
    int notes[]={
        659, 659, 659, -1,  659, 659, 659, -1,  659, 784, 523,
        587, 659, -1,  698, 698, -1,  698, 698, -1,  698, 659,
        659, 659, 659, -1,  659, 587, 587, 659, 587, 784
    };

    for(int i=0;i<(sizeof(notes)/sizeof(int *));i++){
        lemon.tone(notes[i],400);   // สั่งให้เล่นตัวโน๊ต
    }

// 1.สร้างตัวแปร array ชื่อ notes เพื่อจัดเก็บความถี่ของโน๊ตดนตรี ประกอบกันเป็น melody
// 2.ใช้คำสั่งทำซ้ำ loop โดยให้เล่นตั้งแต่โน๊ตตัวแรก (0) ไปจนถึงตัวสุดท้าย
// 3.โน๊ตแต่ละตัวเล่นนานระยะเวลา 0.4 วินาที แล้วจึงเล่นตัวถัดไป
```

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

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

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

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

void loop()
{
    // สร้างอาเรย์สำหรับเก็บข้อมูลตัวโน๊ต
    int notes[]={
        659, 659, 659, -1,  659, 659, 659, -1,  659, 784, 523,
        587, 659, -1,  698, 698, -1,  698, 698, -1,  698, 659,
        659, 659, 659, -1,  659, 587, 587, 659, 587, 784
    };
    
    // วนอ่านข้อมูลตัวโน๊ตแต่ละตัวในอาเรย์ notes[]
    for(int i=0;i<(sizeof(notes)/sizeof(int *));i++){
        lemon.tone(notes[i],400);   // สั่งให้เล่นตัวโน๊ต
    }

    lemon.noTone();     // สั่งให้หยุเล่นเสียง
    delay(1000);        // รอ 10000 มิลลิวินาที (10 วินาที)
}
```


---

# 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-005-sound-play-jingle-bells.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.
