Astronomia (Coffin Meme) Tune on Arduino

Astronomia (Coffin Meme) Tune on Arduino

Circuit Diagram

Addicted to the viral Coffin Meme tune? Want to play it on an Arduino and use it in your projects? Here’s how.

You only need two components: an Arduino and a Buzzer (and some jumper wires).

Video tutorial:

Connection Diagram:

Code Explanation

Code can be downloaded from https://github.com/pranavmittal611/AstronomiaArduino.

#include "pitches.h"
int button = 5; //pin to connect to button
int buzzer = 8; //pin to connect to buzzer +ve terminal
int looptune = 2; // how many times do you want the tune to loop

Includes “pitches.h” in the code (download from GitHub link above and place in the same folder as the .ino file), which contains frequencies of various musical notes which we will need to play the tune. Sets buzzer pin to 8, and button pin to 5.

Looptune is the number of times you want the tune to repeat after pressing the button. It can only be a positive integer.

int melody[] = {
  NOTE_G5, NOTE_G5, NOTE_G5, NOTE_G5, 
  NOTE_C5, NOTE_AS4, NOTE_A4, NOTE_F4,
  NOTE_G4, 0, NOTE_G4, NOTE_D5,
  NOTE_C5, 0, NOTE_AS4, 0,
  NOTE_A4, 0, NOTE_A4, NOTE_A4,
  NOTE_C5, 0, NOTE_AS4, NOTE_A4, 
  NOTE_G4,0, NOTE_G4, NOTE_AS5,
  NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
  NOTE_G4,0, NOTE_G4, NOTE_AS5,
  NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
  NOTE_G4, 0, NOTE_G4, NOTE_D5,
  NOTE_C5, 0, NOTE_AS4, 0,
  NOTE_A4, 0, NOTE_A4, NOTE_A4,
  NOTE_C5, 0, NOTE_AS4, NOTE_A4, 
  NOTE_G4,0, NOTE_G4, NOTE_AS5,
  NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5,
  NOTE_G4,0, NOTE_G4, NOTE_AS5,
  NOTE_A5, NOTE_AS5, NOTE_A5, NOTE_AS5
 };

Imports the frequencies of the notes we will be needing from “pitches.h” and stores them in an array named melody.

void setup() {
  pinMode(button, INPUT_PULLUP);
  looptune = looptune * 72; //do not change this
}

Configures the button pin to receive input by pulling it high. Multiplies looptune by 72, which is the number of musical notes in the melody array.

void loop() {
  if (digitalRead(button)==LOW){ //if button is pressed
    for (int thisNote = 0; thisNote < looptune; thisNote++) {
      int noteDuration = 750/4;
      tone(buzzer, melody[thisNote%72], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(buzzer);
    }
  }
}

Checks if button is pressed. If yes, plays tune twice (looptune number of times) using the tone function.

 

Leave a Reply

Your email address will not be published.