Post by 卢占卡 on Feb 10, 2021 14:04:53 GMT
一.接线
IN接D9,VCC接5V,GND接GND。
二.程序
第一首:葫芦娃
IN接D9,VCC接5V,GND接GND。
二.程序
第一首:葫芦娃
#define NOTE_D0 -1
#define NOTE_D1 262
#define NOTE_D2 293
#define NOTE_D3 329
#define NOTE_D4 349
#define NOTE_D5 392
#define NOTE_D6 440
#define NOTE_D7 494
#define NOTE_DL1 147
#define NOTE_DL2 165
#define NOTE_DL3 175
#define NOTE_DL4 196
#define NOTE_DL5 221
#define NOTE_DL6 248
#define NOTE_DL7 278
#define NOTE_DH1 523
#define NOTE_DH2 586
#define NOTE_DH3 658
#define NOTE_DH4 697
#define NOTE_DH5 783
#define NOTE_DH6 879
#define NOTE_DH7 987
//以上部分是定义是把每个音符和频率值对应起来,其实不用打这么多,但是都打上了,后面可以随意编写D调的各种歌,我这里用NOTE_D+数字表示音符,NOTE_DH+数字表示上面有点的那种音符,NOTE_DL+数字表示下面有点的那种音符。这样后面写起来比较好识别。
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
//这部分是用英文对应了拍子,这样后面也比较好看
int tune[] =
{
NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_D6,NOTE_D0,
NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_DH1,NOTE_D6,NOTE_D0,NOTE_D6,
NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,NOTE_D0,NOTE_D6,
NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_DH1,NOTE_D6,NOTE_D0,
NOTE_D1,NOTE_D1,NOTE_D3,
NOTE_D1,NOTE_D1,NOTE_D3,NOTE_D0,
NOTE_D6,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,
NOTE_D5,NOTE_D1,NOTE_D3,NOTE_D0,
NOTE_DH1,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,
NOTE_D5,NOTE_D1,NOTE_D2,NOTE_D0,
NOTE_D7,NOTE_D7,NOTE_D5,NOTE_D3,
NOTE_D5,
NOTE_DH1,NOTE_D0,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D5,NOTE_D6,NOTE_D6,
NOTE_D0,NOTE_D5,NOTE_D1,NOTE_D3,NOTE_D0,
NOTE_DH1,NOTE_D0,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D5,NOTE_D6,NOTE_D6,
NOTE_D0,NOTE_D5,NOTE_D1,NOTE_D2,NOTE_D0,
NOTE_D3,NOTE_D3,NOTE_D1,NOTE_DL6,
NOTE_D1,
NOTE_D3,NOTE_D5,NOTE_D6,NOTE_D6,
NOTE_D3,NOTE_D5,NOTE_D6,NOTE_D6,
NOTE_DH1,NOTE_D0,NOTE_D7,NOTE_D5,
NOTE_D6,
};//这部分就是整首曲子的音符部分,用了一个序列定义为tune,整数
float duration[]=
{
1,1,0.5,0.5,1,
0.5,0.5,0.5,0.5,1,0.5,0.5,
0.5,1,0.5,1,0.5,0.5,
0.5,0.5,0.5,0.5,1,1,
1,1,1+1,
0.5,1,1+0.5,1,
1,1,0.5,0.5,1,
0.5,1,1+0.5,1,
0.5,0.5,0.5,0.5,1+1,
0.5,1,1+0.5,1,
1+1,0.5,0.5,1,
1+1+1+1,
0.5,0.5,0.5+0.25,0.25,0.5+0.25,0.25,0.5+0.25,0.25,
0.5,1,0.5,1,1,
0.5,0.5,0.5+0.25,0.25,0.5+0.25,0.25,0.5+0.25,0.25,
0.5,1,0.5,1,1,
1+1,0.5,0.5,1,
1+1+1+1,
0.5,1,0.5,1+1,
0.5,1,0.5,1+1,
1+1,0.5,0.5,1,
1+1+1+1
};//这部分是整首曲子的接拍部分,也定义个序列duration,浮点(数组的个数和前面音符的个数是一样的,一一对应么)
int length;//这里定义一个变量,后面用来表示共有多少个音符
int tonePin=9;//蜂鸣器的pin
void setup() {
// put your setup code here, to run once:
pinMode(tonePin,OUTPUT);//设置蜂鸣器的pin为输出模式
length = sizeof(tune)/sizeof(tune[0]);//这里用了一个sizeof函数,可以查出tone序列里有多少个音符
}
void loop() {
for(int x=0;x<length;x++)
{
tone(tonePin,tune[x]);//此函数依次播放tune序列里的数组,即每个音符
delay(400*duration[x]);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
noTone(tonePin);//停止当前音符,进入下一音符
}
delay(5000);//等待5秒后,循环重新开始
}
第二首:黑人抬棺#define NOTE_F1 350
#define NOTE_F2 393
#define NOTE_F3 441
#define NOTE_F4 495
#define NOTE_F5 556
#define NOTE_F6 624
#define NOTE_F7 661
#define NOTE_0 0
#define NOTE_FL1 175
#define NOTE_FL2 196
#define NOTE_FL3 221
#define NOTE_FL4 234
#define NOTE_FL5 262
#define NOTE_FL6 294
#define NOTE_FL7 330
#define NOTE_FH1 700
#define NOTE_FH2 786
#define NOTE_FH3 882
#define NOTE_FH4 935
#define NOTE_FH5 1049
#define NOTE_FH6 1178
#define NOTE_FH7 1322
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
int tune[] = //简谱频率
{
NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F6,NOTE_F6,NOTE_F6,NOTE_F6,
NOTE_F5,NOTE_F5,NOTE_F5,NOTE_F5,NOTE_FH1,NOTE_FH1,NOTE_FH1,NOTE_FH1,
NOTE_FH2,NOTE_FH2,NOTE_FH2,NOTE_FH2,NOTE_FH2,NOTE_FH2,NOTE_FH2,NOTE_FH2,
NOTE_F5,NOTE_F4,NOTE_F3,NOTE_F1,NOTE_F2,NOTE_0,NOTE_F2,NOTE_F6,
NOTE_F5,NOTE_0,NOTE_F4,NOTE_0,NOTE_F3,NOTE_0,NOTE_F3,NOTE_F3,NOTE_F5,NOTE_0,NOTE_F4,NOTE_F3,NOTE_F2,NOTE_0,NOTE_F2,NOTE_FH4,
NOTE_FH3, NOTE_FH4, NOTE_FH3, NOTE_FH4, NOTE_F2, NOTE_0,NOTE_F2,NOTE_FH4, NOTE_FH3, NOTE_FH4, NOTE_FH3, NOTE_FH4, NOTE_F2, NOTE_0,NOTE_F2,NOTE_F6,
NOTE_F5,NOTE_0,NOTE_F4,NOTE_0,NOTE_F3,NOTE_0,NOTE_F3,NOTE_F3,NOTE_F5,NOTE_0,NOTE_F4,NOTE_F3,NOTE_F2,NOTE_0,NOTE_F2,NOTE_FH4,
NOTE_FH3, NOTE_FH4, NOTE_FH3, NOTE_FH4, NOTE_F2, NOTE_0,NOTE_F2,NOTE_FH4,NOTE_FH3, NOTE_FH4, NOTE_FH3, NOTE_FH4,
};
float durt[]= //根据简谱列出各节拍
{
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5
};
int length;//这里定义一个变量,后面用来表示共有多少个音符
int tonepin=9;//蜂鸣器的pin
void setup() {
// put your setup code here, to run once:
pinMode(tonepin,OUTPUT);//设置蜂鸣器的pin为输出模式
length = sizeof(tune)/sizeof(tune[0]);//这里用了一个sizeof函数,可以查出tone序列里有多少个音符
}
void loop() {
// put your main code here, to run repeatedly:
for(int x=0;x<length;x++)
{
tone(tonepin,tune[x]);
delay(500*durt[x]); //这里用来根据节拍调节延时,500这个指数可以自己调整,在该音乐中,我发现用500比较合适。
noTone(tonepin);
}
}