// This program plays a tune when a button is pressed
//If you keep the button pressed, it loops continuously
// The first lines of program code below get everything setup on the Arduino
// The bit after "void loop()" is where the tune is created
/* define nicknames or aliases for things and values for constants */
#include "pitches.h" // library of tones and their frequencies
#define SPEAKER_PIN 8 // defines which pin of Arduino is connected to the loud speaker
#define BUTTON_PIN 6 // defines which pin of Arduino is connected to the push button
void setup() {
//Tell Arduino how each pin should work i.e. as an output or input
pinMode(SPEAKER_PIN, OUTPUT); //enable Arduino to output to speaker on pin 8
pinMode(BUTTON_PIN, INPUT_PULLUP); //enable Arduino to use its pin 6 to check the push button
}
void loop() { //the program code below repeats endlessly **********************************************
if (digitalRead( BUTTON_PIN) == LOW) { //Check if button pressed, if so, do following
for (int i = 0; i < 3; i++) {
tone(SPEAKER_PIN, NOTE_G4); //Play a note
delay(150); //For 150 milliseconds
tone(SPEAKER_PIN, NOTE_A5); //Play a different note
delay(150);
tone(SPEAKER_PIN, NOTE_B5);
delay(150);
tone(SPEAKER_PIN, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
tone(SPEAKER_PIN, NOTE_A5);
delay(150);
tone(SPEAKER_PIN, NOTE_B5);
delay(150);
tone(SPEAKER_PIN, NOTE_G5);
delay(300);
noTone(SPEAKER_PIN); //Stop Playing
}
tone(SPEAKER_PIN, NOTE_B6);
delay(150);
noTone(SPEAKER_PIN); //Stop Playing
delay(150);
tone(SPEAKER_PIN, NOTE_B6);
delay(150);
noTone(SPEAKER_PIN); //Stop Playing
delay(150);
tone(SPEAKER_PIN, NOTE_B6);
delay(150);
noTone(SPEAKER_PIN); //Stop Playing
delay(150);
tone(SPEAKER_PIN, NOTE_B6);
delay(150);
noTone(SPEAKER_PIN);
}
}
//End*****************************************************************************************************