Many newbies got trouble playing the melody using the piezo buzzer because the example code from Arduino IDE uses the delay() function. Playing melody in that way blocks other code. Therefore, I have made a ezBuzzer library that supports beeping and playing melody without using delay().
The below example code uses two buttons: one to start playing the melody, the other to stop playing the melody.
#include <ezBuzzer.h> // ezBuzzer library
const int START_BUTTON_PIN = 7;
const int STOP_BUTTON_PIN = 8;
const int BUZZER_PIN = 3;
int lastStartButtonState = HIGH; // the previous state from the input pin
int lastStopButtonState = 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_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
NOTE_E5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
void setup() {
Serial.begin(9600);
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
buzzer.loop(); // MUST call the buzzer.loop() function in loop()
int startButtonState = digitalRead(START_BUTTON_PIN);
int stopButtonState = digitalRead(STOP_BUTTON_PIN);
if (lastStartButtonState == HIGH && startButtonState == LOW) {
Serial.println("The START button is pressed");
if (buzzer.getState() == BUZZER_IDLE) {
int length = sizeof(noteDurations) / sizeof(int);
buzzer.playMelody(melody, noteDurations, length); // playing
}
}
if (lastStopButtonState == HIGH && stopButtonState == LOW) {
Serial.println("The STOP button is pressed");
if (buzzer.getState() != BUZZER_IDLE) {
buzzer.stop() ; // stop
}
}
lastStartButtonState = startButtonState;
lastStopButtonState = stopButtonState;
}
And this is another example: Keypad - beeping on key press