\

코딩/아두이노

아두이노 피에조 부저 방법 & plum r 피에조 부저 코드

코딩하는 메추라기 2021. 8. 23. 11:46
반응형

목차

        1. 서론

        2. 설치 방법

        3. 알아야 할 코드

                ▶ #define

                ▶ pinMode

                ▶ tone

                ▶ delay

        4. Plum R

        5. 마무리

 

안녕하세요! 코딩하는 메추라기 케일입니다.

오늘은 제가 아두이노에 대해 글을 써 보려고 해요.

아두이노 중에서도 소리를 낼 수 있는 피에조 부저 라는 것이 있거든요. 

피에조 부저는 소리를 낼 수 있는 아두이노 부품입니다.

 

----설치 방법----

피에조 부저를 아두이노에서 작동시키는 방법입니다.

 

이렇게 생겼지만

사실 피에조 부저 윗부분을 보면 쪼그만하게 +라고 쓰여져 있는 것을 보실 수 있을 거에요.

그래서 저 피에조 부저의 +부분이 빨간색 전선과 연결되어 있습니다.

빨간색 전선은 5번 핀에 연결되어 있습니다.

그리고 +의 반대쪽(검정색 전선이 있는 곳) 에는 파란색 전선이 연결되어 있습니다.

파란색 전선은 GND로 이어집니다.

 

이렇게 설치는 끝났습니다.

 

----알아야 할 코드----

피에조 부저를 사용할 때 꼭 알아야 할 코드를 알려드리겠습니다.

제일 처음으로 할 것은 계이름들을 정의해 주셔야 합니다.

피에조 부저는 주파수로 소리를 내기 때문에 각각의 음에 맞는 주파수를 정해 주셔야 합니다.

#define C 262

C를 262로 정하라 라는 뜻입니다.

음계중에서 가장 가운데 있는 도의 주파수가 262 입니다.

 

아래 코드는 

void setup() {

}

위에 쓰시는 겁니다.

#define C 262 // 도
#define Cs 277 // 도#
#define D 294 // 레
#define Ds 311 // 레#
#define E 330 // 미
#define F 349 // 파
#define Fs 370 // 파#
#define G 392 // 솔
#define Gs 415 // 솔#
#define A 440 // 라
#define As 466 // 라#
#define B 494 // 시

지금 위 코드는 "도" 부터 "시"까지의 음의 주파수를 검정 건반 포함해서 정하는 코드입니다. 

 

그리고 코드를 실행시키기 전, 세팅 같은 걸 해 주셔야 합니다.

위에 아두이노 사진을 보시면 빨간색 전선이 5번 핀에 연결되에 있습니다.

그러면 5번 핀에 전력을 공급해 줘야겠죠?

void setup() {


}

안에 이런 코드를 집어넣어 줍니다.

pinMode(5, OUTPUT)

꼭 대소문자를 구분해야 합니다.

5번 핀의 모드를 OUTPUT 모드로, 즉 출력 모드로 바꾼다는 이야기입니다.

 

이제 본격적으로 노래를 트는 코드를 알려드리겠습니다.

 tone 함수가 음을 내는 역할을 담당합니다.

tone 함수는 무슨 핀에서 무슨 음을 낼지, 얼마나 길게 소리를 내는지 정할 때 쓰는 코드입니다.

이 아래 코드들은 

void loop() {

 

}

안에 쓰시는 겁니다.

tone(5, C, 1000);

위 코드는 5번 핀의 피에저 부조에 C음(아까 우리가 #define으로 C의 주파수를 262로 정해주었죠? 262 주파수는 피아노의 가운데 도를 가리킵니다.)을 1000ms(마이크로초) 동안 낸다는 소립니다.

한마디로 5번 핀에 연결된 피에더 부조에 "도" 음을 1초 동안 낸다는 소리입니다.
참고)
1마이크로초 = 0.001 초 -> 1000마이크로초 = 1초

 

그럼 이건 무슨 뜻일까요?

tone(5, C, 200);

5번 핀에 연결된 피에저 부조에 도 음을 0.2초동안 재생한다는 소리입니다.

 

마지막으로 필요한 함수는

delay(); 가 있습니다.

dalay는 말 그대로 기다린다는 뜻입니다.

 

기다리는 것도 아주 중요한데요.

여기 기다리지 않고 바로 다음 음을 내는 코드가 있습니다.

tone(5, C, 200);
tone(5, D, 200);

이렇게 할 경우 도 음과 레 음이 동시에 0.2초가 나옵니다.

말만 동시에지 피에저 부조는 화음이 나오지 못하기 때문에 이상한 소리만 나올 것입니다.

 

그래서

delay함수를 쓰는 겁니다.

tone(5, C, 200);
delay(200);
tone(6, D, 200);
delay(200);

이렇게 할 경우

도 음이 0.2초 동안 나오고

그 동안 0.2초 동안 대기를 타 줍니다.

그러면 도 음이 0.2초동안 나온 다음 바로 레 음이 0.2초 나오겠죠?

 

아마 잘 나오실 겁니다.

 

여기까지 알면 자신이 원하는 음을 쉽게 내실 수 있을 것입니다.

 

쉼표는 어떻게 표시할까요?

 

tone(5, C, 500);
delay(1000);
tone(5, D, 500);
delay(500);

중간에 delay(1000);

보이시죠?

0.5초동안 도 음을 내면서

1초를 대기 타면

0.5초동안 아무 소리도 나지 않은 다음 레 음이 나올 겁겁니다.

 

자 그럼 저는 엄청난 노가다를 통해 Plum 님의 곡 R을 한번 아두이노 코드로 만들어 보았는데요

저작권 문제가 있으면 바로 내리겠습니다.

화음이 안 되서 중간에 어색한 부분이 있을 수 있습니다.

 

----Plum R 아두이노 코드----

 

#define C 262
#define Cs 277
#define D 294
#define Ds 311
#define E 330
#define F 349
#define Fs 370
#define G 392
#define Gs 415
#define A 440
#define As 466
#define B 494
#define hC 523
#define hCs 554
#define hD 587
#define hDs 622
#define hE 659
#define hF 693
#define hFs 740
#define hG 784
#define hGs 831
#define hA 880
#define hAs 932
#define hB 988
#define hhC 1047
#define hhCs 1109
#define hhD 1175
#define hhDs 1245
#define hhE 1319
#define hhF 1397
#define hhFs 1480
#define hhG 1568
#define hhGs 1661
#define hhA 1760
#define hhAs 1865
#define hhB 1976
#define hhhC 2093
#define hhhCs 2217
#define hhhD 2349
#define hhhDs 2489
#define hhhAs 3729
#define hhhB 3951

void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  delay(600);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, Gs, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, As, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, As, 200);
  delay(200);
  
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, Gs, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, As, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhF, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhhAs, 200);
  delay(201);
  tone(5, hF, 200);
  delay(200);

  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hC, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, Gs, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 200);
  delay(400);
  tone(5, hDs, 200);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, As, 200);
  delay(200);


  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hC, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, Gs, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hF, 200);
  delay(400);
  tone(5, hDs, 200);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhF, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhhAs, 400);
  delay(400);

  tone(5, As, 400);
  delay(400);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hD, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hF, 600);
  delay(600);
  tone(5, hG, 200);
  delay(200);
  tone(5, hDs, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);

  tone(5, hC, 400);
  delay(400);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hD, 400);
  delay(400);
  tone(5, hC, 400);
  delay(400);

  tone(5, hC, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);
  tone(5, As, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);

  tone(5, hC, 370);
  delay(400);
  tone(5, hC, 400);
  delay(400);
  tone(5, hD, 200);
  delay(200);
  tone(5, B, 200);
  delay(200);
  tone(5, hC, 200);
  delay(201);
  tone(5, E, 200);
  delay(200);

  tone(5, hF, 600);
  delay(600);
  tone(5, hDs, 100);
  delay(100);
  tone(5, hD, 100);
  delay(100);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hFs, 400);
  delay(400);
  tone(5, hF, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, As, 200);
  delay(200);

  tone(5, hDs, 400);
  delay(400);
  tone(5, hD, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hF, 370);
  delay(400);
  tone(5, hF, 200);
  delay(200);


  tone(5, As, 400);
  delay(400);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hD, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hF, 600);
  delay(600);
  tone(5, hG, 200);
  delay(200);
  tone(5, hDs, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);

  tone(5, hC, 400);
  delay(400);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hD, 400);
  delay(400);
  tone(5, hC, 400);
  delay(400);

  tone(5, hC, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);
  tone(5, As, 600);
  delay(600);
  tone(5, As, 200);
  delay(200);

  tone(5, hC, 370);
  delay(400);
  tone(5, hC, 400);
  delay(400);
  tone(5, hD, 200);
  delay(200);
  tone(5, B, 200);
  delay(200);
  tone(5, hC, 200);
  delay(201);
  tone(5, E, 200);
  delay(200);

  tone(5, hF, 600);
  delay(600);
  tone(5, hDs, 100);
  delay(100);
  tone(5, hD, 100);
  delay(100);
  tone(5, hDs, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hFs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hF, 370);
  delay(400);
  tone(5, hF, 200);
  delay(200);
  tone(5, hG, 200);
  delay(200);
  tone(5, hGs, 200);
  delay(200);

  tone(5, hGs, 600);
  delay(600);
  tone(5, hG, 100);
  delay(100);
  tone(5, hF, 100);
  delay(100);
  tone(5, hG, 600);
  delay(600);
  tone(5, hGs, 100);
  delay(100);
  tone(5, hAs, 100);
  delay(100);

  tone(5, hB, 600);
  delay(600);
  tone(5, hAs, 600);
  delay(600);
  tone(5, hFs, 400);
  delay(400);

  tone(5, hCs, 200);
  delay(200);
  tone(5, B, 400);
  delay(400);
  tone(5, hFs, 400);
  delay(400);
  tone(5, B, 200);
  delay(200);
  tone(5, hCs, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);
  
  tone(5, hC, 90);
  delay(101);
  tone(5, B, 90);
  delay(101);
  tone(5, As, 190);
  delay(201);
  tone(5, Fs, 190);
  delay(201);
  tone(5, hC, 90);
  delay(101);
  tone(5, B, 90);
  delay(101);
  tone(5, As, 190);
  delay(201);
  tone(5, Fs, 190);
  delay(201);
  tone(5, hC, 190);
  delay(201);
  tone(5, B, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);

  tone(5, Fs, 200);
  delay(200);
  tone(5, hFs, 400);
  delay(400);
  tone(5, hAs, 90);
  delay(100);
  tone(5, hB, 90);
  delay(100);
  tone(5, hhC, 90);
  delay(100);
  tone(5, hB, 90);
  delay(100);
  tone(5, hAs, 90);
  delay(100);
  tone(5, hFs, 90);
  delay(100);
  tone(5, hC, 90);
  delay(100);
  tone(5, B, 90);
  delay(100);
  tone(5, As, 90);
  delay(100);
  tone(5, Fs, 90);
  delay(100);

  tone(5, hDs, 600);
  delay(600);
  tone(5, hhDs, 800);
  delay(800);
  tone(5, hhCs, 100);
  delay(100);
  tone(5, hhDs, 100);
  delay(100);
  tone(5, hhFs, 1600);
  delay(1600);

  tone(5, hB, 90);
  delay(100);
  tone(5, hAs, 90);
  delay(100);
  tone(5, hFs, 190);
  delay(200);
  tone(5, B, 190);
  delay(200);
  tone(5, hB, 90);
  delay(100);
  tone(5, hAs, 90);
  delay(100);
  tone(5, hFs, 190);
  delay(200);
  tone(5, B, 190);
  delay(200);
  tone(5, hB, 90);
  delay(100);
  tone(5, hAs, 90);
  delay(100);
  tone(5, hFs, 90);
  delay(100);
  tone(5, B, 90);
  delay(100);

  tone(5, hCs, 90);
  delay(101);
  tone(5, Fs, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hCs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hCs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);

  tone(5, hB, 400);
  delay(400);
  tone(5, B, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);
  tone(5, Fs, 200);
  delay(200);
  tone(5, hFs, 400);
  delay(400);

  tone(5, hCs, 200);
  delay(200);
  tone(5, B, 400);
  delay(400);
  tone(5, hFs, 400);
  delay(400);
  tone(5, B, 200);
  delay(200);
  tone(5, hCs, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);

  tone(5, hC, 90);
  delay(101);
  tone(5, B, 90);
  delay(101);
  tone(5, As, 190);
  delay(201);
  tone(5, Fs, 200);
  delay(201);
  tone(5, hCs, 400);
  delay(401);
  tone(5, hFs, 600);
  delay(600);
  tone(5, hhFs, 800);
  delay(800);

  tone(5, hhAs, 90);
  delay(101);
  tone(5, hhB, 90);
  delay(101);
  tone(5, hhAs, 90);
  delay(101);
  tone(5, hhFs, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hCs, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);

  tone(5, hDs, 195);
  delay(201);
  tone(5, hCs, 90);
  delay(101);
  tone(5, hDs, 90);
  delay(101);
  tone(5, hGs, 195);
  delay(201);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hGs, 90);
  delay(101);
  tone(5, hhDs, 195);
  delay(201);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hhDs, 90);
  delay(101);
  tone(5, hhGs, 200);
  delay(201);
  tone(5, hhDs, 200);
  delay(201);

  tone(5, hhB, 90);
  delay(101);
  tone(5, hhAs, 90);
  delay(101);
  tone(5, hhFs, 90);
  delay(101);
  tone(5, hhDs, 90);
  delay(101);
  tone(5, hhFs, 90);
  delay(101);
  tone(5, hhDs, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hDs, 90);
  delay(101);
  tone(5, B, 90);
  delay(101);

  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hCs, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hCs, 90);
  delay(101);
  tone(5, hhFs, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hFs, 90);
  delay(101);
  tone(5, hhAs, 90);
  delay(101);
  tone(5, hhFs, 90);
  delay(101);
  tone(5, hhCs, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);

  tone(5, hG, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, B, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hB, 90);
  delay(101);
  tone(5, hhG, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhD, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hDs, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);

  tone(5, hDs, 600);
  delay(600);
  tone(5, hD, 600);
  delay(600);
  tone(5, hDs, 400);
  delay(400);

  tone(5, As, 600);
  delay(600);
  tone(5, hAs, 600);
  delay(600);
  tone(5, hDs, 400);
  delay(400);

  tone(5, hF, 600);
  delay(600);
  tone(5, As, 600);
  delay(600);
  tone(5, hGs, 380);
  delay(400);

  tone(5, hGs, 400);
  delay(400);
  tone(5, hG, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);
  tone(5, hG, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhDs, 600);
  delay(600);
  tone(5, hhD, 400);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhC, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hG, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, As, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);

  tone(5, hF, 400);
  delay(400);
  tone(5, hG, 200);
  delay(200);
  tone(5, hF, 400);
  delay(400);
  tone(5, hDs, 380);
  delay(400);
  tone(5, hDs, 800);
  delay(800);

  tone(5, hAs, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hDs, 600);
  delay(600);
  tone(5, hD, 600);
  delay(600);
  tone(5, hDs, 400);
  delay(400);

  tone(5, As, 600);
  delay(600);
  tone(5, hAs, 600);
  delay(600);
  tone(5, hDs, 400);
  delay(400);

  tone(5, hF, 600);
  delay(600);
  tone(5, As, 600);
  delay(600);
  tone(5, hGs, 380);
  delay(400);

  tone(5, hGs, 400);
  delay(400);
  tone(5, hG, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);
  tone(5, hG, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhDs, 600);
  delay(600);
  tone(5, hhD, 400);
  delay(400);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhAs, 400);
  delay(400);

  tone(5, hhGs, 200);
  delay(200);
  tone(5, hhG, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);

  tone(5, hhF, 400);
  delay(400);
  tone(5, hhG, 200);
  delay(200);
  tone(5, hhF, 400);
  delay(400);
  tone(5, hhDs, 380);
  delay(400);
  tone(5, hhDs, 800);
  delay(800);

  tone(5, hAs, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);

  tone(5, hDs, 1200);
  delay(1200);
  tone(5, hAs, 400);
  delay(400);
  
  tone(5, hF, 600);
  delay(600);
  tone(5, hDs, 600);
  delay(600);
  tone(5, As, 400);
  delay(400);

  tone(5, hD, 600);
  delay(600);
  tone(5, hDs, 600);
  delay(600);
  tone(5, hF, 400);
  delay(400);

  tone(5, hGs, 800);
  delay(800);
  tone(5, hG, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhDs, 800);
  delay(800);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhC, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hG, 200);
  delay(200);
  tone(5, hDs, 400);
  delay(400);
  tone(5, As, 200);
  delay(200);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hGs, 400);
  delay(400);
  tone(5, hG, 400);
  delay(400);
  tone(5, hF, 400);
  delay(400);
  tone(5, hD, 400);
  delay(400);

  tone(5, hDs, 200);
  delay(200);
  tone(5, As, 200);
  delay(200);
  tone(5, hDs, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hGs, 200);
  delay(200);
  tone(5, hG, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);
  tone(5, hD, 200);
  delay(200);

  tone(5, hDs, 1200);
  delay(1200);
  tone(5, hAs, 400);
  delay(400);
  
  tone(5, hF, 600);
  delay(600);
  tone(5, hDs, 600);
  delay(600);
  tone(5, As, 400);
  delay(400);

  tone(5, hD, 600);
  delay(600);
  tone(5, hDs, 600);
  delay(600);
  tone(5, hF, 400);
  delay(400);

  tone(5, hGs, 800);
  delay(800);
  tone(5, hG, 400);
  delay(400);
  tone(5, hAs, 400);
  delay(400);

  tone(5, hhDs, 800);
  delay(800);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhAs, 400);
  delay(400);

  tone(5, hhGs, 200);
  delay(200);
  tone(5, hhGs, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 380);
  delay(400);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhAs, 400);
  delay(400);

  tone(5, hhGs, 200);
  delay(200);
  tone(5, hGs, 200);
  delay(200);
  tone(5, hhG, 200);
  delay(200);
  tone(5, hG, 200);
  delay(200);
  tone(5, hhF, 200);
  delay(200);
  tone(5, hF, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);
  tone(5, hD, 420);
  delay(200);

  tone(5, hhDs, 200);
  delay(200);
  tone(5, hAs, 200);
  delay(200);
  tone(5, hhDs, 200);
  delay(200);
  tone(5, hhAs, 200);
  delay(200);
  tone(5, hhGs, 200);
  delay(200);
  tone(5, hhG, 200);
  delay(200);
  tone(5, hhF, 200);
  delay(200);
  tone(5, hhD, 200);
  delay(200);

  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, F, 90);
  delay(101);
  tone(5, D, 90);
  delay(101);
  tone(5, F, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);

  tone(5, hF, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);

  tone(5, hG, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, E, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhG, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);

  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);

  tone(5, hF, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);

  tone(5, hhAs, 190);
  delay(201);
  tone(5, hhA, 45);
  delay(52);
  tone(5, hhAs, 45);
  delay(52);
  tone(5, hhA, 90);
  delay(101);
  tone(5, hhF, 190);
  delay(201);
  tone(5, hhE, 45);
  delay(52);
  tone(5, hhF, 45);
  delay(52);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 190);
  delay(201);
  tone(5, hA, 45);
  delay(52);
  tone(5, hAs, 45);
  delay(52);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);

  tone(5, hAs, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);

  tone(5, hF, 190);
  delay(201);
  tone(5, hhhC, 90);
  delay(101);
  tone(5, hhhD, 90);
  delay(101);
  tone(5, hhhC, 90);
  delay(101);
  tone(5, hhhD, 90);
  delay(101);
  tone(5, hhhC, 45);
  delay(52);
  tone(5, hhhD, 45);
  delay(52);
  tone(5, hhhC, 45);
  delay(52);
  tone(5, hhhD, 45);
  delay(52);
  tone(5, hhhC, 90);
  delay(101);
  tone(5, hhG, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 65);
  delay(72);
  tone(5, hE, 65);
  delay(72);
  tone(5, hC, 65);
  delay(72);
  tone(5, G, 65);
  delay(72);
  tone(5, hC, 65);
  delay(72);
  tone(5, hE, 65);
  delay(72);

  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, F, 90);
  delay(101);
  tone(5, D, 90);
  delay(101);
  tone(5, F, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);

  tone(5, hF, 90);
  delay(101);
  tone(5, As, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hD, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);

  tone(5, hG, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, E, 90);
  delay(101);
  tone(5, G, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhG, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);

  tone(5, hhAs, 190);
  delay(201);
  tone(5, hhA, 45);
  delay(52);
  tone(5, hhAs, 45);
  delay(52);
  tone(5, hhA, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hE, 90);
  delay(101);
  tone(5, hG, 90);
  delay(101);

  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hAs, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);

  tone(5, hhAs, 190);
  delay(201);
  tone(5, hhA, 45);
  delay(52);
  tone(5, hhAs, 45);
  delay(52);
  tone(5, hhA, 90);
  delay(101);
  tone(5, hhF, 190);
  delay(201);
  tone(5, hhE, 45);
  delay(52);
  tone(5, hhF, 45);
  delay(52);
  tone(5, hhE, 90);
  delay(101);
  tone(5, hhC, 190);
  delay(201);
  tone(5, hA, 45);
  delay(52);
  tone(5, hAs, 45);
  delay(52);
  tone(5, hA, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);

  tone(5, hAs, 190);
  delay(201);
  tone(5, hC, 190);
  delay(201);
  tone(5, hA, 190);
  delay(201);
  tone(5, hC, 190);
  delay(201);
  tone(5, hG, 190);
  delay(201);
  tone(5, hC, 190);
  delay(201);
  tone(5, hE, 190);
  delay(201);
  tone(5, hC, 190);
  delay(201);

  tone(5, hF, 90);
  delay(101);
  tone(5, F, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, A, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);
  tone(5, hC, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hhF, 90);
  delay(101);
  tone(5, hF, 90);
  delay(101);
  tone(5, hA, 90);
  delay(101);
  tone(5, hhC, 90);
  delay(101);

  tone(5, hhF, 1600);
  delay(1601);
}

진짜 깁니다.

 

이거 쓰느라 엄청 힘들었습니다 ㅠㅠ

 

저는 이렇게 노가다로 했는데

배열과 반복문을 이용해서 코드를 만들 수도 있다고 합니다.

 

int melody[] = {E, D, C, D, E, E, E, D, D, D, E, E, E};
int beat[] = {1000, 500,750, 750, 750, 750, 1000, 750, 750, 1000, 750, 750, 750}

을 한 다음

배열의 원소 개수 반복문을 반복시켜서 하면 되겠죠?

배열의 원소 개수는 배열의 총 크기를 배열의 원소 개수로 나누시면 됩니다.

 

int melody[] = {E, D, C, D, E, E, E, D, D, D, E, E, E};
int beat[] = {1000, 500,750, 750, 750, 750, 1000, 750, 750, 1000, 750, 750, 750};

for(int i = 0; i++; i < (sizeof(melody) / sizeof(melody[0]))) {
	tone(5, tone[i], beat[i]);
    delay(beat[i]);
}

 

이렇게 해 주면 더 간단한 코드를 만드실 수 있을 것 같습니다.

 

이번 글은 여기까지입니다.

제 글을 봐 주셔서 감사합니다. o((>ω< ))o