If statement 我如何用按钮停止音乐?

If statement 我如何用按钮停止音乐?,if-statement,button,arduino,push,If Statement,Button,Arduino,Push,我想知道如何用按钮停止音乐(在你按下按钮的那一刻停止音乐)。因为现在我的代码做了我想做的(停止音乐,当我按下按钮时打开灯,但它会一直等到歌曲结束才停止)。 这是我的代码: int buttonState = 0; int speakerOut = 10; int buttonPin= 7; int frequency = 500; int ledPin = 13; int length = 17; // the number of notes char notes[] = "gcefgcefgc

我想知道如何用按钮停止音乐(在你按下按钮的那一刻停止音乐)。因为现在我的代码做了我想做的(停止音乐,当我按下按钮时打开灯,但它会一直等到歌曲结束才停止)。 这是我的代码:

int buttonState = 0;
int speakerOut = 10;
int buttonPin= 7;
int frequency = 500;
int ledPin = 13;
int length = 17; // the number of notes
char notes[] = "gcefgcefgcefgcefga "; // a space represents a rest
//int beats[] = {2,2,1,1,2,2,1,1,2,2,1,1,2,2,1,1};
//int tempo = 250;
int DEBUG = 1;
#define  c     3830    
#define  d     3400     
#define  e     3038   
#define  f     2864    
#define  g     2550    
#define  a     2272    
#define  b     2028  
#define  C     1912    
#define  R     0

void setup() {
  // put your setup code here, to run once:

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin,INPUT);
  pinMode(speakerOut, OUTPUT);
  if (DEBUG) { 
    Serial.begin(9600); // Set serial out if we want debugging
  } 
}

// MELODY and TIMING  =======================================
//  melody[] is an array of notes, accompanied by beats[], 
//  which sets each note's relative length (higher #, longer note) 
int melody[] = {  C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };
int beats[]  = { 16, 16, 16,  8,  8,  16, 32, 16, 16, 16, 8, 8 }; 
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration  = 0;

// PLAY TONE  ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
  long elapsed_time = 0;
  if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 
    //  played less long than 'duration', pulse speaker HIGH and LOW
    while (elapsed_time < duration) {

    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(tone_ / 2);

    // DOWN
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_ / 2);

    // Keep track of how long we pulsed
    elapsed_time += (tone_);
    } 
  }
  else { // Rest beat; loop times delay
    for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
    delayMicroseconds(duration);  
    }                      
  }                       
}


void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin);
  if (buttonState==HIGH){
    digitalWrite(ledPin, HIGH);
    noTone(speakerOut);
  }else {
    digitalWrite(ledPin, LOW);
    digitalWrite(speakerOut,HIGH);
    for (int i=0; i<MAX_COUNT; i++) {
    tone_ = melody[i];
    beat = beats[i];

    duration = beat * tempo; // Set up timing

    playTone(); 
    // A pause between notes...
    delayMicroseconds(pause);
    }
  }
}
int buttonState=0;
int speakerOut=10;
int buttonPin=7;
int频率=500;
int-ledPin=13;
int length=17;//注释数
char notes[]=“gcefgcefga”;//一个空格表示一个rest
//int beats[]={2,2,1,1,2,2,1,1,2,2,1,1,2,2,2,1,1};
//int-tempo=250;
int-DEBUG=1;
#定义C3830
#定义d 3400
#定义e 3038
#定义f 2864
#定义G2550
#定义一个2272
#定义b 2028
#定义C 1912
#定义r0
无效设置(){
//将安装代码放在此处,以便运行一次:
引脚模式(LED引脚,输出);
pinMode(按钮输入,输入);
pinMode(扬声器输出);
如果(调试){
Serial.begin(9600);//如果需要调试,请设置Serial
} 
}
//旋律与时间=======================================
//melody[]是一组音符,伴有节拍[],
//设置每个音符的相对长度(越高,音符越长)
int melody[]={C,b,g,C,b,e,R,C,C,g,a,C};
int beats[]={16,16,16,8,8,16,32,16,16,16,8,8};
int MAX_COUNT=sizeof(melody)/2;//用于循环的旋律长度。
//设定整体节奏
长节奏=10000;
//设置音符之间的停顿长度
int pause=1000;
//循环变量以增加静止长度
int rest_count=100;//0){//如果这不是一个静止拍,而音调有
//播放时间小于“持续时间”,脉冲扬声器高电平和低电平
while(经过的时间<持续时间){
数字写入(扬声器输出,高电平);
延迟微秒(音调/2);
//向下
数字写入(扬声器输出,低电平);
延迟微秒(音调/2);
//记录我们的脉搏跳动时间
经过的时间+=(音调);
} 
}
else{//休息拍;循环时间延迟
对于(intj=0;j对于(int i=0;i您有两个选项,在
playTone()之前检查按钮,如果按下,则结束
for
循环

for (int i=0; i<MAX_COUNT; i++) {
  tone_ = melody[i];
  beat = beats[i];

  duration = beat * tempo; // Set up timing
  if (digitalRead(buttonPin)==LOW){
    playTone();
  } else {
    break; //End for loop
  }
  // A pause between notes...
  delayMicroseconds(pause);
}

for(int i=0;i在
playTone()之前,您有两个选项
检查按钮,如果按下,则结束
for
循环

for (int i=0; i<MAX_COUNT; i++) {
  tone_ = melody[i];
  beat = beats[i];

  duration = beat * tempo; // Set up timing
  if (digitalRead(buttonPin)==LOW){
    playTone();
  } else {
    break; //End for loop
  }
  // A pause between notes...
  delayMicroseconds(pause);
}
for(int i=0;i