GeneralSpud
Published © GPL3+

Passive Buzzer Song: "Take on Me" By A-Ha!

A super simple beginner project playing around a passive buzzer. It plays "'Take on Me" by A-Ha!

BeginnerProtip100,053
Passive Buzzer Song: "Take on Me" By A-Ha!

Things used in this project

Story

Read more

Schematics

Wiring Diagram

Circuit Diagram

Wiring Diagram 2

Wiring Diagram

Code

A-ha! Take on Me Melody with a Passive Buzzer

C/C++
You can make any song you want by changing the order of the notes in the melody array and the duration of those notes in the duration array. Have fun with it!
//A-ha!
//by GeneralSpud

// For this to work, we need the pitches library

#include "pitches.h"

// Two things need to be created: the array for the notes of the melody (in order)
// and the duration of each (think of it like sheet music in two parts)

// BOTH ARRAYS MUST BE THE SAME SIZE!

// The melody array 
int melody[] = {
  NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5, 
  NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5, 
  NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5, 
  NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5
};

// The note duration, 8 = 8th note, 4 = quarter note, etc.
int durations[] = {
  8, 8, 8, 4, 4, 4, 
  4, 5, 8, 8, 8, 8, 
  8, 8, 8, 4, 4, 4, 
  4, 5, 8, 8, 8, 8
};
// determine the length of the arrays to use in the loop iteration
int songLength = sizeof(melody)/sizeof(melody[0]);
void setup() {
 //We don't need anything here
}

void loop() {
  // Iterate through both arrays
  // Notice how the iteration variable thisNote is created in the parenthesis
  // The for loop stops when it is equal to the size of the melody array
  for (int thisNote = 0; thisNote < songLength; thisNote++){
    // determine the duration of the notes that the computer understands
    // divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
    int duration = 1000/ durations[thisNote];
    tone(8, melody[thisNote], duration);
    // pause between notes
    int pause = duration * 1.3;
    delay(pause);
    // stop the tone
    noTone(8);
  }
}

Credits

GeneralSpud
0 projects • 3 followers

Comments