Advertisement
Whoneon

Mozart "Eine kleine Nachtmusik" I. Allegro for Arduino

Dec 9th, 2020 (edited)
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Mozart "Eine kleine Nachtmusik" I. Allegro per Arduino - Antonio Guastella, M05000889
  3.  * Si ringrazia il sito internet https://sparks.gogo.co.nz/ per avermi fornito uno strumento in grado di estrarre gli array contenenti  le note e le rispettive durate dal file midi fornitogli in input
  4.  */
  5.  //Dichiaro il pin a cui collegherò il buzzer
  6. static const uint8_t tonePin = 4;
  7.  
  8. void setup()
  9. {
  10. //Niente da dichiarare qui
  11. }
  12.  
  13. void loop()
  14. {
  15. //Cambiando il valore di tempoToPlay si modifica la velocità con cui la melodia viene riprodotta, dove 1 indica la velocità massima e 255 quella più lenta
  16. //Il valore di melodyToPlay invece indica la traccia da ripodurre. Nel nostro caso la melodia 0 sarebbe la mano "destra" del pianista, mentre la 1 la mano "sinistra"
  17. //Avendo a disposizione due buzzer sarebbe possibile riprodurre entrambe le tracce in simultanea...
  18.  
  19.   uint8_t melodyToPlay = 0;
  20.   uint8_t tempoToPlay  = 1;
  21.  
  22.   playMelody(melodyToPlay, tempoToPlay);  
  23. }
  24. //Un po di valori: tonalità e durata di ogni nota
  25. #include <avr/pgmspace.h>
  26. #define NUMBER_OF_MELODIES 2
  27. static const uint16_t Melody0[] PROGMEM = {
  28.         0b1111111101100111, 0b1111111101100111, 0b1111111101100111, 0b1111111101100111,
  29.         0b0000010001100111, 0b1111111111111111, 0b1111111111111111, 0b0000001011111111,
  30.         0b1111111101100010, 0b1111111101100010, 0b0000001001100010, 0b1111111101100111,
  31.         0b1111111101100111, 0b1111111101100111, 0b1111111101100111, 0b0000010001100111,
  32.         0b1111111111111111, 0b1111111111111111, 0b0000001011111111, 0b1111111101100010,
  33.         0b1111111101100010, 0b0000001001100010, 0b1111111101100111, 0b1111111101100111,
  34.         0b0000001001100111, 0b1111111101100010, 0b1111111101100010, 0b0000001001100010,
  35.         0b1111111101100111, 0b1111111101100111, 0b0000001001100111, 0b1111111101101011,
  36.         0b1111111101101011, 0b0000001001101011, 0b1111111101110010, 0b1111111101110010,
  37.         0b1111111101110010, 0b1111111101110010, 0b0000010001110010, 0b1111111111111111,
  38.         0b1111111111111111, 0b1111111111111111, 0b1111111111111111, 0b1111111101110000,
  39.         0b1111111101110000, 0b1111111101110000, 0b1111111101110000, 0b0000010001110000,
  40.         0b1111111111111111, 0b1111111111111111, 0b0000001011111111, 0b1111111101101001,
  41.         0b1111111101101001, 0b0000001001101001, 0b1111111101110000, 0b1111111101110000,
  42.         0b1111111101110000, 0b1111111101110000, 0b0000010001110000, 0b1111111111111111,
  43.         0b1111111111111111, 0b0000001011111111, 0b1111111101101001, 0b1111111101101001,
  44.         0b0000001001101001, 0b1111111101110000, 0b1111111101110000, 0b0000001001110000,
  45.         0b1111111101101001, 0b1111111101101001, 0b0000001001101001, 0b1111111101100110,
  46.         0b1111111101100110, 0b0000001001100110, 0b1111111101101001, 0b1111111101101001,
  47.         0b0000001001101001, 0b1111111101100010, 0b1111111101100010, 0b1111111101100010,
  48.         0b1111111101100010, 0b0000010001100010, 0b1111111111111111, 0b1111111111111111,
  49.         0b1111111111111111, 0b1111111111111111, 0b1111111101100111, 0b1111111101100111,
  50.         0b0000001001100111, 0b1111111111111111, 0b1111111111111111, 0b0000001011111111,
  51.         0b1111111101100111, 0b1111111101100111, 0b1111111101100111, 0b1111111101100111,
  52.         0b1111111101100111, 0b1111111101100111, 0b0000011001100111, 0b1111111101101011,
  53.   };
  54. static const uint16_t Melody0_Length    = sizeof( Melody0 ) / sizeof(uint16_t);
  55.  
  56. //Stessa cosa ma riferita alla seconda traccia
  57. static const uint16_t Melody1[] PROGMEM = {
  58.         0b0011101101000111, 0b0011101101000010, 0b1111111100110111, 0b1111111100110111,
  59.         0b1111111100110111, 0b1011111100110111, 0b1111111111111111, 0b1111111111111111,
  60.         0b0000001011111111, 0b1111111101000010, 0b1111111101000010, 0b0000001001000010,
  61.         0b1111111101000111, 0b1111111101000111, 0b1111111101000111, 0b1111111101000111,
  62.         0b0000010001000111, 0b1111111111111111, 0b1111111111111111, 0b0000001011111111,
  63.         0b1111111101000010, 0b1111111101000010, 0b0000001001000010, 0b1111111101000111,
  64.         0b1111111101000111, 0b0000001001000111, 0b1111111101000010, 0b1111111101000010,
  65.         0b0000001001000010, 0b1111111101000111, 0b1111111101000111, 0b0000001001000111,
  66.         0b1111111101001011, 0b1111111101001011, 0b0000001001001011, 0b1111111101010010,
  67.         0b1111111101010010, 0b1111111101010010, 0b1111111101010010, 0b0000010001010010,
  68.         0b1111111111111111, 0b1111111111111111, 0b1111111111111111, 0b1111111111111111,
  69.         0b1111111101010000, 0b1111111101010000, 0b1111111101010000, 0b1111111101010000,
  70.         0b0000010001010000, 0b1111111111111111, 0b1111111111111111, 0b0000001011111111,
  71.         0b1111111101001001, 0b1111111101001001, 0b0000001001001001, 0b1111111101010000,
  72.         0b1111111101010000, 0b1111111101010000, 0b1111111101010000, 0b0000010001010000,
  73.         0b1111111111111111, 0b1111111111111111, 0b0000001011111111, 0b1111111101001001,
  74.         0b1111111101001001, 0b0000001001001001, 0b1111111101010000, 0b1111111101010000,
  75.         0b0000001001010000, 0b1111111101001001, 0b1111111101001001, 0b0000001001001001,
  76.         0b1111111101000110, 0b1111111101000110, 0b0000001001000110, 0b1111111101001001,
  77.         0b1111111101001001, 0b0000001001001001, 0b1111111101010010, 0b1111111101010010,
  78.         0b1111111101010010, 0b1111111101010010, 0b0000010001010010, 0b1111111111111111,
  79.         0b1111111111111111, 0b1111111111111111, 0b1111111111111111, 0b1111111101001011,
  80.         0b0000000101001011, 0b1111111101010010, 0b0000000101010010, 0b1111111101001011,
  81.         0b0000000101001011, 0b1111111101010010, 0b0000000101010010, 0b1111111101001011,
  82.         0b0000000101001011, 0b1111111101010010, 0b0000000101010010, 0b1111111101001011,
  83.   };
  84. static const uint16_t Melody1_Length    = sizeof( Melody1 ) / sizeof(uint16_t);
  85. void playMelody_Data(const uint16_t MelodyData[], const uint16_t MelodyLength, const uint8_t tempo)
  86. {
  87.   // Frequenze dell'ottava C8 a B8, da cui calcolo le ottave più basse (temperamento equabile, ogni nota è proporzionale all'altra mediante un fattore radice dodicesima di due)
  88.   static const uint16_t Freq8[] PROGMEM = { 4186 , 4435 , 4699  , 4978 , 5274 , 5588 , 5920 , 6272 , 6645 , 7040 , 7459 , 7902 };
  89.  
  90.   for(uint16_t  x = 0; x < MelodyLength; x++)
  91.   {
  92.     uint16_t data = pgm_read_word((uint16_t *)&MelodyData[x]);
  93.     if((data & 0xF) == 0xF)
  94.     {
  95.       noTone(tonePin);
  96.     }
  97.     else
  98.     {
  99.       uint16_t Freq = pgm_read_word(&Freq8[data&0xF]) / ( 1 << (8-(data>>4 & 0xF)) );
  100.       tone(tonePin, Freq);    
  101.     }    
  102.     int16_t Duration = data>>8;
  103.     while(Duration--) delay(tempo);
  104.   }
  105. }
  106.  
  107. inline static void playMelody(uint8_t melodyNumber, uint8_t tempo)
  108. {
  109.   switch(melodyNumber)
  110.   {
  111.   #if NUMBER_OF_MELODIES > 0
  112.     case 0: { playMelody_Data(Melody0, Melody0_Length, tempo); return; }
  113.   #endif
  114.   #if NUMBER_OF_MELODIES > 1
  115.     case 1: { playMelody_Data(Melody1, Melody1_Length, tempo); return; }
  116.   #endif
  117.   }
  118. }
  119.  
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement