利用tone函数与蜂鸣器播放时不能播放多首曲子-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4182|回复: 3

[已解决] 利用tone函数与蜂鸣器播放时不能播放多首曲子

[复制链接]
发表于 2014-5-9 22:17 | 显示全部楼层 |阅读模式
前几天一直期中考试,今天考完才看到回帖中提到要把数组写到flash中,果真有效,在此谢过!!

#include <avr/pgmspace.h>
#define NOTE_G0 -1
#define NOTE_GL1 196
#define NOTE_GL2 221
#define NOTE_GL3 234
#define NOTE_GL4 262
#define NOTE_GL5 294
#define NOTE_GL6 330
#define NOTE_GL7 371

#define NOTE_G1 393
#define NOTE_G2 441
#define NOTE_G3 495
#define NOTE_G4 556
#define NOTE_G5 624
#define NOTE_G6 661
#define NOTE_G7 742

#define NOTE_GH1 700
#define NOTE_GH2 786
#define NOTE_GH3 882
#define NOTE_GH4 935
#define NOTE_GH5 1049
#define NOTE_GH6 1178
#define NOTE_GH7 1322

#define NOTE_E0 -1
#define NOTE_E1 330
#define NOTE_E7 624
#define NOTE_E2 350
#define NOTE_E3 393
#define NOTE_E4 441
#define NOTE_E5 495
#define NOTE_E6 556

#define NOTE_EL7 312
#define NOTE_EL1 165
#define NOTE_EL2 175
#define NOTE_EL3 196
#define NOTE_EL4 221
#define NOTE_EL5 248
#define NOTE_EL6 278

#define NOTE_EH7 1248
#define NOTE_EH1 661
#define NOTE_EH2 700
#define NOTE_EH3 786
#define NOTE_EH4 882
#define NOTE_EH5 990
#define NOTE_EH6 1112

#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 1112

//以上部分是定义是把每个音符和频率值对应起来,其实不用打这么多,但是都打上了,后面可以随意编写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
//这部分是用英文对应了拍子,这样后面也比较好看
PROGMEM int tune1[] =
{
  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,整数

PROGMEM float duration1[]=
{
  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,浮点(数组的个数和前面音符的个数是一样的,一一对应么)

PROGMEM int tune3[] =
{
NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_D6,NOTE_DH1, NOTE_DH1,NOTE_DH1,
  NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_DH2,NOTE_DH3,
NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_D6,NOTE_DH1, NOTE_DH1,NOTE_DH1,
  NOTE_D6,NOTE_DH1,NOTE_DH1,NOTE_DH1,NOTE_D5,NOTE_DH1, NOTE_DH1,NOTE_D7,

  NOTE_D1,NOTE_DL5,NOTE_D1,NOTE_D2,NOTE_D3, NOTE_D1,NOTE_DL6,NOTE_DL5,
  NOTE_DL4,NOTE_DL5,NOTE_DL6,NOTE_D3,NOTE_D2,
NOTE_D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TE_D1,


};//这部分就是整首曲子的音符部分,用了一个序列定义为tune,整数

PROGMEM float duration3[]=
{
  0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
  0.5,0.5,0.5,0.5,1,1,
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,2,
   0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
   0.5,0.5,0.5,0.5,2,

  0.5,0.5,0.5,0.5,1,1,
   0.5,0.5,0.5,0.5,2,
   0.5,0.5,0.5,0.5,1,1,
   0.5,0.5,0.5,0.5,2,

    1,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,
    1,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,

    1,0.5,0.5,0.5,0.5,0.5,0.5,
    0.5,0.5,0.5,0.5,0.5,0.5,1,
    1,0.5,0.5,0.5,0.5,0.5,0.5,
    0.5,0.5,0.5,0.5,0.5,0.5,1,

    1,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,
    1,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,
};

PROGMEM int tune2[] =
{
  NOTE_DL5,
  NOTE_D1,NOTE_D3,NOTE_D5,NOTE_D1,

  NOTE_DL7,NOTE_DH3,NOTE_D5,NOTE_D0,NOTE_D5,
  NOTE_D6,NOTE_D7,NOTE_DH1,NOTE_D6,
  NOTE_D5,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_D3,

  NOTE_D2,NOTE_D1,NOTE_DL6,NOTE_D2,NOTE_D3,
  NOTE_D2,NOTE_D0,NOTE_D5,
  NOTE_D1,NOTE_D3,NOTE_D5,NOTE_D1,
  NOTE_DL7,NOTE_D3,NOTE_D5,NOTE_D0,NOTE_D5,
  NOTE_D6,NOTE_D7,NOTE_DH1,NOTE_D6,

  NOTE_D5,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D3,NOTE_DH2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_D3,
  NOTE_D2,NOTE_D6,NOTE_D7,NOTE_D1,NOTE_D2,
  NOTE_D1,NOTE_D3,NOTE_D5,

  NOTE_D3,NOTE_D2,NOTE_D1,NOTE_D5,
  NOTE_DL7,NOTE_DL6,NOTE_DL7,
  NOTE_DL6,NOTE_DL7,NOTE_DL6,NOTE_DL5,
  NOTE_D3,NOTE_D5,
  NOTE_D3,NOTE_D2,NOTE_D1,NOTE_D5,
  NOTE_DL7,NOTE_DL6,NOTE_DL7,

  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_DH3,
  NOTE_D2,NOTE_D0,NOTE_D5,
  NOTE_D1,NOTE_D3,NOTE_D5,NOTE_D1,
  NOTE_D7,NOTE_D3,NOTE_D5,NOTE_D0,NOTE_D5,
  NOTE_D6,NOTE_D7,NOTE_DH1,NOTE_D6,

  NOTE_D5,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_D3,
  NOTE_D2,NOTE_DL6,NOTE_DL7,NOTE_D1,NOTE_D2,
  NOTE_D1,NOTE_D3,NOTE_D2,
  NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_D3,
  NOTE_D2,NOTE_DL6,NOTE_DL7,NOTE_D1,NOTE_D2,
  NOTE_D1,



};//这部分就是整首曲子的音符部分,用了一个序列定义为tune,整数

PROGMEM float duration2[]=
{
0.5,
  1.5,0.5,1.5,0.5,

  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1.5,1,
  2.5,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,

  1.5,0.5,1,0.5,0.5,
  3,0.5,0.5,
  1.5,0.5,1.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1.5,1,

  2.5,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  3,0.5,0.5,

  1.5,0.5,1,1,
  3,0.5,0.5,
  1.5,0.5,1,1,
  3,1,
  1.5,0.5,1,1,
  3,0.5,0.5,

  1.5,0.5,1,0.5,0.5,
  3,0.5,0.5,  
  1.5,0.5,1.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1.5,1,

  2.5,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  3,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  1.5,0.5,1,0.5,0.5,
  5,  


};

int length1 = 98;
int length2 = 142;
int length3 = 166;  
int tonePin=5;//蜂鸣器的pin

void setup()
{
  pinMode(tonePin,OUTPUT);//设置蜂鸣器的pin为输出模式  
  pinMode(14,INPUT);
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  randomSeed(analogRead(14));
}

void yueliang()
{  
  int tune;
  float duration;
  for(int x=0;x<length2;x++)//循环音符的次数
  {
    tune=pgm_read_word(&(tune2[x]));
    duration=pgm_read_float(&(duration2[x]));
    tone(tonePin,tune);//此函数依次播放tune序列里的数组,即每个音符
    delay(600*duration);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
    noTone(tonePin);//停止当前音符,进入下一音符
  }
}

void huluwa()
{
  int tune;
  float duration;
  for(int x=0;x<length1;x++)//循环音符的次数
  {
    tune=pgm_read_word(&(tune1[x]));
    duration=pgm_read_float(&(duration1[x]));
    tone(tonePin,tune);//此函数依次播放tune序列里的数组,即每个音符
    delay(400*duration);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
    noTone(tonePin);//停止当前音符,进入下一音符
  }
}

void ailala()
{
  int tune;
  float duration;
  for(int x=0;x<length3;x++)//循环音符的次数
  {   
    tune=pgm_read_word(&(tune3[x]));
    duration=pgm_read_float(&(duration3[x]));
    tone(tonePin,tune);//此函数依次播放tune序列里的数组,即每个音符
    delay(480*duration);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
    noTone(tonePin);//停止当前音符,进入下一音符
  }
}


void loop()
{  
   int y=random(1,4);
   if(y==1)
       yueliang();
   else if(y==2)
       ailala ();
   else if(y==3)
     huluwa();

   delay(3000);
}

发表于 2014-5-9 23:34 | 显示全部楼层
{:soso_e179:}{:soso_e179:}{:soso_e179:}
发表于 2014-5-10 00:22 | 显示全部楼层
可以用论坛的代码高亮功能
发表于 2014-5-10 01:10 | 显示全部楼层
看得我蛋都碎了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-5 03:35 , Processed in 0.070681 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表