Arduino蜂鸣器演奏《太阳照常升起》

// 用到的音符乐音频率(C大调)
#define NOTE_A1 221
#define NOTE_B1 248
#define NOTE_C2 262
#define NOTE_D2 294
#define NOTE_E2 330
#define NOTE_F2 350
#define NOTE_G2 393
#define NOTE_A2 441
#define NOTE_B2 495

//音调数组
int tune[]=
{
  NOTE_A1,NOTE_E2,NOTE_E2,NOTE_E2,NOTE_F2,NOTE_E2,NOTE_E2,NOTE_F2,NOTE_G2,NOTE_A2,NOTE_A2,NOTE_G2,NOTE_G2, NOTE_E2,
  NOTE_A1,NOTE_D2,NOTE_D2,NOTE_D2,NOTE_E2,NOTE_D2,NOTE_E2,NOTE_G2,NOTE_E2,NOTE_G2,NOTE_B1,NOTE_C2,NOTE_A1,
  NOTE_A1,NOTE_C2,NOTE_E2,NOTE_A2,NOTE_G2,NOTE_A2, NOTE_G2,NOTE_A2,NOTE_G2,NOTE_A2,NOTE_G2,NOTE_G2,NOTE_E2,
  NOTE_D2,NOTE_A2,NOTE_G2,NOTE_E2,NOTE_D2,NOTE_E2,
  NOTE_D2,NOTE_A2,NOTE_F2,NOTE_E2,NOTE_D2,NOTE_E2,NOTE_G2,NOTE_B1,NOTE_C2,NOTE_A1
};

//时长数组
float durt[]=
{
 1.0,1.0,1.0,0.75,0.25,2.5,0.5,0.5,0.5,1.5,0.5,1.0,1.0,4.0,
 1.0,1.0,1.0,0.75,0.25,2.0,2.0,0.75,0.25,2.0,0.75,0.25,5.0,
 1.0,1.0,1.0,0.75,0.25,2.75,0.25,0.75,0.25,2.0,0.75,0.25,5.0,
 1.0,1.0,1.0,0.75,0.25,4.0,
 1.0,1.0,1.0,0.75,0.25,1.0,1.0,0.75,0.25,4.0
};
int length;
int tonepin=6; 

//初始化:设置输出端口
void setup()
{
  pinMode(6,OUTPUT);
  length=sizeof(tune)/sizeof(tune[0]);
}
//循环播放
void loop()
{
  for(int i=0;i
int tune[]=
{
  /*C2,D2,E2,E2,E2,E2,C3,E2,E2,E2,E2,F2,D2,
  B1,C2,D2,D2,D2,D2,F2,F2,E2,D2,C2,
  F2,G2,A2,A2,A2,A2,G2,F2,E2,D2,F2,E2,
  F2,G2,A2,A2,A2,A2,A2,G2,A2,B2,
  E2,C3,C3,C3,C3,C3,B2,B2,D3,D3,C3,B2,B2,C3,A2,A2,A2,A2,A2,G2,C3,E3,E3,D3,E3,F3,E3, */
  E2,C3,C3,C3,C3,C3,B2,B2,D3,D3,C3,B2,B2,C3,A2,A2,A2,G2,F2,E2,A2,C3,E2,E2,G2,B2,G2,A2
};

//时长数组
float durt[]=
{
 /*0.5,0.5,1,1,1,1,1.25,0.75,1,1,0.5,0.5,5,
 0.5,0.5,1,1,1,1,3,1,1,1,6,
 0.5,0.5,1,1,1,1,2,2,1,1,1,5,
 0.5,0.5,1,1,1,1,4,1,1,6,
 1,1,1,1,2,1,1,1,1,1,1,1.25,0.75,1,1,1,1,1,1,1.25,0.75,1,1,2.5,0.5,0.5,2, */
 1,1,1,1,2,1,1,1,1,1,1,1.25,0.75,1,1,1,1,1,1,0.5,0.5,2,1,1,1,1,1,5
};
int length;
int tonepin=6; 

//初始化:设置输出端口
void setup()
{
  pinMode(6,OUTPUT);
  length=sizeof(tune)/sizeof(tune[0]);
}
//循环播放
void loop()
{
  for(int i=0;i

硬件连接:只需将蜂鸣器直接连在端口上即可(可加电阻以调节声音大小)

你可能感兴趣的:(c语言)