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

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9877|回复: 4

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

[复制链接]
发表于 2014-4-25 00:29 | 显示全部楼层 |阅读模式
在用arduino pro mini ATmage328 控制蜂鸣器播放音乐的时候,为何只能播放两首而过了以后就出现错误?
用串口看返回值,两首或以下音乐就可以,再加就bug了
代码如下 比较乱。。。。如果把loop中后面的两个函数ailala()和tiantian()注释掉就能正常运行,否则不行,播放任意两个可以,多了就不行了。
求大神解答。

[mw_shl_code=c,true]#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
//这部分是用英文对应了拍子,这样后面也比较好看
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,整数

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

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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E_DL7,NOTE_D1,  NOTE_D1,NOTE_D1,
NOTE_DL4,NOTE_D1,NOTE_D1,NOTE_D1,NOTE_D2,NOTE_D3,NOTE_D2,NOTE_D1,


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

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

int tune4[] =
{
  NOTE_E0,NOTE_E1,NOTE_E2,
  NOTE_E3,NOTE_E3,NOTE_E3,NOTE_E2,NOTE_E3,NOTE_E0,NOTE_E1,
NOTE_E2,NOTE_E1,NOTE_EL7,NOTE_EL6,NOTE_E0, NOTE_E2,NOTE_E3,

  NOTE_E4,NOTE_E4,NOTE_E4,NOTE_E3,NOTE_E4, NOTE_E0,NOTE_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4,NOTE_E4,NOTE_EL6,NOTE_E1,NOTE_E3,NOTE_E2,NOTE_E1,NOTE_EL7,
NOTE_E1,
NOTE_E0,NOTE_E3,NOTE_E3,NOTE_E4,NOTE_E5, NOTE_E4,NOTE_E3,
  NOTE_E6,NOTE_E5,NOTE_E4,NOTE_E3,NOTE_E4,
NOTE_E0,NOTE_E4,NOTE_E4,NOTE_EL6,NOTE_E1,NOTE_E3,NOTE_E2,NOTE_E1,NOTE_EL7,
NOTE_E1,

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

float duration4[]=
{
  0.5,0.25,0.25,
  0.5,0.5,0.5,0.25,1.25,0.75,0.25,
0.5,0.25,0.5,1,0.5,0.25,0.25,

  0.5,0.5,0.5,0.25,1.25,0.75,0.25,
  0.5,0.5,0.25,0.25, 1.5,0.5,0.25,0.25,
   0.5,0.5,0.5,0.25,1.25,0.75,0.25,

0.5,0.25,0.5,1,0.5,0.25,0.25,
  0.5,0.5,0.5,0.25,1.25,0.75,0.25,
0.5,0.5,0.25,0.25, 2.5,

    0.5,0.5,0.5,0.25,2.25,
     0.5,0.5,0.5,0.25,0.75,0.75,0.5,0.25,
      0.5,0.5,0.5,0.5,1.25,0.5,0.25,

        0.5,1,0.5,2,
         0.5,0.5,0.25,0.25,0.25,0.75,0.25,1.25,
         0.5,0.5,0.25,0.25,0.25,0.75,0.25,1.25,

         0.5,0.5,0.25,0.25,0.25,0.75,0.5,0.25,0.75,
     4,


        0.5,0.5,0.75,0.25,1.25,0.5,0.25,
         0.5,0.5,0.5,2.5,
         0.5,0.5,0.5,0.25,0.75,0.5,0.5,0.5,
         1,0.5,2.5,

    0.5,0.5,0.75,0.25,1.25,0.5,0.25,
         0.75,0.75,0.25,2.5,2,
         0.5,0.25,0.25,0.5,0.5,0.75,0.75,0.25,0.25,
         3,

           0.5,0.5,0.75,0.25,1.25,0.5,0.25,
         0.75,0.75,0.25,2.5,2,
         0.5,0.25,0.25,0.5,0.5,0.75,0.75,0.25,0.25,
         5,
};//这部分是整首曲子的接拍部分,也定义个序列duration,浮点(数组的个数和前面音符的个数是一样的,一一对应么)
int length4;
int length1;
int length2;
int length3;
int y;//这里定义一个变量,后面用来表示共有多少个音符
int tonePin=5;//蜂鸣器的pin
int i=0;
int tune2[] =
{
  NOTE_D5,NOTE_D3,NOTE_D2,NOTE_D1,
  NOTE_DH3,
  NOTE_D6,NOTE_D4,NOTE_DH2,NOTE_D1,
  NOTE_DL7,NOTE_D0,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,整数

float duration2[]=
{
  1.5,0.5,0.5,0.5,
  4,
  1.5,0.5,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.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,  
};
void setup()
{
  pinMode(tonePin,OUTPUT);//设置蜂鸣器的pin为输出模式
  length1 = sizeof(tune1)/sizeof(tune1[0]);//这里用了一个sizeof函数,可以查出tone序列里有多少个音符
    length2 = sizeof(tune2)/sizeof(tune2[0]);
      length3 = sizeof(tune3)/sizeof(tune3[0]);  
           length4 = sizeof(tune4)/sizeof(tune4[0]);  
    pinMode(14,INPUT);
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
Serial.begin(9600);
randomSeed(analogRead(14));

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

void tiantian()
{
  for(int x=0;x<length4;x++)//循环音符的次数
  {
    tone(tonePin,tune4[x]);//此函数依次播放tune序列里的数组,即每个音符
    delay(400*duration4[x]);//每个音符持续的时间,即节拍duration,400是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握吧
    noTone(tonePin);//停止当前音符,进入下一音符
  }
}
void loop()
{  int reading = analogRead(14);
  float voltage = reading / 204.6;
  Serial.print("Reading=");
  Serial.print(reading);
  Serial.print("\t\tVolts=");
  Serial.println(voltage);
  delay(500);
if(analogRead(14)>=380)
{
  y=random(1,5);
  if(y==1)
{Serial.print(1);
huluwa();

} else if ( y==2)
{ Serial.print(2);
yueliang();}

  {else if(y==3)
Serial.print(3);
ailala();}

else if(y==4)
{Serial.print(4);
tiantian();
}
}
delay(5000);
} [/mw_shl_code]

发表于 2014-5-2 20:16 | 显示全部楼层
本帖最后由 艰苦奋斗 于 2014-5-2 20:21 编辑

看了下,主程序后面一段“if ”函数嵌套层数太多了点,编译报错,改一下用switch 语句就通过了。但这“int reading = analogRead(14);   float voltage = reading / 204.6;   Serial.print("Reading=");   Serial.print(reading);   Serial.print("\t\tVolts=");   Serial.println(voltage);   “没看懂什么意思。我的UNO板子好象只有6个模拟口,MINI好象有8个,analogRead(14): 我的MEGA 2560好象有16个摸拟口。
发表于 2014-5-2 20:51 | 显示全部楼层
本帖最后由 艰苦奋斗 于 2014-5-3 14:04 编辑

还有,这个程序数组太大了,百度了一 下,arduino数组受制于SRAM空间,mega 328好象是2K,这个程序中的数组可能超过这个空间了,看到有位高人提示应该用PROGMEM关键字来把数据存入flash内存,我也是初学arduino,不是太懂。主要是看不到arduino全面的技术资料,大多是比较简单的教程,一些极限数据还弄不太清楚。
 楼主| 发表于 2014-5-5 20:11 | 显示全部楼层
艰苦奋斗 发表于 2014-5-2 20:16
看了下,主程序后面一段“if ”函数嵌套层数太多了点,编译报错,改一下用switch 语句就通过了。但这“int  ...

14是代表A0吧
 楼主| 发表于 2014-5-9 22:07 | 显示全部楼层
艰苦奋斗 发表于 2014-5-2 20:51
还有,这个程序数组太大了,百度了一 下,arduino数组受制于SRAM空间,mega 328好象是2K,这个程序中的数组 ...

感谢感谢,写进去果真可以了,粗略算了算,这个数组还真是大的很啊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-5 03:30 , Processed in 0.074344 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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