//The following sketch shows you the basics of using one piezzo buzzer with your Arduino
//David Abela
int buzzerPin = 7;
//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
tone(buzzerPin, 31);
//'tone' needs a minimukm of 2 arguements to work.(pin, frequency)
//The above code 'tone' generates a square wave signal and a 50% duty cycle to pin 7 aka 'buzzerPin
//The number '31' specifies the frequency to send to the piezo buzzer
//Adjust this value to what ever you like, it can be between 31 and 65,535
delay(1000);
//The above 'delay' code will play the signal for 1000 milliseconds/1 second
//Adjust this value to what ever you like
noTone(buzzerPin);
// The above 'noTone' code will stop the tone playing on pin 7
delay(1000);
//Now the below code will do exactly the same thing accept more simplied and less code
tone(buzzerPin, 100, 1000);
delay(2000);
//'tone' can have up to 3 arguements to work.(pin, frequency, duration)
//When you follow with a delay this time it will play no tone for 1000 milliseconds
//Why 1000 milliseconds you ask, when the delay says 2000 miliseconds?
//Well when the tone started playing, the delay started counting at the same time
//So this means what ever length of tone you play, add the duration of delay/pause you want
//to get the desired delay/pause
//tone 1000 + pause you want 1000 = delay (2000);
// You can keep repeating the above 2 lines of code and
// changing the values to create your own music or desired outcome
}