const int songLength = 1; char notes[] = "c"; // a space represents a rest int beats[] = {4}; int tempo = 150; const int buzzerPin = 7; // buzzer connected to digital pin 7 int buttonPin = 2; // pushbutton connected to digital pin 2 int val = 0; // variable to store the read value void setup() { pinMode(buzzerPin, OUTPUT); // sets the digital pin 7 as output pinMode(buttonPin, INPUT); // sets the digital pin 2 as input } void loop() { val = digitalRead(inPin); // read the input pin if (val == 1) { int i, duration; for (i = 0; i < songLength; i++) // step through the song arrays { duration = beats[i] * tempo; // length of note/rest in ms if (notes[i] == ' ') // is this a rest? { delay(duration); // then pause for a moment } else // otherwise, play the note { tone(buzzerPin, frequency(notes[i]), duration); delay(duration); // wait for tone to finish } delay(tempo/10); // brief pause between notes } // We only want to play the song once, so we'll pause forever: while(true){} // If you'd like your song to play over and over, // remove the above statement } int frequency(char note) { // This function takes a note character (a-g), and returns the // corresponding frequency in Hz for the tone() function. int i; const int numNotes = 8; // number of notes we're storing char names[] = { 'c'}; int frequencies[] = {262}; // Now we'll search through the letters in the array, and if // we find it, we'll return the frequency for that note. for (i = 0; i < numNotes; i++) // Step through the notes { if (names[i] == note) // Is this the one? { return(frequencies[i]); // Yes! Return the frequency } } return(0); // We looked through everything and didn't find it, // but we still need to return a value, so return 0. } }