Arduino-BuzzerModule

Buzzer Module

参数

工作电压:3.3-5V

介绍:电磁式无源蜂鸣器,2K±5K方波驱动。

使用

接线

模块 Arduino
GND (Power) GND
VCC (Power) 3.3 / 5V
I/O (Digital PWM(~)) *

程序

动态内存优化方法

使用全局变量储存歌曲音符和时值,会造成动态内存占用率高,可能出现稳定性问题。

已掌握的解决方案有两种:

  1. 需要使用的时候用 new 动态分配内存,为每一个音符赋和时值赋值,用完后及时释放内存。

    1
    2
    3
    4
    5
    int *tune = new int[n];
    float *duration = new float[n];

    delete[] tune;
    delete[] duration;
  2. 这个静态数组放在MCU的FLASHROM中,而不存在RAM里。

    1
    2
    3
    4
    5
    #include <avr/pgmspace.h>
    const int tune[] PROGMEM = {.......};
    # PROGMEM 就是将数组放入闪存中
    pgm_read_byte(&tune[n]);
    # 读取数组

    但是在国内版的UNO上表现极不稳定,因此在本项目中不采用这个修改较简便的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//天空之城,共 156 个音符,pin = 9
#define NOTE_D0 -1
#define NOTE_D1 294
#define NOTE_D2 330
#define NOTE_D3 350
#define NOTE_D4 393
#define NOTE_D5 441
#define NOTE_D6 495
#define NOTE_D7 556

#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 589
#define NOTE_DH2 661
#define NOTE_DH3 700
#define NOTE_DH4 786
#define NOTE_DH5 882
#define NOTE_DH6 990
#define NOTE_DH7 112
//以上部分是定义是把每个音符和频率值对应起来

#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
//用英文对应了拍子


/*
** 一般用作全局变量,但是歌曲多的时候会造成动态内存占用过高,程序不稳定。
** 这时需要使用动态分配内存,再赋值,语句放入函数中。
int tune[] =
{
NOTE_D0,NOTE_D0,NOTE_D0,NOTE_D6,NOTE_D7, NOTE_DH1,NOTE_D7,NOTE_DH1,NOTE_DH3,NOTE_D7,
NOTE_D7,NOTE_D7,NOTE_D3,NOTE_D3,NOTE_D6, NOTE_D5,NOTE_D6,NOTE_DH1,NOTE_D5,NOTE_D5,
NOTE_D5,NOTE_D3,NOTE_D4,NOTE_D3,NOTE_D4, NOTE_DH1,NOTE_D3,NOTE_D3,NOTE_D0,NOTE_DH1,
NOTE_DH1,NOTE_DH1,NOTE_D7,NOTE_D4,NOTE_D4, NOTE_D7,NOTE_D7,NOTE_D7,NOTE_D0,NOTE_D6,
NOTE_D7,NOTE_DH1,NOTE_D7,NOTE_DH1,NOTE_DH3, NOTE_D7,NOTE_D7,NOTE_D7,NOTE_D3,NOTE_D3,
NOTE_D6,NOTE_D5,NOTE_D6,NOTE_DH1,NOTE_D5, NOTE_D5,NOTE_D5,NOTE_D2,NOTE_D3,NOTE_D4,
NOTE_DH1,NOTE_D7,NOTE_D7,NOTE_DH1,NOTE_DH1, NOTE_DH2,NOTE_DH2,NOTE_DH3,NOTE_DH1,NOTE_DH1,
NOTE_DH1,NOTE_DH1,NOTE_D7,NOTE_D6,NOTE_D6, NOTE_D7,NOTE_D5,NOTE_D6,NOTE_D6,NOTE_D6,
NOTE_DH1,NOTE_DH2,NOTE_DH3,NOTE_DH2,NOTE_DH3, NOTE_DH5,NOTE_DH2,NOTE_DH2,NOTE_DH2,NOTE_D5,
NOTE_D5,NOTE_DH1,NOTE_D7,NOTE_DH1,NOTE_DH3, NOTE_DH3,NOTE_DH3,NOTE_DH3,NOTE_DH3,NOTE_D6,
NOTE_D7,NOTE_DH1,NOTE_D7,NOTE_DH2,NOTE_DH2, NOTE_DH1,NOTE_D5,NOTE_D5,NOTE_D5,NOTE_DH4,
NOTE_DH3,NOTE_DH2,NOTE_DH1,NOTE_DH3,NOTE_DH3, NOTE_DH3,NOTE_DH3,NOTE_DH6,NOTE_DH6,NOTE_DH5,
NOTE_DH5,NOTE_DH3,NOTE_DH2,NOTE_DH1,NOTE_DH1, NOTE_D0,NOTE_DH1,NOTE_DH2,NOTE_DH1,NOTE_DH2,
NOTE_DH2,NOTE_DH5,NOTE_DH3,NOTE_DH3,NOTE_DH3, NOTE_DH3,NOTE_DH6,NOTE_DH6,NOTE_DH5,NOTE_DH5,
NOTE_DH3,NOTE_DH2,NOTE_DH1,NOTE_DH1,NOTE_D0, NOTE_DH1,NOTE_DH2,NOTE_DH1,NOTE_DH2,NOTE_DH2,
NOTE_D7,NOTE_D6,NOTE_D6,NOTE_D6,NOTE_D6, NOTE_D7
};
// 音符部分

float duration[]=

{
1,1,1,0.5,0.5, 1+0.5,0.5,1,1, 1,1,1,0.5,0.5,
1+0.5,0.5,1,1, 1,1,1,1, 1+0.5,0.5,1,1,
1,1,0.5,0.5,0.5,0.5, 1+0.5,0.5,1,1, 1,1,1,0.5,0.5,
1+0.5,0.5,1,1, 1,1,1,0.5,0.5, 1+0.5,0.5,1,1,
1,1,1,0.5,0.5, 1,0.5,0.25,0.25,0.25,0.5, 0.5,0.5,0.5,0.25,0.5,1,
0.5,0.5,0.5,0.5,1,1, 1,1,1,0.5,0.5, 1+0.5,0.5,1,1,
1,1,1,0.5,0.5, 1.5,0.5,1,1, 1,1,1,1,
0.5,0.5,1,1,0.5,0.5, 1.5,0.25,0.5,1, 1,1,1,1,
1,1,1,1, 1,1,1,1, 0.5,0.5,1,1,0.5,0.5,
1,0.5,0.5,1,1, 1,1,1,1, 1,1,1,1,
0.5,0.5,1,1,0.5,0.5, 1,0.5,0.25,0.5,1, 1,1,1,0.5,0.5
};
// 音符时值节拍部分
*/

int tonePin = 7; // 蜂鸣器的 pin

void setup()

{
Serial.begin(9600);
pinMode(tonePin, OUTPUT); // 设置蜂鸣器的pin为输出模式
Serial.println("Buzzer Test");
}

void loop()

{
// 音符部分
int *tune = new int[156];
tune[0]=tune[1]=tune[2]=NOTE_D0; tune[3]=NOTE_D6; tune[4]=NOTE_D7; tune[5]=NOTE_DH1; tune[6]=NOTE_D7; tune[7]=NOTE_DH1; tune[8]=NOTE_DH3;
tune[9]=tune[10]=tune[11]=NOTE_D7; tune[12]=tune[13]=NOTE_D3; tune[14]=NOTE_D6; tune[15]=NOTE_D5; tune[16]=NOTE_D6;tune[17]=NOTE_D1;
tune[18]=tune[19]=tune[20]=NOTE_D5; tune[21]=NOTE_D3; tune[22]=NOTE_D4; tune[23]=NOTE_D3; tune[24]=NOTE_D4;
tune[25]=NOTE_DH1; tune[26]=tune[27]=NOTE_D3; tune[28]=NOTE_D0; tune[29]=tune[30]=tune[31]=NOTE_DH1; tune[32]=NOTE_D7; tune[33]=tune[34]=NOTE_D4;
tune[35]=tune[36]=tune[37]=NOTE_D7; tune[38]=NOTE_D0; tune[39]=NOTE_D6; tune[40]=NOTE_D7; tune[41]=NOTE_DH1; tune[42]=NOTE_D7; tune[43]=NOTE_DH1; tune[44]=NOTE_DH3;
tune[45]=tune[46]=tune[47]=NOTE_D7; tune[48]=tune[49]=NOTE_D3; tune[50]=NOTE_D6; tune[51]=NOTE_D5; tune[52]=NOTE_D6; tune[53]=NOTE_DH1;
tune[54]=tune[55]=tune[56]=NOTE_D5; tune[57]=NOTE_D2; tune[58]=NOTE_D3; tune[59]=NOTE_D4; tune[60]=NOTE_DH1; tune[61]=tune[62]=NOTE_D7;
tune[63]=tune[64]=NOTE_DH1; tune[65]=tune[66]=NOTE_DH2; tune[67]=NOTE_DH3; tune[68]=tune[69]=tune[70]=NOTE_DH1;
tune[71]=NOTE_DH1; tune[72]=NOTE_D7; tune[73]=tune[74]=NOTE_D6; tune[75]=NOTE_D7; tune[76]=NOTE_D5; tune[77]=tune[78]=tune[79]=NOTE_D6;
tune[80]=NOTE_DH1; tune[81]=NOTE_DH2; tune[82]=NOTE_DH3; tune[83]=NOTE_DH2; tune[84]=NOTE_DH3; tune[85]=NOTE_DH5;
tune[86]=tune[87]=tune[88]=NOTE_DH2; tune[89]=tune[90]=NOTE_D5; tune[91]=NOTE_DH1; tune[92]=NOTE_D7; tune[93]=NOTE_DH1;
tune[94]=tune[95]=tune[96]=tune[97]=tune[98]=NOTE_DH3; tune[99]=NOTE_D6; tune[100]=NOTE_D7;
tune[101]=NOTE_DH1; tune[102]=NOTE_D7; tune[103]=tune[104]=NOTE_DH2; tune[105]=NOTE_DH1; tune[106]=tune[107]=tune[108]=NOTE_D5;
tune[109]=NOTE_DH4; tune[110]=NOTE_DH3; tune[111]=NOTE_DH2; tune[112]=NOTE_DH1; tune[113]=tune[114]=tune[115]=tune[116]=NOTE_DH3;
tune[117]=tune[118]=NOTE_DH6; tune[119]=tune[120]=NOTE_DH5; tune[121]=NOTE_DH3; tune[122]=NOTE_DH2; tune[123]=tune[124]=NOTE_DH1;
tune[125]=NOTE_D0; tune[126]=NOTE_DH1; tune[127]=NOTE_DH2; tune[128]=NOTE_DH1; tune[129]=tune[130]=NOTE_DH2;
tune[131]=NOTE_DH5; tune[132]=tune[133]=tune[134]=tune[135]=NOTE_DH3; tune[136]=tune[137]=NOTE_DH6; tune[138]=tune[139]=NOTE_DH5;
tune[140]=NOTE_DH3; tune[141]=NOTE_DH2; tune[142]=tune[143]=NOTE_DH1; tune[144]=NOTE_D0; tune[145]=NOTE_DH1; tune[146]=NOTE_DH2;
tune[147]=NOTE_DH1; tune[148]=tune[149]=NOTE_DH2; tune[150]=NOTE_D7; tune[151]=tune[152]=tune[153]=tune[154]=NOTE_D6; tune[155]=NOTE_D7;

// 音符时值部分
float *duration = new float[156];
duration[0]=duration[1]=duration[2]=1; duration[3]=duration[4]=0.5;
duration[5]=1.5; duration[6]=0.5; duration[7]=duration[8]=duration[9]=duration[10]=duration[11]=1; duration[12]=duration[13]=0.5;
duration[14]=1.5; duration[15]=0.5; duration[16]=duration[17]=duration[18]=duration[19]=duration[20]=duration[21]=1;
duration[22]=1.5; duration[23]=0.5; duration[24]=duration[25]=duration[26]=duration[27]=1;
duration[28]=duration[29]=duration[30]=duration[31]=0.5; duration[32]=1.5; duration[33]=0.5;
duration[34]=duration[35]=duration[36]=duration[37]=duration[38]=1; duration[39]=duration[40]=0.5;
duration[41]=1.5; duration[42]=0.5; duration[43]=duration[44]=duration[45]=duration[46]=duration[47]=1;
duration[48]=duration[49]=0.5; duration[50]=1.5; duration[51]=0.5; duration[52]=duration[53]=duration[54]=duration[55]=duration[56]=1;
duration[57]=duration[58]=0.5; duration[59]=1; duration[60]=0.5; duration[61]=duration[62]=duration[63]=0.25;
duration[64]=duration[65]=duration[66]=duration[67]=0.5; duration[68]=0.25; duration[69]=0.5; duration[70]=1;
duration[71]=duration[72]=duration[73]=duration[74]=0.5; duration[75]=duration[76]=duration[77]=duration[78]=duration[79]=1;
duration[80]=duration[81]=0.5; duration[82]=1.5; duration[83]=0.5; duration[84]=duration[85]=duration[86]=duration[87]=duration[88]=1;
duration[89]=duration[90]=0.5; duration[91]=1.5; duration[92]=0.5;
duration[93]=duration[94]=duration[95]=duration[96]=duration[97]=duration[98]=1; duration[99]=duration[100]=0.5;
duration[101]=duration[102]=1; duration[103]=duration[104]=0.5; duration[105]=1.5; duration[106]=0.25; duration[107]=0.5;
duration[108]=duration[109]=duration[110]=duration[111]=duration[112]=1;
duration[113]=duration[114]=duration[115]=duration[116]=duration[117]=duration[118]=duration[119]=duration[120]=1;
duration[121]=duration[122]=0.5; duration[123]=duration[124]=1; duration[125]=duration[126]=0.5; duration[127]=1;
duration[128]=duration[129]=0.5; duration[130]=duration[131]=duration[132]=duration[133]=duration[134]=duration[135]=1;
duration[136]=duration[137]=duration[138]=duration[139]=1; duration[140]=duration[141]=0.5; duration[142]=duration[143]=1;
duration[144]=duration[145]=0.5; duration[146]=1; duration[147]=0.5; duration[148]=0.25; duration[149]=0.5;
duration[150]=duration[151]=duration[152]=duration[153]=1; duration[154]=duration[155]=0.5;

for(int x=0; x<156; x++) // 循环每个音符
{
tone(tonePin,tune[x]); // 此函数依次播放每个音符
delay(400 * duration[x]); // 每个音符持续的时间
noTone(tonePin); // 停止当前音符,进入下一音符
}
delete[] tune;
delete[] duration;
delay(5000); // 等待5秒后,重新开始播放
}

内存优化

即在需要使用时动态分配内存,为每一个音符和音符时值赋值,用完后及时释放。

不使用内存优化,动态内存占用率可达 85%,使用后在编译时算出的动态内存占用率仅为 15%。当然,若使用时一股脑全部分配空寂爱你,也会达到 85%,甚至更高。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
void Musics()
{
Serial.println("Music Load Begin:");

//----------------------------------------------------------------------------------
int m = random(0, 4); //随机生成0,1,2,3五个数,用于选歌
Serial.println(m);
int length1 = 80; //《潮鸣》长度
int length2 = 156; //《天空之城》长度
int length3 = 25; //《祝你生日快乐》长度
if (m == 0) // m = 0,潮鸣
{
//-------------------------------潮鸣-------------------------------------
// 音符
int *tune1 = new int[80];
tune1[0]=G; tune1[1]=A; tune1[2]=EE; tune1[3]=A; tune1[4]=G; tune1[5]=A; tune1[6]=G; tune1[7]=A; tune1[8]=EE; tune1[9]=A; tune1[10]=G;
tune1[11]=A; tune1[12]=EE; tune1[13]=A; tune1[14]=G; tune1[15]=A; tune1[16]=E;
tune1[17]=G; tune1[18]=D; tune1[19]=E; tune1[20]=G; tune1[21]=A; tune1[22]=B;
tune1[23]=A; tune1[24]=EE; tune1[25]=A; tune1[26]=G; tune1[27]=A; tune1[28]=G;
tune1[29]=A; tune1[30]=EE; tune1[31]=B; tune1[32]=CC; tune1[33]=B; tune1[34]=CC; tune1[35]=B; tune1[36]=A; tune1[37]=E;
tune1[38]=D; tune1[39]=E; tune1[40]=G; tune1[41]=A; tune1[42]=B; tune1[43]=A; tune1[44]=EE; tune1[45]=A; tune1[46]=G; tune1[47]=A;
tune1[48]=G; tune1[49]=A; tune1[50]=EE; tune1[51]=A; tune1[52]=G; tune1[53]=A; tune1[54]=EE; tune1[55]=A; tune1[56]=G; tune1[57]=A;
tune1[58]=E; tune1[59]=G; tune1[60]=D; tune1[61]=E; tune1[62]=G; tune1[63]=A; tune1[64]=B; tune1[65]=A; tune1[66]=EE; tune1[67]=A; tune1[68]=G; tune1[69]=A;
tune1[70]=G; tune1[71]=A; tune1[72]=EE; tune1[73]=B; tune1[74]=CC; tune1[75]=B; tune1[76]=CC; tune1[77]=DD; tune1[78]=EE; tune1[79]=AA;
// 音符时值
float *duration1 = new float[80];
duration1[0]=2; duration1[1]=duration1[2]=duration1[3]=duration1[4]=1; duration1[5]=7; duration1[6]=duration1[7]=duration1[8]=duration1[9]=duration1[10]=1;
duration1[11]=duration1[12]=duration1[13]=duration1[14]=1; duration1[15]=3; duration1[16]=1;
duration1[17]=3; duration1[18]=duration1[19]=duration1[20]=duration1[21]=duration1[22]=1;
duration1[23]=duration1[24]=duration1[25]=duration1[26]=1; duration1[27]=7; duration1[28]=1;
duration1[29]=duration1[30]=duration1[31]=duration1[32]=duration1[33]=duration1[34]=duration1[35]=duration1[36]=1; duration1[37]=6;
duration1[38]=duration1[39]=duration1[40]=duration1[41]=duration1[42]=duration1[43]=duration1[44]=duration1[45]=duration1[46]=1; duration1[47]=7;
duration1[48]=duration1[49]=duration1[50]=duration1[51]=duration1[52]=duration1[53]=duration1[54]=duration1[55]=duration1[56]=1; duration1[57]=3;
duration1[58]=1; duration1[59]=3; duration1[60]=duration1[61]=duration1[62]=duration1[63]=duration1[64]=duration1[65]=duration1[66]=duration1[67]=duration1[68]=1; duration1[69]=7;
duration1[70]=duration1[71]=duration1[72]=duration1[73]=duration1[74]=duration1[75]=duration1[76]=duration1[77]=duration1[78]=1; duration1[79]=4;
Serial.println("1st Finished");
for (int i = 0; i < length1; i++)
{
tone(Buzzer, tune1[i]);
delay(250 * duration1[i]);
noTone(Buzzer);
delay(100);
}
delay(3000); //音乐结束,安静3s
delete[] tune1;
delete[] duration1;
}
else if (m == 1) // m = 1,天空之城
{
//------------------------------天空之城------------------------------------
// 音符部分
int *tune2 = new int[156];
tune2[0]=tune2[1]=tune2[2]=NOTE_D0; tune2[3]=NOTE_D6; tune2[4]=NOTE_D7; tune2[5]=NOTE_DH1; tune2[6]=NOTE_D7; tune2[7]=NOTE_DH1; tune2[8]=NOTE_DH3;
tune2[9]=tune2[10]=tune2[11]=NOTE_D7; tune2[12]=tune2[13]=NOTE_D3; tune2[14]=NOTE_D6; tune2[15]=NOTE_D5; tune2[16]=NOTE_D6;tune2[17]=NOTE_D1;
tune2[18]=tune2[19]=tune2[20]=NOTE_D5; tune2[21]=NOTE_D3; tune2[22]=NOTE_D4; tune2[23]=NOTE_D3; tune2[24]=NOTE_D4;
tune2[25]=NOTE_DH1; tune2[26]=tune2[27]=NOTE_D3; tune2[28]=NOTE_D0; tune2[29]=tune2[30]=tune2[31]=NOTE_DH1; tune2[32]=NOTE_D7; tune2[33]=tune2[34]=NOTE_D4;
tune2[35]=tune2[36]=tune2[37]=NOTE_D7; tune2[38]=NOTE_D0; tune2[39]=NOTE_D6; tune2[40]=NOTE_D7; tune2[41]=NOTE_DH1; tune2[42]=NOTE_D7; tune2[43]=NOTE_DH1; tune2[44]=NOTE_DH3;
tune2[45]=tune2[46]=tune2[47]=NOTE_D7; tune2[48]=tune2[49]=NOTE_D3; tune2[50]=NOTE_D6; tune2[51]=NOTE_D5; tune2[52]=NOTE_D6; tune2[53]=NOTE_DH1;
tune2[54]=tune2[55]=tune2[56]=NOTE_D5; tune2[57]=NOTE_D2; tune2[58]=NOTE_D3; tune2[59]=NOTE_D4; tune2[60]=NOTE_DH1; tune2[61]=tune2[62]=NOTE_D7;
tune2[63]=tune2[64]=NOTE_DH1; tune2[65]=tune2[66]=NOTE_DH2; tune2[67]=NOTE_DH3; tune2[68]=tune2[69]=tune2[70]=NOTE_DH1;
tune2[71]=NOTE_DH1; tune2[72]=NOTE_D7; tune2[73]=tune2[74]=NOTE_D6; tune2[75]=NOTE_D7; tune2[76]=NOTE_D5; tune2[77]=tune2[78]=tune2[79]=NOTE_D6;
tune2[80]=NOTE_DH1; tune2[81]=NOTE_DH2; tune2[82]=NOTE_DH3; tune2[83]=NOTE_DH2; tune2[84]=NOTE_DH3; tune2[85]=NOTE_DH5;
tune2[86]=tune2[87]=tune2[88]=NOTE_DH2; tune2[89]=tune2[90]=NOTE_D5; tune2[91]=NOTE_DH1; tune2[92]=NOTE_D7; tune2[93]=NOTE_DH1;
tune2[94]=tune2[95]=tune2[96]=tune2[97]=tune2[98]=NOTE_DH3; tune2[99]=NOTE_D6; tune2[100]=NOTE_D7;
tune2[101]=NOTE_DH1; tune2[102]=NOTE_D7; tune2[103]=tune2[104]=NOTE_DH2; tune2[105]=NOTE_DH1; tune2[106]=tune2[107]=tune2[108]=NOTE_D5;
tune2[109]=NOTE_DH4; tune2[110]=NOTE_DH3; tune2[111]=NOTE_DH2; tune2[112]=NOTE_DH1; tune2[113]=tune2[114]=tune2[115]=tune2[116]=NOTE_DH3;
tune2[117]=tune2[118]=NOTE_DH6; tune2[119]=tune2[120]=NOTE_DH5; tune2[121]=NOTE_DH3; tune2[122]=NOTE_DH2; tune2[123]=tune2[124]=NOTE_DH1;
tune2[125]=NOTE_D0; tune2[126]=NOTE_DH1; tune2[127]=NOTE_DH2; tune2[128]=NOTE_DH1; tune2[129]=tune2[130]=NOTE_DH2;
tune2[131]=NOTE_DH5; tune2[132]=tune2[133]=tune2[134]=tune2[135]=NOTE_DH3; tune2[136]=tune2[137]=NOTE_DH6; tune2[138]=tune2[139]=NOTE_DH5;
tune2[140]=NOTE_DH3; tune2[141]=NOTE_DH2; tune2[142]=tune2[143]=NOTE_DH1; tune2[144]=NOTE_D0; tune2[145]=NOTE_DH1; tune2[146]=NOTE_DH2;
tune2[147]=NOTE_DH1; tune2[148]=tune2[149]=NOTE_DH2; tune2[150]=NOTE_D7; tune2[151]=tune2[152]=tune2[153]=tune2[154]=NOTE_D6; tune2[155]=NOTE_D7;

// 音符时值部分
float *duration2 = new float[156];
duration2[0]=duration2[1]=duration2[2]=1; duration2[3]=duration2[4]=0.5;
duration2[5]=1.5; duration2[6]=0.5; duration2[7]=duration2[8]=duration2[9]=duration2[10]=duration2[11]=1; duration2[12]=duration2[13]=0.5;
duration2[14]=1.5; duration2[15]=0.5; duration2[16]=duration2[17]=duration2[18]=duration2[19]=duration2[20]=duration2[21]=1;
duration2[22]=1.5; duration2[23]=0.5; duration2[24]=duration2[25]=duration2[26]=duration2[27]=1;
duration2[28]=duration2[29]=duration2[30]=duration2[31]=0.5; duration2[32]=1.5; duration2[33]=0.5;
duration2[34]=duration2[35]=duration2[36]=duration2[37]=duration2[38]=1; duration2[39]=duration2[40]=0.5;
duration2[41]=1.5; duration2[42]=0.5; duration2[43]=duration2[44]=duration2[45]=duration2[46]=duration2[47]=1;
duration2[48]=duration2[49]=0.5; duration2[50]=1.5; duration2[51]=0.5; duration2[52]=duration2[53]=duration2[54]=duration2[55]=duration2[56]=1;
duration2[57]=duration2[58]=0.5; duration2[59]=1; duration2[60]=0.5; duration2[61]=duration2[62]=duration2[63]=0.25;
duration2[64]=duration2[65]=duration2[66]=duration2[67]=0.5; duration2[68]=0.25; duration2[69]=0.5; duration2[70]=1;
duration2[71]=duration2[72]=duration2[73]=duration2[74]=0.5; duration2[75]=duration2[76]=duration2[77]=duration2[78]=duration2[79]=1;
duration2[80]=duration2[81]=0.5; duration2[82]=1.5; duration2[83]=0.5; duration2[84]=duration2[85]=duration2[86]=duration2[87]=duration2[88]=1;
duration2[89]=duration2[90]=0.5; duration2[91]=1.5; duration2[92]=0.5;
duration2[93]=duration2[94]=duration2[95]=duration2[96]=duration2[97]=duration2[98]=1; duration2[99]=duration2[100]=0.5;
duration2[101]=duration2[102]=1; duration2[103]=duration2[104]=0.5; duration2[105]=1.5; duration2[106]=0.25; duration2[107]=0.5;
duration2[108]=duration2[109]=duration2[110]=duration2[111]=duration2[112]=1;
duration2[113]=duration2[114]=duration2[115]=duration2[116]=duration2[117]=duration2[118]=duration2[119]=duration2[120]=1;
duration2[121]=duration2[122]=0.5; duration2[123]=duration2[124]=1; duration2[125]=duration2[126]=0.5; duration2[127]=1;
duration2[128]=duration2[129]=0.5; duration2[130]=duration2[131]=duration2[132]=duration2[133]=duration2[134]=duration2[135]=1;
duration2[136]=duration2[137]=duration2[138]=duration2[139]=1; duration2[140]=duration2[141]=0.5; duration2[142]=duration2[143]=1;
duration2[144]=duration2[145]=0.5; duration2[146]=1; duration2[147]=0.5; duration2[148]=0.25; duration2[149]=0.5;
duration2[150]=duration2[151]=duration2[152]=duration2[153]=1; duration2[154]=duration2[155]=0.5;
Serial.println("2ed Finished");
for (int x = 0; x < length2; x++) //循环音符的次数
{
tone(Buzzer, tune2[x]); //此函数依次播放tune序列里的数组,即每个 音符
delay(400 * duration2[x]); //每个音符持续的时间,即节拍duration,是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
noTone(Buzzer); //停止当前音符,进入下一音符
}
delete[] tune2;
delete[] duration2;
delay(3000); //音乐结束,安静3秒
}
else if (m == 2) // m = 2,祝你生日快乐
{
//-----------------------------生日快乐----------------------------------------
// 音符
int *tune3 = new int[25];
tune3[0]=tune3[1]=Sol; tune3[2]=La; tune3[3]=Sol; tune3[4]=Do_h; tune3[5]=Si;
tune3[6]=tune3[7]=Sol; tune3[8]=La; tune3[9]=Sol; tune3[10]=Re_h; tune3[11]=Do_h;
tune3[12]=tune3[13]=Sol; tune3[14]=Sol_h; tune3[15]=Mi_h; tune3[16]=Do_h; tune3[17]=Si; tune3[18]=La;
tune3[19]=tune3[20]=Fa_h; tune3[21]=Mi_h; tune3[22]=Do_h; tune3[23]=Re_h; tune3[24]=Do_h;
// 音符时值
float *duration3 = new float[25];
duration3[0]=duration3[1]=0.5; duration3[2]=duration3[3]=duration3[4]=1; duration3[5]=2;
duration3[6]=duration3[7]=0.5; duration3[8]=duration3[9]=duration3[10]=1; duration3[11]=2;
duration3[12]=duration3[13]=0.5; duration3[14]=duration3[15]=duration3[16]=duration3[17]=duration3[18]=1;
duration3[19]=duration3[20]=0.5; duration3[21]=duration3[22]=duration3[23]=1; duration3[24]=2;
Serial.println("3rd Finished");
for (int x = 0; x < length3; x++)
{
tone(Buzzer, tune3[x]);
delay(500 * duration3[x]); //用来根据节拍调节延时,500这个指数可以自己调整
delay(100 * duration3[x]);
noTone(Buzzer);
}
delay(3000); //音乐结束,安静3秒
delete[] tune3;
delete[] duration3;
}
else if (m == 3) //hiahiahia,没有音乐
{
delay(3000); //音乐结束,安静3秒
}
}