Buzzer En: 두 판 사이의 차이

아두위키 : Arduwiki
잔글편집 요약 없음
잔글편집 요약 없음
66번째 줄: 66번째 줄:


</syntaxhighlight>
</syntaxhighlight>
<div class="coders70">
<youtube> vayYbS_Qjy0 </youtube>
</div>


=== Example 2. Simple Melody ===
=== Example 2. Simple Melody ===

2024년 7월 10일 (수) 14:03 판

A component that emits sound when a specific frequency value is applied.

It operates between 3.3V to 5V.

Required Hardware

  • Buzzer
  • Arduino Board
  • Jumper Cables

Connection

Since it has polarity, make sure to check the + and - terminals.

It is okay to connect to a digital pin other than D11.

buzzer Arduino UNO
+ D11(changeable)
- GND

Example Code

Reference Material: Standard Frequencies for Octaves and Scales

Example 1. Scale Test

#define buzzer 11
int numTones = 8;

// In order (C4, D4, E4, F4, G4, A4, B4, C5)
double tones[] = { 261.6256, 293.6648, 329.6276, 349.2282, 391.9954, 440, 493.8833, 523.2511 };

int cnt = 0;

void setup()
{

}

void loop()
{
    if (cnt == 0)
    {
        for (int i = 0; i < numTones; i++)
        {
            // Use the tone(pin, frequency) function.
            tone(buzzer, tones[i]);
            delay(500);
        }
        // If you do not use the noTone function, the previously inputted tone function will continue,
        // so turn off the sound with the noTone function.
        noTone(buzzer);
        delay(500);
        cnt++;
    }
}



Example 2. Simple Melody

#define buzzer 11

int numTones = 8;
double g_tones[] = { 195.9977, 220, 246.9417, 261.6256, 293.6648, 329.6276, 349.2282, 391.9954 };
//G0, A1, B2, C3, D4, E5, F6, G7
int cnt = 0;

void setup()
{

}

void loop()
{
    if (cnt == 0)
    {
        tone(buzzer, g_tones[0]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[2]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[4]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[4]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[5]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[5]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[6]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[5]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[1]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[4]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[4]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[2]);
        delay(250 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[1]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[2]);
        delay(125 * 2);
        noTone(buzzer);
        tone(buzzer, g_tones[3]);
        delay(250 * 4);
        noTone(buzzer);
        cnt++;
    }else
    {
   
    }
}