#include "SCoop.h"
int flag=3;
int ButtonA=4;//D0是B键 D1是D键 D2是A键 D3是C键
int ButtonB=5;
int ButtonC=6;
int ButtonD=7;
int kaiguan1=8;
int kaiguan2=9;
int i=0;
int j=0;
int k=0;
int b=0;
int flag1=0;
int flag2=0;
long int timeout1=5000;
long int timeout2=0;
#define D0 -1
#define D1 262
#define D2 293
#define D3 329
#define D4 349
#define D5 392
#define D6 440
#define D7 494
#define M1 523
#define M2 586
#define M3 658
#define M4 697
#define M5 783
#define M6 879
#define M7 987
#define H1 1045
#define H2 1171
#define H3 1316
#define H4 1393
#define H5 1563
#define H6 1755
#define H7 1971
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
int tune[]=
{
M3,M3,M4,M5,
M5,M4,M3,M2,
M1,M1,M2,M3,
M3,M2,M2,
M3,M3,M4,M5,
M5,M4,M3,M2,
M1,M1,M2,M3,
M2,M1,M1,
M2,M2,M3,M1,
M2,M3,M4,M3,M1,
M2,M3,M4,M3,M2,
M1,M2,D5,D0,
M3,M3,M4,M5,
M5,M4,M3,M4,M2,
M1,M1,M2,M3,
M2,M1,M1,
};
float durt[]=
{
1,1,1,1,
1,1,1,1,
1,1,1,1,
1+0.5,0.5,1+1,
1,1,1,1,
1,1,1,1,
1,1,1,1,
1+0.5,0.5,1+1,
1,1,1,1,
1,0.5,0.5,1,1,
1,0.5,0.5,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,0.5,0.5,
1,1,1,1,
1+0.5,0.5,1+1,
};
int lengthss;
int tonepin=10;
int wancheng=0;
void setup() {
mySCoop.start();
pinMode(flag,INPUT);
pinMode(ButtonA,INPUT);
pinMode(ButtonB,INPUT);
pinMode(ButtonC,INPUT);
pinMode(ButtonD,INPUT);
pinMode(kaiguan2,OUTPUT);
}
void loop() {
yield();
}
defineTask(laba);
void laba::setup()
{
pinMode(tonepin,OUTPUT);
}
void laba::loop()
{ digitalWrite(kaiguan2,0);
sleep(2000);
if(digitalRead(flag)==0&&digitalRead(ButtonA)==0&&digitalRead(ButtonB)==0&&digitalRead(ButtonC)==0&&digitalRead(ButtonD)==0)
{ for(int a=0;a<56;a++)
{
tone(tonepin,tune[a]);
sleep(400*durt[a]);
noTone(tonepin);
if(digitalRead(flag)==1||digitalRead(ButtonA)==1||digitalRead(ButtonB)==1||digitalRead(ButtonC)==1||digitalRead(ButtonD)==1)
break;
}
}
}
这个是源代码,我想要的就是我按下一个键,音乐就会停止 ,但是事实上停止不了,只能是把一首歌放完之后才能停止,怎么写代码???求大神 ,我就一小白
|