2014-02-28 57 views
0

我试图实现的是一个按钮,充当压电开关的开/关开关。最初,我希望压电器不要发出任何声音,直到用户按下一个按钮,然后他们可以通过按下相同的按钮来关闭压电。我想知道是否有人可以帮我弄清楚我需要添加什么代码才能获得这种按钮功能。Arduino - 如何用按钮控制压电?

这是toneMelody草图的修改版本附带Arduino的IDE,我具有连接到引脚12的按钮,以及一压电到销8

#include "pitches.h" 

// notes in the melody: 
int melody[] ={ 
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0, 
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5, 
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5, 
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5,0,NG5, 
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5, 
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0, 
NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0, 
NE5,NF5,NE5,ND5,ND5,NC5 
       }; 
// note durations: 4 = quarter note, 8 = eighth note, etc.: 
int noteDurations[] = { 
       8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8, 
       8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8, 
       8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8, 
       4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8, 
       4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2, 
       8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8, 
       8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8, 
       8,8,8,8,4,1 
       }; 

void setup() { 
    // iterate over the notes of the melody: 
    for (int thisNote = 0; thisNote < 240; thisNote++) { 

    // to calculate the note duration, take one second 
    // divided by the note type. 
    //e.g. quarter note = 1000/4, eighth note = 1000/8, etc. 
    int noteDuration = 1000/noteDurations[thisNote]; 
    tone(8, melody[thisNote],noteDuration); 

    // to distinguish the notes, set a minimum time between them. 
    // the note's duration + 30% seems to work well: 
    int pauseBetweenNotes = noteDuration * 1.30; 
    delay(pauseBetweenNotes); 
    // stop the tone playing: 
    noTone(8); 
    } 
} 

void loop() { 
    // no need to repeat the melody. 
} 

回答

0

我会用一个按钮会的方式像这样。我在上面和下面添加了评论是我添加的东西。这将是,当你按下按钮,整首歌曲将播放,然后停止,直到你再次按下按钮。你可以在这里看到我正在采取什么样的按钮http://arduino.cc/en/Tutorial/Button它也给了你一些示例代码,让你看看我刚才的错误,如果我搞砸 只是给我发消息,我会告诉你如何解决。

#include "pitches.h" 

// notes in the melody: 

int melody[] ={ 

NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5, 

NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0, 

0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND 

5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5, 

0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND 


5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5, 

NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5, 

0,NG5, 

NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5, 

NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5, 

NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0, 

NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0, 

NE5,NF5,NE5,ND5,ND5,NC5 
       }; 
// note durations: 4 = quarter note, 8 = eighth note, etc.: 

int noteDurations[] = { 
       8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8, 
       8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8, 
       8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8, 

       4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8, 

       4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2, 
       8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8, 

       8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8, 

       8,8,8,8,4,1 

       }; 


//int to identify what digital pin the button is hooked up to 

// can Chang 2 if put button on a different pin 

int buttonPin = 2; 

void setup() { 

//says button pin is a input 

    pinMode(buttonPin, INPUT); 

// I might say make a int = to the pin you have 

// your sound maker on, name it soundMakerPin, and the uncomment the line below 

// pinMode(soundMakerPin, OUTPUT); 

} 

void loop(){ 

// read the button 

    buttonState = digitalRead(buttonPin); 

    // check if the pushbutton is pressed. 

    // if it is, the buttonState is HIGH: 

    if (buttonState == HIGH) { 

    // play song 

song(); 

} 

} 


//function named song, call this and your song should play 

void song(){ 

// iterate over the notes of the melody: 

    for (int thisNote = 0; thisNote < 240; thisNote++) { 

    // to calculate the note duration, take one second 

    // divided by the note type. 

    //e.g. quarter note = 1000/4, eighth note = 1000/8, etc. 

    int noteDuration = 1000/noteDurations[thisNote]; 

    tone(8, melody[thisNote],noteDuration); 

    // to distinguish the notes, set a minimum time between them. 

    // the note's duration + 30% seems to work well: 

    int pauseBetweenNotes = noteDuration * 1.30; 

    delay(pauseBetweenNotes); 

    // stop the tone playing: 

    noTone(8); 

    } 

} 
+0

没有弹出我想要的方式,但所有的代码应该在那里 – holycatcrusher