+극을 10 디지털핀에 반대편을 gnd로 연결
#include<TimerOne.h>
const int BUZZER = 10;
const int melody[]={
//262, 294,330,349,393,440,494,523 //도레미파솔라시도
262,330,393,262,330,393,440,440,440,393,349,349,349,330,330,330,294,294,294,262
};
void setup() {
// put your setup code here, to run once:
Timer1.initialize();
Timer1.pwm(BUZZER, 0);
Timer1.setPwmDuty(BUZZER, 100);
for(int note=0; note< ( sizeof(melody) / sizeof(melody[0]) ); note++){
Timer1.setPeriod(1000000/melody[note]);
delay(500);
}
Timer1.disablePwm(BUZZER);
}
void loop() {
// put your main code here, to run repeatedly:
}
여기서 melody배열의 len값은
sizeof(melody) / sizeof(melody[0])
이다
sizeof(melody)는 melody 배열의 전체 크기를 바이트 단위로 반환하며, sizeof(melody[0])은 배열의 첫 번째 원소인 정수형 변수의 크기를 바이트 단위로 반환합니다. 따라서 sizeof(melody) / sizeof(melody[0])은 배열의 길이가 됩니다.
정보가 많아서 도움이 많이 됐습니다.