#include "ezBuzzer.h" // ezBuzzer library
const int BUZZER_PIN = 5;
int lastButtonState = HIGH; // the previous state from the input pin
ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin;
// notes in the melody:
int melody[] = {
NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_B5, NOTE_FS5, NOTE_DS5, REST,
NOTE_D5, NOTE_DS5, NOTE_E5,
NOTE_E5, NOTE_F5, NOTE_FS5,
NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_B5
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
16,16,16,16,32,16,8,32,32,32,16,32,32,16,32,32,16,8
};
// notes in the melody:
int melody2[] = {
NOTE_E4, NOTE_G4, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_G5
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations2[] = {
16,16,16,16,16,16
};
// notes in the melody:
int melody3[] = {
NOTE_C4, NOTE_G3, NOTE_C4, NOTE_E4,
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_GS3,
NOTE_C4, NOTE_DS4, NOTE_GS4, NOTE_DS4,
NOTE_G4, NOTE_C5, NOTE_DS5, NOTE_GS5,
NOTE_DS5, NOTE_AS3, NOTE_D4, NOTE_F4,
NOTE_AS4, NOTE_F4, NOTE_AS4, NOTE_D5,
NOTE_F5, NOTE_AS5, NOTE_F5
};
// notes in the melody:
int melody4[] = {
NOTE_C5, NOTE_G4, NOTE_C5, NOTE_E5,
NOTE_G5, NOTE_C6, NOTE_G5, NOTE_GS4,
NOTE_C5, NOTE_DS5, NOTE_GS5, NOTE_DS5,
NOTE_G5, NOTE_C6, NOTE_DS6, NOTE_GS6,
NOTE_DS6, NOTE_AS4, NOTE_D5, NOTE_F5,
NOTE_AS5, NOTE_F5, NOTE_AS5, NOTE_D6,
NOTE_F6, NOTE_AS6, NOTE_F6
};
// notes in the melody:
int melody5[] = {
NOTE_C3, NOTE_G2, NOTE_C3, NOTE_E3,
NOTE_G3, NOTE_C4, NOTE_G3, NOTE_GS2,
NOTE_C3, NOTE_DS3, NOTE_GS3, NOTE_DS3,
NOTE_G3, NOTE_C4, NOTE_DS4, NOTE_GS4,
NOTE_DS4, NOTE_AS2, NOTE_D3, NOTE_F3,
NOTE_AS3, NOTE_F3, NOTE_AS3, NOTE_D4,
NOTE_F4, NOTE_AS4, NOTE_F4
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations3[] = {
16,16,16,16,
16,16,16,16,
16,16,16,16,
16,16,16,16,
16,16,16,16,
16,16,16,16,
16,16,16
};
int noteLength;
int mario_init[] = {
NOTE_E5, REST, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5, REST,
NOTE_G5, REST, NOTE_G4
};
int mario_init_dur[] = {
16, 16, 8, 8, 8, 8, 8, 8,
8, 4, 4, 4
};
int mario_init_len = sizeof(mario_init_dur) / sizeof(int);
int mario_end[] = {
NOTE_B4, NOTE_F5, REST, NOTE_F5, REST, NOTE_F5, NOTE_E5, NOTE_D5,
NOTE_C5, NOTE_G4, REST, NOTE_G4, NOTE_C4,
};
int mario_end_dur[] = {
8, 4, 64, 8, 64, 6, 6, 6,
8, 4, 64, 8, 2
};
int mario_end_len = sizeof(mario_end_dur) / sizeof(int);
void setup() {
pinMode(3, INPUT_PULLUP);
Serial.begin(9600);
noteLength = sizeof(noteDurations3) / sizeof(int);
for (int i = 0; i < noteLength; i++)
{
noteDurations3[i] *= 2;
}
if (buzzer.getState() == BUZZER_IDLE)
{ // if stopped
buzzer.playMelody(mario_end, mario_end_dur, mario_end_len); // playing
}
}
void loop() {
buzzer.loop(); // MUST call the buzzer.loop() function in loop()
if (!digitalRead(3))// && buzzer.getState() == BUZZER_IDLE)
{ // if stopped
//buzzer.playMelody(melody3, noteDurations3, noteLength); // playing
buzzer.beep(100);
}
}