//The following sketch shows you the basics of using one piezzo buzzer with your Arduino
//David Abela
#include "ezBuzzer.h"
int buzzerPin = 7;
ezBuzzer buzzer(buzzerPin); // create ezBuzzer object that attach to a pin;
#include "melodies.h"
//The above code tells the Arduino UNO that the positive pin on the piezzo
//is connected to pin 7 on the board and we have created/declared an interger called 'buzzerPin'
//Every time we refer to pin 7 below you will use the interger reference 'buzzerPin'
//Intergers are used to store numbers
//Also you can use any pin for a Piezzo Buzzer(Digital, Analog or PWM), it doesn't matter
//with the acception if you are using an analogueWrite()on pins 3 and 11 at the same time
//you are using a tone()function, avoid these 2 pins or you'll get wierd results
void setup() {
//void setup(){ } is used to initialise any variables, pin modes, libraries being used, etc
//within the {} brackets
//This code only runs once after you switch on or reset the board
pinMode(buzzerPin, OUTPUT);
//The above code declares/initialises Pin 7 as an output pin
//This allows us to send a voltage @ varing different frequencies to pin 7 aka'buzzerPin'
//which plays a sound on the piezo buzzer
}
void loop() {
//Anything that sits within the void loop {}(Curley brackets)
//will run the below code consecutively
//Must start and finish with {} brackets
buzzer.loop();
if (buzzer.getState() == BUZZER_IDLE) {
int length = sizeof(m1nd) / sizeof(int);
buzzer.playMelody(m1, m1nd, length); // playing
}
else if (buzzer.getState() == BUZZER_IDLE) {
buzzer.stop();
}
}