Advertisement
gabry1609

ChristmasDuino - codice

Dec 31st, 2013
2,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Christmas tunes with dancing lights  
  3.     by Raj Madhuram ( http://geekraj.com )
  4.    
  5.     Adapted the melody code by Tom Igoe
  6.     Notes from various places (acknowledged inline)
  7.    
  8.  */
  9. #include "pitches.h"
  10.  
  11. typedef struct {
  12.    int *melody;
  13.    int *durations;
  14.    int numCount;
  15.    int tempo;
  16.    int low, high;
  17. } tune;
  18.  
  19. tune tunes[5];
  20.  
  21. // Jingle Bells
  22. int jingleBells[] = { N_E5, N_E5, N_E5, N_E5, N_E5, N_E5, N_E5, N_G5, N_C5, N_D5, N_E5,
  23.                       N_F5, N_F5, N_F5, N_F5, N_F5, N_E5, N_E5, N_E5, N_E5, N_D5, N_D5, N_E5, N_D5, N_G5,
  24.                       N_E5, N_E5, N_E5, N_E5, N_E5, N_E5, N_E5, N_G5, N_C5, N_D5, N_E5,
  25.                       N_F5, N_F5, N_F5, N_F5, N_F5, N_E5, N_E5, N_E5, N_G5, N_G5, N_F5, N_D5, N_C5};
  26. int jingleBellsDurations[] = { 4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 16,
  27.                                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8,
  28.                                4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 16,
  29.                                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 16 };
  30.                                
  31. // The first noel
  32. // http://www.music-for-music-teachers.com/support-files/first-noel-broken-chord-stretch-key-of-c.pdf
  33.  
  34. int firstNoel[] = { N_E4, N_D4, N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4, N_C5, N_B4, N_A4,
  35.                     N_G4, N_A4, N_B4, N_C5, N_B4, N_A4, N_G4, N_A4, N_B4, N_C5, N_G4, N_F4,
  36.                     N_E4, N_E4, N_D4, N_C4, N_D4, N_E4, N_F4, N_G4, N_A4, N_B4, N_C5, N_B4, N_A4,
  37.                     N_G4, N_A4, N_B4, N_C5, N_B4, N_A4, N_G4, N_A4, N_B4, N_C5, N_G4, N_F4,
  38.                     N_E4, N_E4, N_D4, N_C4, N_D4, N_E4, N_F4, N_G4, N_C5, N_B4, N_A4, N_A4,
  39.                     N_G4, N_C5, N_B4, N_A4, N_G4, N_A4, N_B4, N_C5, N_G4, N_F4, N_E4
  40.                   };
  41.  
  42. int firstNoelDurations[] = { 2, 2, 6, 2, 2, 2, 8, 2, 2, 4, 4, 4,
  43.                              8, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  44.                              8, 2, 2, 6, 2, 2, 2, 8, 2, 2, 4, 4, 4,
  45.                              8, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4,                            
  46.                              8, 2, 2, 6, 2, 2, 2, 8, 2, 2, 8, 4,
  47.                              12, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8  };
  48.                              
  49. // O Holy Night
  50. // http://www.music-for-music-teachers.com/support-files/o-holy-night-lead-sheet-c.pdf
  51.  
  52. int oHolyNight[] = { N_E4, N_E4, N_E4, N_G4, N_REST, N_G4, N_A4, N_A4, N_F4, N_A4, N_C5,
  53.                      N_G4, N_G4, N_E4, N_D4, N_C4, N_E4, N_F4, N_G4, N_F4, N_D4, N_C4,
  54.                      N_E4, N_E4, N_E4, N_G4, N_REST, N_G4, N_A4, N_A4, N_F4, N_A4, N_C5,
  55.                      N_G4, N_G4, N_FS4, N_E4, N_B4, N_G4, N_A4, N_B4, N_C5, N_B4, N_E4, N_REST, N_G4,
  56.                      N_G4, N_A4, N_D4, N_G4, N_A4, N_G4, N_C5, N_E4, N_A4, N_G4, N_G4, N_G4, N_A4,
  57.                      N_D4, N_G4, N_A4, N_G4, N_C5, N_E4, N_G4, N_C5, N_C5, N_B4, N_A4,
  58.                      N_B4, N_A4, N_REST, N_A4, N_D5, N_D5, N_A4, N_A4, N_A4, N_C5,
  59.                      N_C5, N_REST, N_C5, N_E5, N_D5, N_D5, N_G4, N_C5, N_C5, N_B4, N_A4,
  60.                      N_G4, N_G4, N_G4, N_A4, N_G4, N_G4, N_G4, N_C5, N_D5,
  61.                      N_D5, N_G4, N_E5, N_E5, N_D5, N_C5, N_B4, N_C5, N_D5,
  62.                      N_C5 };
  63.  
  64. int oHolyNightDurations[] = { 6, 4, 2, 8, 2, 2 , 4, 2, 4, 2, 12,
  65.                               4, 2, 4, 2, 6, 4, 2, 6, 4, 2, 24,
  66.                               6, 4, 2, 8, 2, 2 , 4, 2, 4, 2, 12,
  67.                               4, 2, 4, 2, 6, 4, 2, 6, 4, 2, 20, 2, 2,
  68.                               6, 6, 6, 6, 4, 2, 4, 2, 6, 4, 2, 6, 6,
  69.                               6, 6, 4, 2, 4, 2, 12, 12, 6, 4, 2,
  70.                               12, 8, 2, 2, 12, 4, 2, 4, 2, 12,
  71.                               8, 2, 2, 12, 8, 2, 2, 12, 6, 4, 2,
  72.                               12, 4, 2, 4, 2, 12, 6, 6, 12,
  73.                               6, 6, 12, 6, 6, 12, 6, 4, 2,
  74.                               24 };
  75.  
  76. // http://www.christmas-carol-music.org/Lead_Sheets/WeThreeKings.html
  77.  
  78. int weThreeKings[] = { N_B4, N_A4, N_G4, N_E4, N_FS4, N_G4, N_FS4, N_E4,
  79.                        N_B4, N_A4, N_G4, N_E4, N_FS4, N_G4, N_FS4, N_E4,
  80.                        N_G4, N_G4, N_G4, N_A4, N_A4, N_B4, N_B4, N_D5, N_C5, N_B4,
  81.                        N_A4, N_B4, N_A4, N_G4, N_FS4, N_E4,
  82.                        N_FS4, N_A4, N_G4, N_G4, N_G4, N_D4, N_G4, N_E4, N_G4, N_G4, N_G4, N_G4, N_D4, N_G4, N_E4, N_G4,
  83.                        N_G4, N_G4, N_A4, N_B4, N_C5, N_B4, N_A4, N_B4, N_G4, N_G4, N_G4, N_D4, N_G4, N_E4, N_G4 };
  84.                        
  85. int weThreeKingsDurations[] = { 4, 2, 4, 2, 2, 2, 2, 6,
  86.                                 4, 2, 4, 2, 2, 2, 2, 6,
  87.                                 2, 2, 2, 4, 2, 4, 2, 2, 2, 2,
  88.                                 2, 2, 2, 4, 2, 6,
  89.                                 6, 6, 4, 2, 4, 2, 4, 2, 6, 4, 2, 4, 2, 4, 2, 6,
  90.                                 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 6  };
  91.  
  92. // What child is this
  93. // http://www.free-scores.com/PDF_EN/traditional-what-child-is-this-39593.pdf
  94.  
  95. int whatChild[] = { N_E4, N_G4, N_A4, N_B4, N_C5, N_B4, N_A4, N_FS4, N_D4, N_E4, N_FS4, N_G4, N_E4, N_E4, N_DS4, N_E4, N_FS4, N_B3, N_REST,  N_E4,
  96.                     N_G4, N_A4, N_B4, N_C4, N_B4, N_A4, N_FS4, N_D4, N_E4, N_FS4, N_G4, N_FS4, N_E4, N_DS4, N_CS4, N_D4, N_E4, N_E4, N_REST,
  97.                     N_D5, N_D5, N_C5, N_B4, N_A4, N_FS4, N_D4, N_E4, N_FS4, N_G4, N_E4, N_E4, N_DS4, N_E4, N_FS4, N_DS4, N_B3, N_REST,
  98.                     N_D5, N_D5, N_C5, N_B4, N_A4, N_FS4, N_D4, N_E4, N_FS4, N_G4, N_FS4, N_E4, N_DS4, N_CS4, N_D4, N_E4, N_E4} ;
  99.  
  100. int whatChildDurations[] = { 2, 4, 2, 3, 1, 2, 4, 2, 3, 1, 2, 4, 2, 3, 1, 2, 6, 2, 2, 2,
  101.                              4, 2, 3, 1, 2, 4, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 6, 4, 2,
  102.                              6, 3, 1, 2, 4, 2, 3, 1, 2, 4, 2, 3, 1, 2, 4, 2, 4, 2,
  103.                              6, 3, 1, 2, 4, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 6, 4};
  104.  
  105. const int buttonPin = 2;     // the number of the pushbutton pin
  106. int buttonState = 0;
  107. int toneNum = 0;  
  108. int randomOffset = 0;
  109. int outputStartPin = 3;
  110. int continuousMode = 0;
  111.  
  112. void play(int tuneNum) {
  113.  
  114.   randomOffset = random(32);
  115.    
  116.   int numNotes = tunes[tuneNum].numCount;
  117.   int tempo = tunes[tuneNum].tempo;
  118.   for (int thisNote = 0; thisNote < numNotes; thisNote++) {
  119.    
  120.     int freq = tunes[tuneNum].melody[thisNote]*2;
  121.     int noteDuration = tempo*tunes[tuneNum].durations[thisNote];
  122.    
  123.     if (freq > 0) {
  124.        int n = (map(freq, tunes[tuneNum].low, tunes[tuneNum].high, 1, 31) + randomOffset)%31 + 1;
  125.        lightsUp(n);
  126.        tone(8, freq, noteDuration);
  127.        
  128.     } else {
  129.        // REST
  130.        delay(noteDuration);
  131.     }
  132.  
  133.     delay(noteDuration);
  134.     blankLights();
  135.    
  136.     // to distinguish the notes, set a minimum time between them.
  137.     // the note's duration + 30% seems to work well:
  138.     int pauseBetweenNotes = noteDuration * 0.30;    
  139.     delay(pauseBetweenNotes);    
  140.    
  141.     noTone(8);
  142.      
  143.   }  
  144. }
  145.  
  146. void lightSweep(int k, int nBits) {
  147.   for (int i=0; i<5; i++) {
  148.     lightsUp(k);
  149.     delay(50);
  150.     k = k<<1;
  151.     blankLights();
  152.     delay(20);
  153.   }
  154.  
  155.   k = k>>nBits;
  156.  
  157.   for (int i=0; i<5; i++) {
  158.     lightsUp(k);
  159.     delay(50);
  160.     k = k>>1;
  161.     blankLights();
  162.     delay(20);
  163.   }  
  164. }
  165.  
  166. void fromCenter() {
  167.   for (int i=0; i<2; i++) {
  168.     lightsUp(4);
  169.     delay(50);
  170.     blankLights();
  171.     delay(50);
  172.   }
  173.  
  174.   for (int i=0; i<4; i++) {
  175.     lightsUp(10);
  176.     delay(50);
  177.     blankLights();
  178.     delay(50);
  179.   }
  180.  
  181.   lightsUp(31);
  182.   delay(100);
  183.   blankLights();
  184. }
  185.  
  186. void lightEffect(int n) {
  187.   switch(n) {
  188.      case 0: for (int i=0; i<2; i++) { lightSweep(1, 1); }
  189.              break;        
  190.              
  191.      case 1: for (int i=0; i<2; i++) { lightSweep(7, 2); }
  192.              break;        
  193.          
  194.      case 2: for (int i=0; i<2; i++) { lightSweep(5, 2); }
  195.              break;  
  196.              
  197.      case 3: for (int i=0; i<2; i++) { fromCenter(); }
  198.              break;              
  199.   }
  200. }
  201.  
  202. void lightsUp(int n) {
  203.   int i = 0;
  204.   while (n>0 && i<5) {
  205.     if (n%2 == 1) {
  206.       digitalWrite(outputStartPin+i, HIGH);            
  207.     } else {
  208.       digitalWrite(outputStartPin+i, LOW);
  209.     }
  210.     n = n>>1;
  211.     i++;
  212.   }
  213. }
  214.  
  215. void blankLights() {
  216.   for (int i=0; i<5; i++) {
  217.     digitalWrite(i+outputStartPin, LOW);
  218.   }
  219. }
  220.  
  221. void setup() {
  222.  
  223.   int numTunes = 5;
  224.  
  225.   tunes[0].melody = jingleBells;
  226.   tunes[0].durations = jingleBellsDurations;
  227.   tunes[0].numCount = sizeof(jingleBells)/sizeof(int);
  228.   tunes[0].tempo = 60;
  229.  
  230.   tunes[1].melody = firstNoel;
  231.   tunes[1].durations = firstNoelDurations;
  232.   tunes[1].numCount = sizeof(firstNoel)/sizeof(int);  
  233.   tunes[1].tempo = 60;
  234.  
  235.   tunes[2].melody = oHolyNight;
  236.   tunes[2].durations = oHolyNightDurations;
  237.   tunes[2].numCount = sizeof(oHolyNight)/sizeof(int);    
  238.   tunes[2].tempo = 100;
  239.  
  240.   tunes[3].melody = weThreeKings;
  241.   tunes[3].durations = weThreeKingsDurations;
  242.   tunes[3].numCount = sizeof(weThreeKings)/sizeof(int);      
  243.   tunes[3].tempo = 100;
  244.  
  245.   tunes[4].melody = whatChild;
  246.   tunes[4].durations = whatChildDurations;
  247.   tunes[4].numCount = sizeof(whatChild)/sizeof(int);      
  248.   tunes[4].tempo = 100;  
  249.  
  250.   for (int i=0; i<numTunes; i++) {
  251.     int low = N_DS8;
  252.     int high = 0;
  253.     for (int j=0; j<tunes[i].numCount; j++) {
  254.       int freq = tunes[i].melody[j];
  255.       if (freq != 0 && freq<low) low = freq;
  256.       if (freq>high) high = freq;
  257.     }
  258.     tunes[i].high = high;
  259.     tunes[i].low = low;
  260.   }
  261.    
  262.   // initialize the pushbutton pin as an input:
  263.   pinMode(buttonPin, INPUT);    
  264.  
  265.   for (int i=0; i<5; i++) {
  266.      pinMode(outputStartPin + i, OUTPUT);
  267.   }
  268.  
  269. }
  270.  
  271. void playTune() {
  272.   int effectNum = random(4);
  273.   lightEffect(effectNum);
  274.   play(toneNum);
  275.   toneNum = (toneNum+1)%5;
  276.   lightEffect(effectNum);  
  277. }
  278.  
  279. void loop() {
  280.  
  281.   if (continuousMode) {
  282.     playTune();
  283.     delay(2000);
  284.    
  285.   } else {
  286.  
  287.     // read the state of the pushbutton value:
  288.     buttonState = digitalRead(buttonPin);
  289.  
  290.     // check if the pushbutton is pressed.
  291.     // if it is, the buttonState is HIGH:
  292.     if (buttonState == HIGH) {    
  293.       playTune();
  294.     }
  295.   }
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement