如何通过按钮停止音乐?

时间:2015-11-26 19:08:22

标签: 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[] = "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);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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

或相同但使用void loop()作为for循环。一个全局变量,count介于0和MAX_COUNT之间并执行相同的检查。如果按下count=0;,如果没有,则继续播放下一个音符。现在,我无法编写代码,但没有困难。