Advertisement
ripred

MarioTune.ino

May 24th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.38 KB | None | 0 0
  1. // 2 voices on 1 Arduino
  2. // demo by John Harrison
  3. // Nov 28, 2011
  4. // public domain
  5. // Modified by Dipto Pratyaksa
  6. // Added Mario tunes by Dipto Pratyaksa
  7. // Apr 3, 2013
  8. // v 1.1
  9.  
  10. // for the demo to work unmodified, hook up a 1Kish resistor to pin 11 and a 1Kish resistor to pin 12.
  11. // tie the other ends of the resistors together and hook those up to an amp and speaker.
  12. // or you could skip the resistors and hook up pin 11 to one side of a stereo amp and pin 12 to the other side
  13.  
  14. // You have to include pitches.h in the same folder as your .ino or .pde skecth file
  15. #include "pitches.h"
  16.  
  17. // as stated above,
  18. // connect amp/speaker to FIRST_TONE_PIN and subsequent.
  19. // for example, if FIRST_TONE_PIN is 8 and NUM_OF_VOICE is 2,
  20. // connect amp/speaker to arduino  pin 8 and pin 9.
  21. // if using the same amp/speaker for both pins, have each
  22. // pin go through a 1Kish resistor before being summed together
  23. #define FIRST_TONE_PIN 8
  24.  
  25. #define NUM_OF_VOICES 2
  26. #define LED_PIN 13
  27.  
  28. // a future version will support more than 2 simultaneous voices
  29.  
  30. // notes in the melody:
  31. int melody1[] = {
  32.   NOTE_E7, NOTE_E7,0,NOTE_E7,
  33.   0, NOTE_C7, NOTE_E7,0,
  34.   NOTE_G7,0,0,0,
  35.   NOTE_G6,0,0,0,
  36.  
  37.   NOTE_C7, 0, 0,NOTE_G6,
  38.   0, 0,NOTE_E6,0,
  39.   0, NOTE_A6, 0, NOTE_B6,
  40.   0, NOTE_AS6, NOTE_A6, 0,
  41.  
  42.   NOTE_G7, NOTE_E7, NOTE_G7,
  43.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  44.   0, NOTE_E7, 0,NOTE_C7,
  45.   NOTE_D7, NOTE_B6, 0,0,
  46.  
  47.   NOTE_C7, 0, 0,NOTE_G6,
  48.   0, 0,NOTE_E6,0,
  49.   0, NOTE_A6, 0, NOTE_B6,
  50.   0, NOTE_AS6, NOTE_A6, 0,
  51.  
  52.   NOTE_G7, NOTE_E7, NOTE_G7,
  53.   NOTE_A7, 0, NOTE_F7, NOTE_G7,
  54.   0, NOTE_E7, 0,NOTE_C7,
  55.   NOTE_D7, NOTE_B6, 0,0,
  56. };
  57.  
  58. int harmony1[] = {
  59.   NOTE_FS6, NOTE_FS6,0,NOTE_FS6,
  60.   0, NOTE_FS6, NOTE_FS6,0,
  61.   NOTE_B6,0,0,0,
  62.   NOTE_G6,0,0,0,
  63.  
  64.   NOTE_E6, 0, 0,NOTE_C6,
  65.   0, 0,NOTE_G5,0,
  66.   0, NOTE_C6, 0, NOTE_D6,
  67.   0, NOTE_CS6, NOTE_C6, 0,
  68.  
  69.   NOTE_C6, NOTE_G6, NOTE_B6,
  70.   NOTE_C7, 0, NOTE_A6, NOTE_B6,
  71.   0, NOTE_A6, 0,NOTE_E6,
  72.   NOTE_F6, NOTE_D6, 0,0,
  73.  
  74.   NOTE_E6, 0, 0,NOTE_C6,
  75.   0, 0,NOTE_G5,0,
  76.   0, NOTE_C6, 0, NOTE_D6,
  77.   0, NOTE_CS6, NOTE_C6, 0,
  78.  
  79.   NOTE_C6, NOTE_G6, NOTE_B6,
  80.   NOTE_C7, 0, NOTE_A6, NOTE_B6,
  81.   0, NOTE_A6, 0,NOTE_E6,
  82.   NOTE_F6, NOTE_D6, 0,0,
  83. };
  84.  
  85. int underworld_melody[] = {
  86.   NOTE_C5, NOTE_C6, NOTE_A4, NOTE_A5,
  87.   NOTE_AS4, NOTE_AS5, 0,
  88.   0,
  89.   NOTE_C5, NOTE_C6, NOTE_A4, NOTE_A5,
  90.   NOTE_AS4, NOTE_AS5, 0,
  91.   0,
  92.   NOTE_F4, NOTE_F5, NOTE_D4, NOTE_D5,
  93.   NOTE_DS4, NOTE_DS5, 0,
  94.   0,
  95.   NOTE_F4, NOTE_F5, NOTE_D4, NOTE_D5,
  96.   NOTE_DS4, NOTE_DS5, 0,
  97.   0, NOTE_DS5, NOTE_D5, NOTE_CS5,
  98.   NOTE_C5, NOTE_DS5,
  99.   NOTE_DS5, NOTE_GS4,
  100.   NOTE_G4, NOTE_CS5,
  101.   NOTE_C5, NOTE_FS5,NOTE_F5, NOTE_E4, NOTE_AS5, NOTE_A5,
  102.   NOTE_GS5, NOTE_DS5, NOTE_B4,
  103.   NOTE_AS4, NOTE_A4, NOTE_GS4,
  104.   0, 0, 0
  105. };
  106.  
  107. int underworld_melody1[] = {
  108.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  109.   NOTE_AS3, NOTE_AS4, 0,
  110.   0,
  111.   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
  112.   NOTE_AS3, NOTE_AS4, 0,
  113.   0,
  114.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  115.   NOTE_DS3, NOTE_DS4, 0,
  116.   0,
  117.   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
  118.   NOTE_DS3, NOTE_DS4, 0,
  119.   0, NOTE_DS4, NOTE_D4, NOTE_CS4,
  120.   NOTE_C4, NOTE_DS4,
  121.   NOTE_DS4, NOTE_GS3,
  122.   NOTE_G3, NOTE_CS4,
  123.   NOTE_C4, NOTE_FS4,NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
  124.   NOTE_GS4, NOTE_DS4, NOTE_B3,
  125.   NOTE_AS3, NOTE_A3, NOTE_GS3,
  126.   0, 0, 0
  127. };
  128. //melody notes in miliseconds
  129. int underworld_tempo[] = {
  130.   200, 200, 200, 200,
  131.   200, 200, 400,
  132.   800,
  133.   200, 200, 200, 200,
  134.   200, 200, 400,
  135.   800,
  136.   200, 200, 200, 200,
  137.   200, 200, 400,
  138.   800,
  139.   200, 200, 200, 200,
  140.   200, 200, 400,
  141.   400, 133, 133, 133,
  142.   400, 400,
  143.   400, 400,
  144.   400, 400,
  145.   133, 133, 133,133, 133, 133,
  146.   267, 267, 267,
  147.   267, 267, 267,
  148.   800, 800, 800
  149. };
  150.  
  151. int mel1Durations[] = {
  152.   150, 150, 150,150,
  153.   150, 150, 150,150,
  154.   150, 150,150,150,
  155.   150, 150,150,150,
  156.  
  157.   150, 150,150,150,
  158.   150, 150,150,150,
  159.   150, 150, 150,150,
  160.   150, 150, 150,150,
  161.  
  162.   200, 200, 200,
  163.   150, 150,150,150,
  164.   150, 150, 150,150,
  165.   150, 150, 150,150,
  166.  
  167.   150, 150, 150,150,
  168.   150, 150, 150,150,  
  169.   150, 150, 150,150,
  170.   150, 150, 150,150,
  171.  
  172.   200, 200, 200,
  173.   150, 150, 150,150,  
  174.   150, 150, 150,150,
  175.   150, 150, 150,150,
  176. };
  177.  
  178. int harm1Durations[] = {
  179.   150, 150, 150,150,
  180.   150, 150, 150,150,
  181.     150, 150, 150,150,
  182.   150, 150, 150,150,
  183.  
  184.   150, 150, 150,150,
  185.   150, 150, 150,150,  
  186.   150, 150, 150,150,
  187.   150, 150, 150,150,
  188.  
  189.   150, 150, 150,150,
  190.   150, 150, 150,150,  
  191.   150, 150, 150,150,
  192.   150, 150, 150,150,
  193.  
  194.   150, 150, 150,150,
  195.   150, 150, 150,150,  
  196.   150, 150, 150,150,
  197.   150, 150, 150,150,
  198.  
  199.   150, 150, 150,150,
  200.   150, 150, 150,150,  
  201.   150, 150, 150,150,
  202.   150, 150, 150,150,
  203. };
  204.  
  205. void setup() {
  206.   Serial.println("play");
  207.   Serial.begin(9600);
  208.   for (unsigned char i = 0; i < NUM_OF_VOICES; i++) {
  209.     pinMode(i+FIRST_TONE_PIN,OUTPUT);
  210.     Serial.println(i);
  211.   }
  212.  
  213.   //led indicator
  214.   pinMode(LED_PIN,OUTPUT);
  215.  
  216.   Serial.println("play");
  217.  
  218. }
  219.  
  220. void loop() {
  221.   // if we get here, we played the melody
  222.   // all we can do is pee in corner of circular room
  223.  
  224.   // OPTIONAL. in this test example, the harmony and the melody are initially the same
  225.   // so we raise the harmony up an octave and slow it down to 1/2 speed
  226.   //for (int j = 0; j < (sizeof(harmony1) / sizeof(int)); j++) {
  227.   //  harmony1[j] = harmony1[j] *2;
  228.   //  harm1Durations[j] = harm1Durations[j] * 2;
  229.   //  Serial.println(harmony1[j]);
  230.   //}
  231.   //for (int j = 0; j < (sizeof(melody1) / sizeof(int)); j++) {
  232.   //  melody1[j] = melody1[j] *2;
  233.   //  mel1Durations[j] = mel1Durations[j] * 2;
  234.   //  Serial.println(melody1[j]);
  235.   //}
  236.  
  237.   // call magic function to play both voices
  238.   playTune(melody1, sizeof(melody1)/sizeof(int), mel1Durations, harmony1, sizeof(harmony1)/sizeof(int), mel1Durations);
  239.   playTune(underworld_melody, sizeof(underworld_melody)/sizeof(int), underworld_tempo, underworld_melody1, sizeof(underworld_melody1)/sizeof(int), underworld_tempo);
  240. }
  241.  
  242. // no need to modify anything below for basic use
  243. void playTune(int melody[], int melLength, int melDurations[], int harmony[], int harmLength, int harmDurations[])
  244. {
  245.   unsigned int Counts[] = { 0, 0};
  246.   unsigned int Periods[] = { 0, 0};
  247.   unsigned char States[] = { 0, 0};
  248.  
  249.   unsigned long stopTimeNote1 = 0;
  250.   unsigned long stopTimeNote2 = 0;
  251.   unsigned char TimedPin = 0;
  252.   unsigned int CurrentCount = 0;
  253.   unsigned char i;
  254.   unsigned char indexMel = 0, indexHarm = 0;
  255.  
  256.   while (1) {
  257.     CurrentCount = Counts[0];
  258.     TimedPin = 0;
  259.     for (i=1;i<NUM_OF_VOICES;i++) {
  260.       if (Counts[i] < CurrentCount) {
  261.         TimedPin = i;
  262.         CurrentCount = Counts[i];
  263.       } // for
  264.     }
  265.     if (CurrentCount > 3)
  266.       delayMicroseconds(CurrentCount);
  267.     if (Periods[TimedPin] < 65535)
  268.       States[TimedPin] = !States[TimedPin];
  269.  
  270.     digitalWrite(LED_PIN, HIGH);
  271.     digitalWrite(FIRST_TONE_PIN+TimedPin,States[TimedPin]);
  272.     digitalWrite(LED_PIN, LOW);
  273.  
  274.     if (millis() >= stopTimeNote1) {
  275.       if (indexMel >= melLength)
  276.         break;
  277.       stopTimeNote1 = millis() + melDurations[indexMel];
  278.       Periods[0] = 1000000 / melody[indexMel++];
  279.  
  280.     }
  281.     if (millis() >= stopTimeNote2) {
  282.       if (indexHarm >= harmLength)
  283.         break;
  284.       stopTimeNote2 = millis() + harmDurations[indexHarm];
  285.       Periods[1] = 1000000 / harmony[indexHarm++];
  286.     }
  287.  
  288.     for (i = 0; i < NUM_OF_VOICES; i++) {
  289.       Counts[i] = Counts[i] - CurrentCount;
  290.     }
  291.     Counts[TimedPin] = Periods[TimedPin];
  292.   }
  293. }
  294.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement