Advertisement
olelek

grajace_pudelko_midi

Jan 22nd, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.11 KB | None | 0 0
  1.  
  2. #include <Adafruit_NeoPixel.h>
  3. #include <SoftwareSerial.h>
  4. #define VS1053_RX  3 // This is the pin that connects to the RX pin on VS1053
  5.  
  6. #define VS1053_RESET A0 // This is the pin that connects to the RESET pin on VS1053
  7. // If you have the Music Maker shield, you don't need to connect the RESET pin!
  8.  
  9. #define SW1  4
  10. #define SW2  5
  11. #define SW3  6
  12. #define SW4  7
  13. #define SW5  8
  14. #define SW6  9
  15. #define SW7  10
  16. #define SW8  11
  17. #define SW9  12
  18.  
  19. // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 31
  20. #define VS1053_BANK_DEFAULT 0x00
  21. #define VS1053_BANK_DRUMS1 0x78
  22. #define VS1053_BANK_DRUMS2 0x7F
  23. #define VS1053_BANK_MELODY 0x79
  24.  
  25. // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 32 for more!
  26. #define VS1053_GM1_OCARINA 80
  27.  
  28. #define MIDI_NOTE_ON  0x90
  29. #define MIDI_NOTE_OFF 0x80
  30. #define MIDI_CHAN_MSG 0xB0
  31. #define MIDI_CHAN_BANK 0x00
  32. #define MIDI_CHAN_VOLUME 0x07
  33. #define MIDI_CHAN_PROGRAM 0xC0
  34.  
  35.  
  36. SoftwareSerial VS1053_MIDI(0, VS1053_RX); // TX only, do not use the 'rx' side
  37. // on a Mega/Leonardo you may have to change the pin to one that
  38. // software serial support uses OR use a hardware serial port!
  39.  
  40.  
  41. //czestotliwosci dzwiekow
  42. #define  n_c  60
  43. #define  n_ch  61
  44. #define  n_d  62
  45. #define  n_dh  63
  46. #define  n_e  64
  47. #define  n_f  65
  48. #define  n_fh  66
  49. #define  n_g  67
  50. #define  n_gh  68
  51. #define  n_a  69
  52. #define  n_ah  70
  53. #define  n_h  71
  54.  
  55. #define  n_2c  72
  56. #define  n_2ch  73
  57. #define  n_2d  74
  58. #define  n_2dh  75
  59. #define  n_2e  76
  60. #define  n_2f  77
  61. #define  n_2fh  78
  62. #define  n_2g  79
  63. #define  n_2gh  80
  64. #define  n_2a  81
  65. #define  n_2ah  82
  66. #define  n_2h  83
  67.  
  68. #define  n_3c  84
  69. #define  n_3ch  85
  70.  
  71. #define n_n  255    //pauza
  72.  
  73. //dlugosci dzwiekow
  74. #define n_1 0xF000 //cala nuta
  75. #define n_2 0x7000 //polnuta
  76. #define n_4 0x3000 //cwiercnuta
  77. #define n_8 0x1000 //osemka
  78. #define n_16 0x0000  //szesnastka
  79.  
  80. #define n_2k 0xB000  //polnuta z kropka
  81. #define n_4k 0x5000  //cwiercnuta z kropka
  82. #define n_8k 0x2000  //osemka z kropka
  83.  
  84. #define N_T  50    //tempo melodii
  85.  
  86.  
  87. Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, A1, NEO_GRB + NEO_KHZ800);
  88.  
  89. byte laststate[9] = {1,1,1,1,1,1,1,1,1};
  90. byte laststate2[3] = {1,1,1};
  91. const byte pentatonic_major_c[9] = {n_c,n_d,n_e,n_g,n_a,n_2c,n_2d,n_2e,n_2g};
  92. const byte pentatonic_minor_c[9] = {n_c,n_dh,n_f,n_g,n_ah,n_2c,n_2dh,n_2f,n_2g};
  93. const byte percussion[9] = {46,49,44,45,36,50,38,51,41};
  94.  
  95. byte scale[9] = {n_c,n_d,n_e,n_g,n_a,n_2c,n_2d,n_2e,n_2g};
  96. uint32_t colors[9];
  97. void setup()
  98. {
  99.   pinMode(SW1, INPUT_PULLUP);
  100.   pinMode(SW2, INPUT_PULLUP);
  101.   pinMode(SW3, INPUT_PULLUP);
  102.   pinMode(SW4, INPUT_PULLUP);
  103.   pinMode(SW5, INPUT_PULLUP);
  104.   pinMode(SW6, INPUT_PULLUP);
  105.   pinMode(SW7, INPUT_PULLUP);
  106.   pinMode(SW8, INPUT_PULLUP);
  107.   pinMode(SW9, INPUT_PULLUP);
  108.  
  109.   pinMode(A2, INPUT_PULLUP);
  110.   pinMode(A3, INPUT_PULLUP);
  111.   pinMode(A4, INPUT_PULLUP);
  112.   strip.begin();
  113.   strip.show(); // Initialize all pixels to 'off'
  114.  
  115.   colors[0] = strip.Color(  127,   0, 0);
  116.   colors[1] = strip.Color(  100,   30, 0);
  117.   colors[2] = strip.Color(  70,   60, 0);
  118.   colors[3] = strip.Color(  60,   127, 0);
  119.   colors[4] = strip.Color(  0,   127, 0);
  120.   colors[5] = strip.Color(  0,   127, 60);
  121.   colors[6] = strip.Color(  0,   60, 127);
  122.   colors[7] = strip.Color(  0,   30, 127);
  123.   colors[8] = strip.Color(  0,   0, 127);
  124.  
  125.  
  126.   VS1053_MIDI.begin(31250); // MIDI uses a 'strange baud rate'
  127.  
  128.   pinMode(VS1053_RESET, OUTPUT);
  129.   digitalWrite(VS1053_RESET, LOW);
  130.   delay(10);
  131.   digitalWrite(VS1053_RESET, HIGH);
  132.   delay(10);
  133.  
  134.   midiSetChannelBank(0, VS1053_BANK_MELODY);//VS1053_BANK_MELODY
  135.   midiSetInstrument(0, 3);//3
  136.   midiSetChannelVolume(0, 255);
  137.   setScale(pentatonic_major_c); //pentatonic_minor_c
  138.  
  139.   strip.setPixelColor(0, strip.Color(  60,   60, 0));
  140.   strip.show();
  141.   delay(100);
  142.   for(uint8_t k =1; k<9;k++){
  143.   strip.setPixelColor(k-1, strip.Color(  0,   0, 0));
  144.   strip.setPixelColor(k, strip.Color(  60,   60, 0));
  145.   strip.show();
  146.   delay(100);  
  147.   }
  148.   colorWipe(strip.Color(0, 0, 0));
  149.   strip.show();
  150.       paluszek();
  151. }
  152.  
  153.  
  154. //C D E G A C
  155. void loop()
  156. {
  157.   byte sw[9];
  158.   for(int k=0;k<9;k++)
  159.     sw[k] = digitalRead(4+k);
  160.   for(int l=0;l<9;l++){
  161.     if(sw[l] != laststate[l]){
  162.       if(sw[l] == LOW){
  163.         midiNoteOn(0, scale[l], 127);
  164.         strip.setPixelColor(l, colors[l]);
  165.       }else{
  166.         midiNoteOff(0, scale[l], 127);
  167.         strip.setPixelColor(l, 0);
  168.       }
  169.      
  170.       laststate[l] = sw[l];
  171.     }
  172.   }
  173.  
  174.   byte sw2[3];
  175.   sw2[0] = digitalRead(A2);
  176.   sw2[1] = digitalRead(A3);
  177.   sw2[2] = digitalRead(A4);
  178.  
  179.   if(sw2[0] != laststate2[0]){
  180.       if(sw2[0] == LOW){
  181.         //on press
  182.         setScale(pentatonic_minor_c); //pentatonic_minor_c
  183.       }else{
  184.         //on release
  185.         setScale(pentatonic_major_c);
  186.       }
  187.      
  188.       laststate2[0] = sw2[0];
  189.     }
  190.    
  191.   if(sw2[1] != laststate2[1]){
  192.       if(sw2[1] == LOW){
  193.         //on press
  194.         midiSetInstrument(0, 1);//3
  195.       }else{
  196.         //on release
  197.         midiSetInstrument(0, 3);//3
  198.       }
  199.      
  200.       laststate2[1] = sw2[1];
  201.     }
  202.    
  203.   if(sw2[2] != laststate2[2]){
  204.       if(sw2[2] == LOW){
  205.         //on press
  206.         midiSetChannelBank(0, VS1053_BANK_DRUMS1);
  207.         midiSetInstrument(0, 1);//3
  208.         setScale(percussion);
  209.       }else{
  210.         //on release
  211.         midiSetChannelBank(0, VS1053_BANK_MELODY);
  212.         midiSetInstrument(0, 3);//3
  213.         setScale(pentatonic_major_c);
  214.       }
  215.      
  216.       laststate2[2] = sw2[2];
  217.     }
  218.  
  219.   /*
  220.   if(digitalRead(SW1) == LOW) {
  221.     tone(SPK,n_c);
  222.     strip.setPixelColor(0, strip.Color(  127,   0, 0));
  223.   }else if(digitalRead(SW2) == LOW) {
  224.     tone(SPK,n_d);
  225.     strip.setPixelColor(1, strip.Color(  100,   30, 0));
  226.   }else  if(digitalRead(SW3) == LOW) {
  227.     tone(SPK,n_e);
  228.     strip.setPixelColor(2, strip.Color(  70,   60, 0));  
  229.   }else if(digitalRead(SW4) == LOW) {
  230.     tone(SPK,n_g);
  231.     strip.setPixelColor(3, strip.Color(  60,   127, 0));  
  232.   }else if(digitalRead(SW5) == LOW) {
  233.     tone(SPK,n_a);
  234.     strip.setPixelColor(4, strip.Color(  0,   127, 0));
  235.   }else if(digitalRead(SW6) == LOW) {
  236.     tone(SPK,n_2c);
  237.     strip.setPixelColor(5, strip.Color(  0,   127, 60));
  238.   }else if(digitalRead(SW7) == LOW) {
  239.     tone(SPK,n_2d);
  240.     strip.setPixelColor(6, strip.Color(  0,   60, 127));
  241.   }else if(digitalRead(SW8) == LOW) {
  242.     tone(SPK,n_2e);
  243.     strip.setPixelColor(7, strip.Color(  0,   30, 127));
  244.   }else if(digitalRead(SW9) == LOW) {
  245.     tone(SPK,n_2g);
  246.     strip.setPixelColor(8, strip.Color(  0,   0, 127));
  247.   }else {
  248.     noTone(SPK);
  249.     colorWipe(strip.Color(0, 0, 0));
  250.   }
  251.   */
  252.  
  253.   strip.show();
  254.   delay(10);
  255.  
  256.  
  257.  
  258. }
  259.  
  260. void colorWipe(uint32_t c) {
  261.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  262.       strip.setPixelColor(i, c);
  263.   }
  264. }
  265.  
  266. void midiSetInstrument(uint8_t chan, uint8_t inst) {
  267.   if (chan > 15) return;
  268.   inst --; // page 32 has instruments starting with 1 not 0 :(
  269.   if (inst > 127) return;
  270.  
  271.   VS1053_MIDI.write(MIDI_CHAN_PROGRAM | chan);  
  272.   VS1053_MIDI.write(inst);
  273. }
  274.  
  275.  
  276. void midiSetChannelVolume(uint8_t chan, uint8_t vol) {
  277.   if (chan > 15) return;
  278.   if (vol > 127) return;
  279.  
  280.   VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
  281.   VS1053_MIDI.write(MIDI_CHAN_VOLUME);
  282.   VS1053_MIDI.write(vol);
  283. }
  284.  
  285. void midiSetChannelBank(uint8_t chan, uint8_t bank) {
  286.   if (chan > 15) return;
  287.   if (bank > 127) return;
  288.  
  289.   VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
  290.   VS1053_MIDI.write((uint8_t)MIDI_CHAN_BANK);
  291.   VS1053_MIDI.write(bank);
  292. }
  293.  
  294. void midiNoteOn(uint8_t chan, uint8_t n, uint8_t vel) {
  295.   if (chan > 15) return;
  296.   if (n > 127) return;
  297.   if (vel > 127) return;
  298.  
  299.   VS1053_MIDI.write(MIDI_NOTE_ON | chan);
  300.   VS1053_MIDI.write(n);
  301.   VS1053_MIDI.write(vel);
  302. }
  303.  
  304. void midiNoteOff(uint8_t chan, uint8_t n, uint8_t vel) {
  305.   if (chan > 15) return;
  306.   if (n > 127) return;
  307.   if (vel > 127) return;
  308.  
  309.   VS1053_MIDI.write(MIDI_NOTE_OFF | chan);
  310.   VS1053_MIDI.write(n);
  311.   VS1053_MIDI.write(vel);
  312. }
  313.  
  314. void kielbasa(void)
  315. {
  316.   const unsigned int kielbasa[15] = {n_e|n_4, //4
  317.                           n_g|n_4,
  318.                           n_a|n_4k,
  319.                           n_e|n_8,
  320.                           n_n|n_8,
  321.                           n_g|n_8,
  322.                           n_n|n_8,
  323.                           n_ah|n_8,
  324.                           n_a|n_2, //2
  325.                           n_e|n_4,
  326.                           n_g|n_4,
  327.                           n_a|n_4k,
  328.                           n_g|n_8,
  329.                           n_n|n_8,
  330.                           n_e|n_2k //2k
  331.                         };
  332.       for(char i=0; i<15;i++)
  333.       nutka(kielbasa[i], N_T*2);
  334.  
  335. }
  336.  
  337. void paluszek(void){
  338.   const unsigned int paluszek[8] = {n_c|n_2, n_e|n_4, n_g|n_4, n_a|n_4, n_g|n_4, n_e|n_4, n_d|n_4, n_c|n_2};
  339.       for(char i=0; i<8;i++)
  340.       nutka(paluszek[i], N_T);
  341.  
  342. }
  343.  
  344. void nutka(unsigned int a, unsigned int t)
  345. {
  346. unsigned int czas = (t * (a>>12)) + t;
  347. if((a&0x0FFF)) midiNoteOn(0, (a & 0x0FFF), 127);
  348. delay(czas);
  349. midiNoteOff(0, (a & 0x0FFF), 127);
  350. delay(czas/10);
  351. }
  352.  
  353. void setScale(const byte new_scale[9])
  354. {
  355.   uint8_t z;
  356.   for(z=0;z<9;z++)
  357.     scale[z]=new_scale[z];
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement