Play a Melody using the tone() function
- int buzzer=8;
- // C D E F G A B
- //音階頻率 Do Re Mi Fa So La Si
- //int freq[]={262,294,330,349,392,440,494};
- int freq[]={523,587,659,698,784,880,988};
- //int freq[]={1046,1175,1318,1397,1568,1760,1976};
- //歌曲音調(小蜜蜂)
- int melody[]={5,3,3,4,2,2,1,2,3,4,5,5,5,5,3,3,4,2,2,1,3,5,5,3,2,2,2,2,2,3,4,3,3,3,3,3,4,5,5,3,3,4,2,2,1,3,5,5,1};
- //節拍(小蜜蜂)
- int beat[]={1,1,2,1,1,2,1,1,1,1,1,1,2,1,1,2,1,1,2,1,1,1,1,4,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,2,1,1,2,1,1,1,1,4};
- void setup(){
- pinMode(buzzer,OUTPUT);
- }
- void loop(){
- for(int i=0; i<49; i++){
- tone(buzzer, freq[melody[i]-1]);
- delay(beat[i]*320);
- noTone(buzzer);
- }
- delay(1000);
- }
複製代碼 |