Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <SoftwareSerial.h>
- #define VS1053_RX 3 // This is the pin that connects to the RX pin on VS1053
- #define VS1053_RESET A0 // This is the pin that connects to the RESET pin on VS1053
- // If you have the Music Maker shield, you don't need to connect the RESET pin!
- #define SW1 4
- #define SW2 5
- #define SW3 6
- #define SW4 7
- #define SW5 8
- #define SW6 9
- #define SW7 10
- #define SW8 11
- #define SW9 12
- // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 31
- #define VS1053_BANK_DEFAULT 0x00
- #define VS1053_BANK_DRUMS1 0x78
- #define VS1053_BANK_DRUMS2 0x7F
- #define VS1053_BANK_MELODY 0x79
- // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 32 for more!
- #define VS1053_GM1_OCARINA 80
- #define MIDI_NOTE_ON 0x90
- #define MIDI_NOTE_OFF 0x80
- #define MIDI_CHAN_MSG 0xB0
- #define MIDI_CHAN_BANK 0x00
- #define MIDI_CHAN_VOLUME 0x07
- #define MIDI_CHAN_PROGRAM 0xC0
- SoftwareSerial VS1053_MIDI(0, VS1053_RX); // TX only, do not use the 'rx' side
- // on a Mega/Leonardo you may have to change the pin to one that
- // software serial support uses OR use a hardware serial port!
- //czestotliwosci dzwiekow
- #define n_c 60
- #define n_ch 61
- #define n_d 62
- #define n_dh 63
- #define n_e 64
- #define n_f 65
- #define n_fh 66
- #define n_g 67
- #define n_gh 68
- #define n_a 69
- #define n_ah 70
- #define n_h 71
- #define n_2c 72
- #define n_2ch 73
- #define n_2d 74
- #define n_2dh 75
- #define n_2e 76
- #define n_2f 77
- #define n_2fh 78
- #define n_2g 79
- #define n_2gh 80
- #define n_2a 81
- #define n_2ah 82
- #define n_2h 83
- #define n_3c 84
- #define n_3ch 85
- #define n_n 255 //pauza
- //dlugosci dzwiekow
- #define n_1 0xF000 //cala nuta
- #define n_2 0x7000 //polnuta
- #define n_4 0x3000 //cwiercnuta
- #define n_8 0x1000 //osemka
- #define n_16 0x0000 //szesnastka
- #define n_2k 0xB000 //polnuta z kropka
- #define n_4k 0x5000 //cwiercnuta z kropka
- #define n_8k 0x2000 //osemka z kropka
- #define N_T 50 //tempo melodii
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, A1, NEO_GRB + NEO_KHZ800);
- byte laststate[9] = {1,1,1,1,1,1,1,1,1};
- byte laststate2[3] = {1,1,1};
- const byte pentatonic_major_c[9] = {n_c,n_d,n_e,n_g,n_a,n_2c,n_2d,n_2e,n_2g};
- const byte pentatonic_minor_c[9] = {n_c,n_dh,n_f,n_g,n_ah,n_2c,n_2dh,n_2f,n_2g};
- const byte percussion[9] = {46,49,44,45,36,50,38,51,41};
- byte scale[9] = {n_c,n_d,n_e,n_g,n_a,n_2c,n_2d,n_2e,n_2g};
- uint32_t colors[9];
- void setup()
- {
- pinMode(SW1, INPUT_PULLUP);
- pinMode(SW2, INPUT_PULLUP);
- pinMode(SW3, INPUT_PULLUP);
- pinMode(SW4, INPUT_PULLUP);
- pinMode(SW5, INPUT_PULLUP);
- pinMode(SW6, INPUT_PULLUP);
- pinMode(SW7, INPUT_PULLUP);
- pinMode(SW8, INPUT_PULLUP);
- pinMode(SW9, INPUT_PULLUP);
- pinMode(A2, INPUT_PULLUP);
- pinMode(A3, INPUT_PULLUP);
- pinMode(A4, INPUT_PULLUP);
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- colors[0] = strip.Color( 127, 0, 0);
- colors[1] = strip.Color( 100, 30, 0);
- colors[2] = strip.Color( 70, 60, 0);
- colors[3] = strip.Color( 60, 127, 0);
- colors[4] = strip.Color( 0, 127, 0);
- colors[5] = strip.Color( 0, 127, 60);
- colors[6] = strip.Color( 0, 60, 127);
- colors[7] = strip.Color( 0, 30, 127);
- colors[8] = strip.Color( 0, 0, 127);
- VS1053_MIDI.begin(31250); // MIDI uses a 'strange baud rate'
- pinMode(VS1053_RESET, OUTPUT);
- digitalWrite(VS1053_RESET, LOW);
- delay(10);
- digitalWrite(VS1053_RESET, HIGH);
- delay(10);
- midiSetChannelBank(0, VS1053_BANK_MELODY);//VS1053_BANK_MELODY
- midiSetInstrument(0, 3);//3
- midiSetChannelVolume(0, 255);
- setScale(pentatonic_major_c); //pentatonic_minor_c
- strip.setPixelColor(0, strip.Color( 60, 60, 0));
- strip.show();
- delay(100);
- for(uint8_t k =1; k<9;k++){
- strip.setPixelColor(k-1, strip.Color( 0, 0, 0));
- strip.setPixelColor(k, strip.Color( 60, 60, 0));
- strip.show();
- delay(100);
- }
- colorWipe(strip.Color(0, 0, 0));
- strip.show();
- paluszek();
- }
- //C D E G A C
- void loop()
- {
- byte sw[9];
- for(int k=0;k<9;k++)
- sw[k] = digitalRead(4+k);
- for(int l=0;l<9;l++){
- if(sw[l] != laststate[l]){
- if(sw[l] == LOW){
- midiNoteOn(0, scale[l], 127);
- strip.setPixelColor(l, colors[l]);
- }else{
- midiNoteOff(0, scale[l], 127);
- strip.setPixelColor(l, 0);
- }
- laststate[l] = sw[l];
- }
- }
- byte sw2[3];
- sw2[0] = digitalRead(A2);
- sw2[1] = digitalRead(A3);
- sw2[2] = digitalRead(A4);
- if(sw2[0] != laststate2[0]){
- if(sw2[0] == LOW){
- //on press
- setScale(pentatonic_minor_c); //pentatonic_minor_c
- }else{
- //on release
- setScale(pentatonic_major_c);
- }
- laststate2[0] = sw2[0];
- }
- if(sw2[1] != laststate2[1]){
- if(sw2[1] == LOW){
- //on press
- midiSetInstrument(0, 1);//3
- }else{
- //on release
- midiSetInstrument(0, 3);//3
- }
- laststate2[1] = sw2[1];
- }
- if(sw2[2] != laststate2[2]){
- if(sw2[2] == LOW){
- //on press
- midiSetChannelBank(0, VS1053_BANK_DRUMS1);
- midiSetInstrument(0, 1);//3
- setScale(percussion);
- }else{
- //on release
- midiSetChannelBank(0, VS1053_BANK_MELODY);
- midiSetInstrument(0, 3);//3
- setScale(pentatonic_major_c);
- }
- laststate2[2] = sw2[2];
- }
- /*
- if(digitalRead(SW1) == LOW) {
- tone(SPK,n_c);
- strip.setPixelColor(0, strip.Color( 127, 0, 0));
- }else if(digitalRead(SW2) == LOW) {
- tone(SPK,n_d);
- strip.setPixelColor(1, strip.Color( 100, 30, 0));
- }else if(digitalRead(SW3) == LOW) {
- tone(SPK,n_e);
- strip.setPixelColor(2, strip.Color( 70, 60, 0));
- }else if(digitalRead(SW4) == LOW) {
- tone(SPK,n_g);
- strip.setPixelColor(3, strip.Color( 60, 127, 0));
- }else if(digitalRead(SW5) == LOW) {
- tone(SPK,n_a);
- strip.setPixelColor(4, strip.Color( 0, 127, 0));
- }else if(digitalRead(SW6) == LOW) {
- tone(SPK,n_2c);
- strip.setPixelColor(5, strip.Color( 0, 127, 60));
- }else if(digitalRead(SW7) == LOW) {
- tone(SPK,n_2d);
- strip.setPixelColor(6, strip.Color( 0, 60, 127));
- }else if(digitalRead(SW8) == LOW) {
- tone(SPK,n_2e);
- strip.setPixelColor(7, strip.Color( 0, 30, 127));
- }else if(digitalRead(SW9) == LOW) {
- tone(SPK,n_2g);
- strip.setPixelColor(8, strip.Color( 0, 0, 127));
- }else {
- noTone(SPK);
- colorWipe(strip.Color(0, 0, 0));
- }
- */
- strip.show();
- delay(10);
- }
- void colorWipe(uint32_t c) {
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- }
- }
- void midiSetInstrument(uint8_t chan, uint8_t inst) {
- if (chan > 15) return;
- inst --; // page 32 has instruments starting with 1 not 0 :(
- if (inst > 127) return;
- VS1053_MIDI.write(MIDI_CHAN_PROGRAM | chan);
- VS1053_MIDI.write(inst);
- }
- void midiSetChannelVolume(uint8_t chan, uint8_t vol) {
- if (chan > 15) return;
- if (vol > 127) return;
- VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
- VS1053_MIDI.write(MIDI_CHAN_VOLUME);
- VS1053_MIDI.write(vol);
- }
- void midiSetChannelBank(uint8_t chan, uint8_t bank) {
- if (chan > 15) return;
- if (bank > 127) return;
- VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
- VS1053_MIDI.write((uint8_t)MIDI_CHAN_BANK);
- VS1053_MIDI.write(bank);
- }
- void midiNoteOn(uint8_t chan, uint8_t n, uint8_t vel) {
- if (chan > 15) return;
- if (n > 127) return;
- if (vel > 127) return;
- VS1053_MIDI.write(MIDI_NOTE_ON | chan);
- VS1053_MIDI.write(n);
- VS1053_MIDI.write(vel);
- }
- void midiNoteOff(uint8_t chan, uint8_t n, uint8_t vel) {
- if (chan > 15) return;
- if (n > 127) return;
- if (vel > 127) return;
- VS1053_MIDI.write(MIDI_NOTE_OFF | chan);
- VS1053_MIDI.write(n);
- VS1053_MIDI.write(vel);
- }
- void kielbasa(void)
- {
- const unsigned int kielbasa[15] = {n_e|n_4, //4
- n_g|n_4,
- n_a|n_4k,
- n_e|n_8,
- n_n|n_8,
- n_g|n_8,
- n_n|n_8,
- n_ah|n_8,
- n_a|n_2, //2
- n_e|n_4,
- n_g|n_4,
- n_a|n_4k,
- n_g|n_8,
- n_n|n_8,
- n_e|n_2k //2k
- };
- for(char i=0; i<15;i++)
- nutka(kielbasa[i], N_T*2);
- }
- void paluszek(void){
- 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};
- for(char i=0; i<8;i++)
- nutka(paluszek[i], N_T);
- }
- void nutka(unsigned int a, unsigned int t)
- {
- unsigned int czas = (t * (a>>12)) + t;
- if((a&0x0FFF)) midiNoteOn(0, (a & 0x0FFF), 127);
- delay(czas);
- midiNoteOff(0, (a & 0x0FFF), 127);
- delay(czas/10);
- }
- void setScale(const byte new_scale[9])
- {
- uint8_t z;
- for(z=0;z<9;z++)
- scale[z]=new_scale[z];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement