#include "pitches.h"
// https://forum.arduino.cc/t/stop-melody/398482
int melody1[] = {
NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_A4, 0, NOTE_C4, NOTE_E4, NOTE_A4, NOTE_B4, 0,
NOTE_E4, NOTE_GS4, NOTE_B4, NOTE_C5, 0, NOTE_E4, NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5,
NOTE_A4, 0, NOTE_C4, NOTE_E4, NOTE_A4, NOTE_B4, 0, NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4
};
int melody2[] = {
NOTE_A5, NOTE_FS5, NOTE_G5, NOTE_A5, NOTE_FS5, NOTE_G5, NOTE_A5, NOTE_A4, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5,
NOTE_FS5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_FS4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_FS4, NOTE_G4, NOTE_A4,
NOTE_G4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_A4, NOTE_B4,
NOTE_G4, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_A4, NOTE_B4, NOTE_CS5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5, NOTE_A5
};
int notedurations1[] = {
8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 2
};
int notedurations2[] = {
3, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 3, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 3, 6, 6, 8, 6, 6, 6, 6, 6, 6, 6,
};
const int button1 = 2;
const int button2 = 3;
const int piezo = 8;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
// iterate over the notes of the melody:
pinMode(piezo, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void playSong(int OutputPin, int Melody[], int NoteDurations[], int NumberOfNotes){
for (int thisNote = 0; thisNote < NumberOfNotes; thisNote++) {
int NoteDuration = 1000 / NoteDurations[thisNote];
tone(OutputPin, Melody[thisNote], NoteDuration);
int pauseBetweenNotes = NoteDuration * 1.30;
delay(pauseBetweenNotes);
}
noTone(OutputPin);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
if (buttonState1 == LOW) {
playSong(8, melody1, notedurations1, sizeof(melody1)/sizeof(melody1[0]));
}
else if (buttonState2 == LOW) {
playSong(8, melody2, notedurations2, sizeof(melody2)/sizeof(melody2[0]));
}
else {}
}