Super Mario theme song w/ piezo buzzer and Arduino!

Today I found a complete post on how to play Super Mario Bros theme song on a piezo buzzer! It’s very simple and fun, and great as a beginner Arduino project.
All fame goes to Dipto Pratyaksa for making the Sketch code and sharing it with us!

I modified the code posted by Dipto and added the PWM-pitches in directly into the Sketch, so you don’t have to mess around with Arduino libraries!

Original post:
http://www.linuxcircle.com/2013/03/31/playing-mario-bros-tune-with-arduino-and-piezo-buzzer/

Hardware requirements

  • An Arduino(I used an Arduino Nano, any other is fine)
  • Piezo buzzer
  • 1 k ohm resistor(any resistor between 333 ohm to 1 k  should be fine in this project)
  • A breadboard
  • Some breadboard cable(s)

Connecting the components

If you have an Arduino Uno(which most people have), connect the components with the help of the image below. If you have an Arduino Nano, look the the image in “Using an Arduino Nano”.

Connect the positive side of the buzzer to digital pin 3, then the negative side to a 1k ohm resistor. Connect the other side of the 1 k ohm resistor to ground(GND) pin on the Arduino. Remember to connect the buzzer the right way, the buzzer has positive and negative pins!

So basically the buzzer, 1 k ohm resistor and Arduino should be connected like this:
Arduino digital pin 3 –> Buzzer –> 1 k ohm resisotor –> Arduino ground(GND) pin.

You can actually do without the 1 k ohm resistor! If you connect without the resistor, the buzzer will be a lot louder, and the sound quality might degrade. But you can also lower the resistance to get a little louder sound, and keep the sound quality. Another idea is using a potentiometer instead of a resistor to act as a volume controller! For this tutorial we’ll just be using a 1 k ohm resistor.

Using an Arduino Uno

Below is an illustration of how to connect the buzzer and resistor to an Arduino Uno.

buzzeruno

Using an Arduino Nano

Below is an illustration of how to connect the buzzer and resistor to an Arduino Nano.

buzzer

Uploading the code

There is a pretty huge amount of code in this Sketch. Just press the “Copy” button on the top right of the code text field for it to automatically highlight the whole code for you.

/*
  Arduino Mario Bros Tunes
  With Piezo Buzzer and PWM

  Connect the positive side of the Buzzer to pin 3,
  then the negative side to a 1k ohm resistor. Connect
  the other side of the 1 k ohm resistor to
  ground(GND) pin on the Arduino.

  by: Dipto Pratyaksa
  last updated: 31/3/13
*/

/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

#define melodyPin 3
//Mario main theme melody
int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempo[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};
//Underworld melody
int underworld_melody[] = {
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  NOTE_AS3, NOTE_AS4, 0,
  0,
  NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  NOTE_AS3, NOTE_AS4, 0,
  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  NOTE_DS3, NOTE_DS4, 0,
  0,
  NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  NOTE_DS3, NOTE_DS4, 0,
  0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  NOTE_CS4, NOTE_DS4,
  NOTE_DS4, NOTE_GS3,
  NOTE_G3, NOTE_CS4,
  NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  NOTE_GS4, NOTE_DS4, NOTE_B3,
  NOTE_AS3, NOTE_A3, NOTE_GS3,
  0, 0, 0
};
//Underwolrd tempo
int underworld_tempo[] = {
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  3,
  12, 12, 12, 12,
  12, 12, 6,
  6, 18, 18, 18,
  6, 6,
  6, 6,
  6, 6,
  18, 18, 18, 18, 18, 18,
  10, 10, 10,
  10, 10, 10,
  3, 3, 3
};

void setup(void)
{
  pinMode(3, OUTPUT);//buzzer
  pinMode(13, OUTPUT);//led indicator when singing a note

}
void loop()
{
  //sing the tunes
  sing(1);
  sing(1);
  sing(2);
}
int song = 0;

void sing(int s) {
  // iterate over the notes of the melody:
  song = s;
  if (song == 2) {
    Serial.println(" 'Underworld Theme'");
    int size = sizeof(underworld_melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000 / underworld_tempo[thisNote];

      buzz(melodyPin, underworld_melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(melodyPin, 0, noteDuration);

    }

  } else {

    Serial.println(" 'Mario Theme'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000 / tempo[thisNote];

      buzz(melodyPin, melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(melodyPin, 0, noteDuration);

    }
  }
}

void buzz(int targetPin, long frequency, long length) {
  digitalWrite(13, HIGH);
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
  digitalWrite(13, LOW);

}

After uploading the Sketch-code, your Arduino should start playing!

If you have any questions feel free to ask me by using the Contact page or by commenting below.

Prince

A second year computer engineering student at Malmö University in Sweden just having some fun with this blog. Computer Engineering and Mobile IT: Bachelor of Science in engineering

You may also like...

73 Responses

  1. I enjoy what you guys tend to be up too. This sort of
    clever work and exposure! Keep up the excellent works guys I’ve incorporated
    you guys to our blogroll.

  2. Thanks for one’s marvelous posting! I actually enjoyed reading it, you could be a great author.I will remember to bookmark your blog and
    definitely will come back sometime soon. I want to encourage you to ultimately continue your great work,
    have a nice weekend!

  3. Émissions Tv says:

    Spot on with this write-up, I really feel this web site needs much
    more attention. I’ll probably be returning to read more,
    thanks for the info!

  4. I go to see each day some web sites and information sites to read articles, but
    this webpage provides quality based articles.

  5. console playstation 4 pas cher says:

    Thanks in favor of sharing such a fastidious thought, article is pleasant,
    thats why i have read it entirely

  6. tente reception 3x9 says:

    I like the helpful info you provide in your articles. I’ll bookmark your weblog and
    check again here frequently. I’m quite sure I will learn many new
    stuff right here! Good luck for the next!

  7. theme cydia 2014 says:

    I was wondering if you ever thought of changing the structure of your
    site? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect
    with it better. Youve got an awful lot of text for only having
    1 or two pictures. Maybe you could space it out better?

    • Prince says:

      Thank you for the well-structured feedback! I added another line break before every heading-title in the article. It might not be much of a difference but I don’t want to change it too much so it actually gets more ugly. Can I have your opinion of the updated layout? And if you have any more suggestions for improvements, don’t be a stranger!

  8. replica hermes birkin says:

    I am curious to find out what blog platform you are working with?
    I’m experiencing some minor security issues with my latest site
    and I’d like to find something more secure.
    Do you have any recommendations?

  9. kate spade diaper bag says:

    I’ll immediately grasp your rss feed as I can not to find your e-mail subscription link or newsletter service.
    Do you have any? Kindly let me recognize in order that I may subscribe.
    Thanks.

  10. giuseppe zanotti men says:

    This is a topic which is close to my heart… Best wishes!
    Where are your contact details though?

  11. test kitchen says:

    It’s actually a cool and useful piece of info. I’m satisfied that you
    shared this useful info with us. Please stay us informed like this.
    Thank you for sharing.

  12. Hi to all, it’s in fact a nice for me to visit this site, it consists
    of important Information.

  13. asphalt 8 airborne cheats says:

    Fantastic items from you, man. I have be aware your stuff
    previous to and you are simply too fantastic. I really like what you have obtained here,
    really like what you’re saying and the way in which by which you
    say it. You make it enjoyable and you still take care
    of to keep it sensible. I can’t wait to learn much more from
    you. This is really a wonderful site.

  14. Hungry Shark evolution Gems cheat says:

    I’m impressed, I must say. Seldom do I encounter a blog that’s equally educative and amusing, and without a doubt, you’ve
    hit the nail on the head. The issue is something that too few folks
    are speaking intelligently about. I’m very happy I came across this in my hunt for something regarding this.

  15. What’s up, I check your new stuff on a regular basis.
    Your writing style is witty, keep it up!

  16. Hello, I wish for to subscribe for this blog to
    obtain hottest updates, so where can i do it please help.

  17. kiNi4PFYAjI says:

    572077 225451Its like you read my mind! You appear to know a whole lot about this, like you wrote the book in it or something. I believe which you could do with several pics to drive the message home a bit, but other than that, this really is wonderful weblog. A fantastic read. I

  18. Dell says:

    I do not know whether it’s just me or if perhaps everybody else encountering issues with your blog.
    It seems like some of the written text on your content are running
    off the screen. Can someone else please provide feedback and let me
    know if this is happening to them as well? This might be a
    issue with my web browser because I’ve had this happen previously.
    Cheers

  19. Thanks for the gkod writeup. It actually was a enjoyment account it.
    Glance complicatewd to more delivered agreeable from you!
    However, how could we be in contact?

  20. Your mode of explaining the whole thing in this post is actually good, every one caan simply be aware of it, Thanks
    a lot.

  21. Donovan Reynolds says:

    Try this modified code right here. Same circuit, better sound:

    http://txt.do/ddmv4

  22. Menion says:

    Hi, i have a buzzer 9v, how can connect the battery with this circuit? The sound is very low. Thanks.

  23. Offset says:

    I’ve connected Arduino to a stepper motor driver, and it did play Mario theme using a motor quite well after a little bit of tweaking )) Thanks a lot!

  24. Shou says:

    Nice work 🙂 Just forgot to write Serial.begin(9600)

  25. Sounds good! 🙂

  26. Matthew says:

    It was so earrape without resistor so I had to upload BareMinimum

  27. Indruto says:

    nice i already test and it works well 😀

    Thanks for sharing

  28. Ben says:

    I tried to upload it and all I got was this “sketch_jan06a:14: error: stray ‘\302’ in program”

  29. Jinho Choi says:

    WOW THE BEST!!!

  30. HYUNJUN KIN says:

    It’s a best song ever!!

  31. Timalsina Raju says:

    Thanks a lot. I enjoy listening its sounds and lyrics!!!

  32. Somen Jana says:

    How to play an Indian Music?
    A demo of a RabindraSangeet (Song by Rabindra Nath Tgore — Nobel Laureates in literature, 1913) is shown in the following link:

  33. jackson says:

    Thanks for the example! I can play Mario through a motor and that makes me happy 🙂

  34. m says:

    i have this error
    exit status 1
    ‘NOTE_E7’ was not declared in this scope

  35. 86Shauna says:

    I have noticed you don’t monetize your website, don’t waste your traffic, you
    can earn additional bucks every month because you’ve got hi quality content.
    If you want to know how to make extra money, search for: best adsense alternative Wrastain’s tools

  36. love spells says:

    Hi there,I log on to your blogs named “Super Mario theme song w/ piezo buzzer and Arduino! | PrinceTronics” regularly.Your humoristic style is witty, keep doing what you’re doing! And you can look our website about love spells.

  37. Que você faz para acumular força elétrica? http://queromeudinheiro.com

  38. bob says:

    it’s a very boring expeirients

  39. LastPaul says:

    I see you don’t monetize your site, don’t waste your traffic, you can earn additional cash every month because you’ve got hi quality
    content. If you want to know how to make extra bucks, search for: Mertiso’s tips
    best adsense alternative

  40. krutika says:

    sir u can post report on arduino piano

  41. kenneth says:

    what if i need 2 seconds, or 4 seconds of note tempo??
    what code im ganno use on tempo
    only 1 sec is declared there

    thanks in advance
    tha

  42. dick says:

    your ma gae

  43. MylesBiggie says:

    Hi admin, i’ve been reading your page for some time
    and I really like coming back here. I can see that you probably don’t make
    money on your website. I know one awesome method of earning money, I think you will
    like it. Search google for: dracko’s tricks

  44. vicsar says:

    This is awesome. I like the modifications done. Thanks.

  45. BestMaik says:

    I see you don’t monetize your website, don’t
    waste your traffic, you can earn extra cash every month.
    You can use the best adsense alternative for any type of website (they approve all websites), for more info simply search
    in gooogle: boorfe’s tips monetize your website

  46. This is veryreally interesting, You areYou’re a very skilled blogger. I haveI’ve joined your feedrss feed and look forward to seeking more of your greatwonderfulfantasticmagnificentexcellent post. Also, I haveI’ve shared your siteweb sitewebsite in my social networks!

  47. Seth says:

    Hey i modified this a bit so that when i pressed the button it would change the song but it only works if the button is down while the song ends and it only switches the main theme to the underground theme and not the other way around how do i fix this?

    code:

    /*
    Arduino Mario Bros Tunes
    With Piezo Buzzer and PWM

    Connect the positive side of the Buzzer to pin 3,
    then the negative side to a 1k ohm resistor. Connect
    the other side of the 1 k ohm resistor to
    ground(GND) pin on the Arduino.

    by: Dipto Pratyaksa
    last updated: 31/3/13
    */

    /*************************************************
    * Public Constants
    *************************************************/

    int switchPin = 7;
    int songNum = 1;

    #define NOTE_B0 31
    #define NOTE_C1 33
    #define NOTE_CS1 35
    #define NOTE_D1 37
    #define NOTE_DS1 39
    #define NOTE_E1 41
    #define NOTE_F1 44
    #define NOTE_FS1 46
    #define NOTE_G1 49
    #define NOTE_GS1 52
    #define NOTE_A1 55
    #define NOTE_AS1 58
    #define NOTE_B1 62
    #define NOTE_C2 65
    #define NOTE_CS2 69
    #define NOTE_D2 73
    #define NOTE_DS2 78
    #define NOTE_E2 82
    #define NOTE_F2 87
    #define NOTE_FS2 93
    #define NOTE_G2 98
    #define NOTE_GS2 104
    #define NOTE_A2 110
    #define NOTE_AS2 117
    #define NOTE_B2 123
    #define NOTE_C3 131
    #define NOTE_CS3 139
    #define NOTE_D3 147
    #define NOTE_DS3 156
    #define NOTE_E3 165
    #define NOTE_F3 175
    #define NOTE_FS3 185
    #define NOTE_G3 196
    #define NOTE_GS3 208
    #define NOTE_A3 220
    #define NOTE_AS3 233
    #define NOTE_B3 247
    #define NOTE_C4 262
    #define NOTE_CS4 277
    #define NOTE_D4 294
    #define NOTE_DS4 311
    #define NOTE_E4 330
    #define NOTE_F4 349
    #define NOTE_FS4 370
    #define NOTE_G4 392
    #define NOTE_GS4 415
    #define NOTE_A4 440
    #define NOTE_AS4 466
    #define NOTE_B4 494
    #define NOTE_C5 523
    #define NOTE_CS5 554
    #define NOTE_D5 587
    #define NOTE_DS5 622
    #define NOTE_E5 659
    #define NOTE_F5 698
    #define NOTE_FS5 740
    #define NOTE_G5 784
    #define NOTE_GS5 831
    #define NOTE_A5 880
    #define NOTE_AS5 932
    #define NOTE_B5 988
    #define NOTE_C6 1047
    #define NOTE_CS6 1109
    #define NOTE_D6 1175
    #define NOTE_DS6 1245
    #define NOTE_E6 1319
    #define NOTE_F6 1397
    #define NOTE_FS6 1480
    #define NOTE_G6 1568
    #define NOTE_GS6 1661
    #define NOTE_A6 1760
    #define NOTE_AS6 1865
    #define NOTE_B6 1976
    #define NOTE_C7 2093
    #define NOTE_CS7 2217
    #define NOTE_D7 2349
    #define NOTE_DS7 2489
    #define NOTE_E7 2637
    #define NOTE_F7 2794
    #define NOTE_FS7 2960
    #define NOTE_G7 3136
    #define NOTE_GS7 3322
    #define NOTE_A7 3520
    #define NOTE_AS7 3729
    #define NOTE_B7 3951
    #define NOTE_C8 4186
    #define NOTE_CS8 4435
    #define NOTE_D8 4699
    #define NOTE_DS8 4978

    #define melodyPin 3
    //Mario main theme melody
    int melody[] = {
    NOTE_E7, NOTE_E7, 0, NOTE_E7,
    0, NOTE_C7, NOTE_E7, 0,
    NOTE_G7, 0, 0, 0,
    NOTE_G6, 0, 0, 0,

    NOTE_C7, 0, 0, NOTE_G6,
    0, 0, NOTE_E6, 0,
    0, NOTE_A6, 0, NOTE_B6,
    0, NOTE_AS6, NOTE_A6, 0,

    NOTE_G6, NOTE_E7, NOTE_G7,
    NOTE_A7, 0, NOTE_F7, NOTE_G7,
    0, NOTE_E7, 0, NOTE_C7,
    NOTE_D7, NOTE_B6, 0, 0,

    NOTE_C7, 0, 0, NOTE_G6,
    0, 0, NOTE_E6, 0,
    0, NOTE_A6, 0, NOTE_B6,
    0, NOTE_AS6, NOTE_A6, 0,

    NOTE_G6, NOTE_E7, NOTE_G7,
    NOTE_A7, 0, NOTE_F7, NOTE_G7,
    0, NOTE_E7, 0, NOTE_C7,
    NOTE_D7, NOTE_B6, 0, 0
    };
    //Mario main them tempo
    int tempo[] = {
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,

    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,

    9, 9, 9,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,

    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,

    9, 9, 9,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    };
    //Underworld melody
    int underworld_melody[] = {
    NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
    NOTE_AS3, NOTE_AS4, 0,
    0,
    NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
    NOTE_AS3, NOTE_AS4, 0,
    0,
    NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
    NOTE_DS3, NOTE_DS4, 0,
    0,
    NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
    NOTE_DS3, NOTE_DS4, 0,
    0, NOTE_DS4, NOTE_CS4, NOTE_D4,
    NOTE_CS4, NOTE_DS4,
    NOTE_DS4, NOTE_GS3,
    NOTE_G3, NOTE_CS4,
    NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
    NOTE_GS4, NOTE_DS4, NOTE_B3,
    NOTE_AS3, NOTE_A3, NOTE_GS3,
    0, 0, 0
    };
    //Underwolrd tempo
    int underworld_tempo[] = {
    12, 12, 12, 12,
    12, 12, 6,
    3,
    12, 12, 12, 12,
    12, 12, 6,
    3,
    12, 12, 12, 12,
    12, 12, 6,
    3,
    12, 12, 12, 12,
    12, 12, 6,
    6, 18, 18, 18,
    6, 6,
    6, 6,
    6, 6,
    18, 18, 18, 18, 18, 18,
    10, 10, 10,
    10, 10, 10,
    3, 3, 3
    };

    void setup(void)
    {
    pinMode(3, OUTPUT);//buzzer
    pinMode(13, OUTPUT);//led indicator when singing a note
    pinMode(switchPin, INPUT_PULLUP);

    }
    void loop()
    {
    //sing the tunes
    if (digitalRead(switchPin) == LOW)
    {
    if (songNum = 1)
    {
    songNum = 2;
    }
    else if(songNum = 2)
    {
    songNum = 1;
    }
    }

    sing(songNum);
    }
    int song = 0;

    void sing(int s) {
    // iterate over the notes of the melody:
    song = s;
    if (song == 2) {
    Serial.println(” ‘Underworld Theme'”);
    int size = sizeof(underworld_melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / underworld_tempo[thisNote];

    buzz(melodyPin, underworld_melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    // stop the tone playing:
    buzz(melodyPin, 0, noteDuration);

    }

    } else {

    Serial.println(" 'Mario Theme'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / tempo[thisNote];

    buzz(melodyPin, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    // stop the tone playing:
    buzz(melodyPin, 0, noteDuration);

    }
    }
    }

    void buzz(int targetPin, long frequency, long length) {
    digitalWrite(13, HIGH);
    long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
    //// 1 second's worth of microseconds, divided by the frequency, then split in half since
    //// there are two phases to each cycle
    long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
    //// multiply frequency, which is really cycles per second, by the number of seconds to
    //// get the total number of cycles to produce
    for (long i = 0; i < numCycles; i++) { // for the calculated length of time…
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
    }
    digitalWrite(13, LOW);

    }

  48. Mars says:

    Hi. How would I make my own song. Let’s say I have the notes for the song like Happy Birthday, how do I make it play with a rhythm or specific beat instead of a flat boring pace?

  49. Kostas says:

    Hi there, really nice techno-art, tnx for sharing!

    any idea how to modify the below code for use in time sensitive loops?
    (due to “delayMicroseconds” the loop interrupted)

    void buzz(int targetPin, long frequency, long length) {
    digitalWrite(13, HIGH);
    long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
    //// 1 second’s worth of microseconds, divided by the frequency, then split in half since
    //// there are two phases to each cycle
    long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
    //// multiply frequency, which is really cycles per second, by the number of seconds to
    //// get the total number of cycles to produce
    for (long i = 0; i < numCycles; i++) { // for the calculated length of time…
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
    }
    digitalWrite(13, LOW);

  50. hael husaino says:

    what the hell

  51. LIL PUMP says:

    OLA SOI AURONPLEY

  52. Shark Youtube to mp3 Online

  53. peanut plays says:

    sub 2 peanut plays on youtube

  54. cant hear it mate, whats going on here. feeling like shit and this sure doesnt help pal. was at a 5 star hotel and greeted home with you, shithead. send help, please, ill do anything. your wife is hot

  55. shea says:

    Hi, my name is Shea Rutledge!

    I`m a professional writer and I`m going to change your lifes onсe and for all
    Writing has been my passion for a long time and now I cannot imagine my life without it.
    Most of my books were sold throughout Canada, USA, China and even Australia. Also I`m working with services that help people to save their time.
    People ask me “Mr, Shea Rutledge, I need your professional help” and I always accept the request, `cause I know, that only I can solve all their problems!

    Professional Writer – Shea Rutledge – http://www.studioahz.comCorps

  56. Awesome post! Keep up the great work!

  57. Victorvug says:

    Interesting idea luorennetpcopsubs.gq

  58. Elvin Witz says:

    Weight Loss Guaranteed

  59. In addition to the poker operate, they will be a raffle, meals and entertainment. It grew to become even more well-liked online because no reside dealers or tables are required. The card dealing position will rotate amongst players.

  1. 06/09/2014

    .

    tnx for info.

  2. 01/24/2015

    […] Supermario theme w/ piezo buzzer and android […]

  3. 02/21/2015

    […] Kaynağı: Princetronics Ana tema müziği ve Underworld müziği notaları kodlarda verilmiş. Kodları Arduino‘ya […]

  4. 04/28/2015
  5. 10/15/2015

    […] the piezo, which you can use to make 8 bit music and use as a vibration sensor. So I found a site (http://www.princetronics.com/supermariothemesong/) that would enable the Mario Theme…. to be […]

  6. 11/04/2015
  7. 11/16/2015

    […] code for “sing()” was found using a website that showed how to create the Super Mario Bros. Theme Song. Other than the actual melody that was played, I used the same structure as the code on this […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.