Advertisement
prasanth_ntu

Supermario

Feb 23rd, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. /*
  2.   Arduino Mario Bros Tunes
  3.   With Piezo Buzzer and PWM
  4.   by: Dipto Pratyaksa
  5.   last updated: 31/3/13
  6. */
  7. #include "pitches.h"
  8.  
  9. #define melodyPin 3
  10. //Mario main theme melody
  11. int melody[] = {
  12.   NOTE_E7, NOTE_E7, 0, NOTE_E7,
  13.   0, NOTE_C7, NOTE_E7, 0,
  14.   NOTE_G7, 0, 0,  0,
  15.   NOTE_G6, 0, 0, 0,
  16.  
  17.   NOTE_C7, 0, 0, NOTE_G6,
  18.   0, 0, NOTE_E6, 0,
  19.   0, NOTE_A6, 0, NOTE_B6,
  20.   0, NOTE_AS6, NOTE_A6, 0,
  21.  
  22.   NOTE_G6, NOTE_E7, NOTE_G7,
  23.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  24.   0, NOTE_E7, 0,NOTE_C7,
  25.   NOTE_D7, NOTE_B6, 0, 0,
  26.  
  27.   NOTE_C7, 0, 0, NOTE_G6,
  28.   0, 0, NOTE_E6, 0,
  29.   0, NOTE_A6, 0, NOTE_B6,
  30.   0, NOTE_AS6, NOTE_A6, 0,
  31.  
  32.   NOTE_G6, NOTE_E7, NOTE_G7,
  33.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  34.   0, NOTE_E7, 0,NOTE_C7,
  35.   NOTE_D7, NOTE_B6, 0, 0
  36. };
  37. //Mario main them tempo
  38. int tempo[] = {
  39.   12, 12, 12, 12,
  40.   12, 12, 12, 12,
  41.   12, 12, 12, 12,
  42.   12, 12, 12, 12,
  43.  
  44.   12, 12, 12, 12,
  45.   12, 12, 12, 12,
  46.   12, 12, 12, 12,
  47.   12, 12, 12, 12,
  48.  
  49.   9, 9, 9,
  50.   12, 12, 12, 12,
  51.   12, 12, 12, 12,
  52.   12, 12, 12, 12,
  53.  
  54.   12, 12, 12, 12,
  55.   12, 12, 12, 12,
  56.   12, 12, 12, 12,
  57.   12, 12, 12, 12,
  58.  
  59.   9, 9, 9,
  60.   12, 12, 12, 12,
  61.   12, 12, 12, 12,
  62.   12, 12, 12, 12,
  63. };
  64.  
  65. //
  66. //Underworld melody
  67. int underworld_melody[] = {
  68.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  69.   NOTE_AS3, NOTE_AS4, 0,
  70.   0,
  71.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  72.   NOTE_AS3, NOTE_AS4, 0,
  73.   0,
  74.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  75.   NOTE_DS3, NOTE_DS4, 0,
  76.   0,
  77.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  78.   NOTE_DS3, NOTE_DS4, 0,
  79.   0, NOTE_DS4, NOTE_CS4, NOTE_D4,
  80.   NOTE_CS4, NOTE_DS4,
  81.   NOTE_DS4, NOTE_GS3,
  82.   NOTE_G3, NOTE_CS4,
  83.   NOTE_C4, NOTE_FS4,NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  84.   NOTE_GS4, NOTE_DS4, NOTE_B3,
  85.   NOTE_AS3, NOTE_A3, NOTE_GS3,
  86.   0, 0, 0
  87. };
  88. //Underwolrd tempo
  89. int underworld_tempo[] = {
  90.   12, 12, 12, 12,
  91.   12, 12, 6,
  92.   3,
  93.   12, 12, 12, 12,
  94.   12, 12, 6,
  95.   3,
  96.   12, 12, 12, 12,
  97.   12, 12, 6,
  98.   3,
  99.   12, 12, 12, 12,
  100.   12, 12, 6,
  101.   6, 18, 18, 18,
  102.   6, 6,
  103.   6, 6,
  104.   6, 6,
  105.   18, 18, 18,18, 18, 18,
  106.   10, 10, 10,
  107.   10, 10, 10,
  108.   3, 3, 3
  109. };
  110.  
  111. void setup(void)
  112. {
  113.    pinMode(3, OUTPUT);//buzzer
  114.    pinMode(13, OUTPUT);//led indicator when singing a note
  115.  
  116. }
  117. void loop()
  118. {
  119. //sing the tunes
  120.   sing(1);
  121.   sing(1);
  122.   sing(2);
  123. }
  124. int song = 0;
  125.  
  126. void sing(int s){      
  127.    // iterate over the notes of the melody:
  128.    song = s;
  129.    if(song==2){
  130.      Serial.println(" 'Underworld Theme'");
  131.      int size = sizeof(underworld_melody) / sizeof(int);
  132.      for (int thisNote = 0; thisNote < size; thisNote++) {
  133.  
  134.        // to calculate the note duration, take one second
  135.        // divided by the note type.
  136.        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  137.        int noteDuration = 1000/underworld_tempo[thisNote];
  138.  
  139.        buzz(melodyPin, underworld_melody[thisNote],noteDuration);
  140.  
  141.        // to distinguish the notes, set a minimum time between them.
  142.        // the note's duration + 30% seems to work well:
  143.        int pauseBetweenNotes = noteDuration * 1.30;
  144.        delay(pauseBetweenNotes);
  145.  
  146.        // stop the tone playing:
  147.        buzz(melodyPin, 0,noteDuration);
  148.  
  149.     }
  150.  
  151.    }else{
  152.  
  153.      Serial.println(" 'Mario Theme'");
  154.      int size = sizeof(melody) / sizeof(int);
  155.      for (int thisNote = 0; thisNote < size; thisNote++) {
  156.  
  157.        // to calculate the note duration, take one second
  158.        // divided by the note type.
  159.        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  160.        int noteDuration = 1000/tempo[thisNote];
  161.  
  162.        buzz(melodyPin, melody[thisNote],noteDuration);
  163.  
  164.        // to distinguish the notes, set a minimum time between them.
  165.        // the note's duration + 30% seems to work well:
  166.        int pauseBetweenNotes = noteDuration * 1.30;
  167.        delay(pauseBetweenNotes);
  168.  
  169.        // stop the tone playing:
  170.        buzz(melodyPin, 0,noteDuration);
  171.  
  172.     }
  173.   }
  174. }
  175.  
  176. void buzz(int targetPin, long frequency, long length) {
  177.   digitalWrite(13,HIGH);
  178.   long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  179.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  180.   //// there are two phases to each cycle
  181.   long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  182.   //// multiply frequency, which is really cycles per second, by the number of seconds to
  183.   //// get the total number of cycles to produce
  184.   for (long i=0; i < numCycles; i++){ // for the calculated length of time...
  185.     digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
  186.     delayMicroseconds(delayValue); // wait for the calculated delay value
  187.     digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
  188.     delayMicroseconds(delayValue); // wait again or the calculated delay value
  189.   }
  190.   digitalWrite(13,LOW);
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement