I don't know much about code and can't figure out how to play the music after pushing a button. It's for a final and I'm stuck. Any ideas on how to help?
//Set pin as speaker pin
#define speakerPin 8
#define NOTE_G4 392
#define NOTE_C5 523
#define NOTE_E5 659
#define NOTE_B3 247
#define NOTE_G5 784
// notes in the melody(reveille:
int melody[] = {
NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,NOTE_C5,
NOTE_E5,NOTE_C5,NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,
NOTE_G4,NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_E5,
NOTE_C5,NOTE_E5,NOTE_C5,NOTE_G4,NOTE_G4,NOTE_C5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,16,16,8,8,8,16,16,8,8,8,16,16,8,8,4,8,8,8,16,
16,8,8,8,16,16,8,8,8,16,16,8,8,4
};
void setup() {
pinMode(8, OUTPUT);
}
int button = 0;
void loop() {
button = digitalRead(13);
//If button is pushes play speaker
if(button == HIGH) {
digitalWrite(speakerPin,HIGH);
delay(1000);
}
//If button is not pushed don't play speaker
else(button == LOW); {
digitalWrite(speakerPin,LOW);
delay(10);
}
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; 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 * 1300;
delay(pauseBetweenNotes);
}
//Turn off music
noTone(8);
}