midi система на ардуино mega 2560
- Войдите на сайт для отправки комментариев
Пнд, 18/11/2019 - 11:17
Здравствуйте
Проект выполнен процентов на 70ита
midi система установлена в баян
arduino mega прошита
mocoLUFA что дает возможность передавать ноты
передавать ноты без переходников .Это как раз просто было .А вот теперь нужен midi разъем СГ 5 для синтезатора .Так как midi система уже работает с андройд виндовс и звуковым модулем без переходников на чистой arduino mega через usb порт. Нужно параллельно прикрутить midi разъем СГ 5 с использованием подключенной библиотеки midi.h. В интернете есть примеры но применить их к готовому коду не получается. Необходимо реализовать возможность отправки сообщений (ноты) через разъем сг 5 с сохранением отправки через usb порт
01 nclude <MIDI.h> 02 #include <midi_Defs.h> 03 #include <midi_Message.h> 04 #include <midi_Namespace.h> 05 #include <midi_Settings.h> 06 struct MySettings : public midi::DefaultSettings 07 { 08 //By default, the Arduino MIDI Library tries to be smart by 09 //excluding the CC byte if it doesn't change (to save bandwidth). 10 //This is a problem when starting up Serial<->MIDI software 11 //after starting up the Arduino because we miss the first CC byte. 12 //Setting UseRunningStatus to false removes this "feature." 13 //See <a href="https://github.com/projectgus/hairless-midiserial/issues/16" rel="nofollow">https://github.com/projectgus/hairless-midiserial/issues/16</a> for details. 14 static const bool UseRunningStatus = false; 15 // Set MIDI baud rate. MIDI has a default baud rate of 31250, 16 // but we're setting our baud rate higher so that the Serial<->MIDI software 17 // can properly decode and read outgoing MIDI data on the computer. 18 static const long BaudRate = 31250; 19 }; 20 21 //#define DEBUG//uncomment this line to print serial messages, comment to send MIDI data 22 //#define BLUETOOTH//uncomment this line to send MIDI data via bluetooth instead of USB 23 //#define BMP//uncomment this line to use the BMP180 to add dynamics via bellows 24 //#define JOYSTICK//uncomment this line to use a joystick as a pitch-bend controller 25 26 #ifdef BLUETOOTH 27 MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, MySettings); 28 #else 29 MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings); 30 #endif 01 void setup() 02 { 03 #ifdef DEBUG 04 Serial.begin(9600); 05 #else 06 MIDI.begin(); 07 //If we're sending MIDI over Serial1, open Serial for additional debugging 08 #ifdef BLUETOOTH 09 Serial.begin(9600); 10 #endif 11 #endif 12 //Digital pins start turned off 13 for (int i=0; i<sizeof(left_hand_pins);i++){ 14 pinMode(left_hand_pins[i],OUTPUT); 15 digitalWrite(left_hand_pins[i], LOW); 16 } 17 for (int i=0; i<sizeof(right_hand_pins);i++){ 18 pinMode(right_hand_pins[i],OUTPUT); 19 digitalWrite(right_hand_pins[i], LOW); 20 } кодрасечатать? 01 void loop() 02 { 03 #ifdef BMP 04 //Read pressure from the BMP_180 and convert it to MIDI expression 05 int expression = get_expression(prev_expression); 06 07 //Ignore it if it didn't change 08 if(expression != prev_expression) { 09 expression_avg[e] = expression; 10 //Only send MIDI CC every bmp_sample_rate times, 11 //but send the average of the last bmp_sample_rate deltas 12 if (e == bmp_sample_rate - 1){ 13 expression = 0; 14 for (int i=0; i<bmp_sample_rate; i++){ 15 expression += expression_avg[i]; 16 } 17 expression = expression/bmp_sample_rate; 18 19 #ifdef DEBUG 20 Serial.print("Expression Change: "); 21 Serial.println(expression); 22 #else 23 MIDI.sendControlChange(CC_Expression,expression,1); 24 //Don't let bass overpower melody 25 MIDI.sendControlChange(CC_Expression,constrain(expression-6,0,127),2); 26 //Don't let chords overpower melody 27 MIDI.sendControlChange(CC_Expression,constrain(expression-12,0,127),3); 28 #endif 29 prev_expression = expression; 30 e = 0; 31 } 32 else { 33 e = e + 1; 34 } 35 } 36 #endif 37 38 #ifdef JOYSTICK 39 int pitch_bend_val = scan_joystick(); 40 if(pitch_bend_val != joystick_prev_val) { 41 //Comment and uncomment to select which channels you want pitch bend to affect. 42 MIDI.sendPitchBend(pitch_bend_val, 1); 43 //MIDI.sendPitchBend(pitch_bend_val, 2); 44 //MIDI.sendPitchBend(pitch_bend_val, 3); 45 joystick_prev_val = pitch_bend_val; 46 } 47 #endif 48 49 //Alternate between scanning the left and right hand pins 50 //to reduce necessary delay between reads 51 for (int i=0; i<9;i++){ 52 scan_pin(right_hand_pins[i], i, RightKeysStatus[i], false); 53 scan_pin(left_hand_pins[i%3], i%3, LeftKeysStatus[i%3], true); 54 } 55 } 56 57 byte reg_values = 0; 58 59 //Read the analog port value for the given pin 60 //If something changed, trigger MIDI signal 61 void scan_pin(int pin, int index, byte PinStatus, bool left) { 62 //TODO - I wonder if we can replace this with direct port write for even better performance? 63 digitalWrite(pin, HIGH); 64 //A slight delay is needed here or else we'll be reading the previous pin 65 delayMicroseconds(300);//was able to cut this in half by alternating between left and right 66 if (left) { 67 reg_values = ~PINL; 68 } 69 else { 70 reg_values = ~PINC; 71 } 72 digitalWrite(pin, LOW); 73 74 //check if something changed 75 if (reg_values != PinStatus){ 76 //if the byte value is greater, we're turning the note on; else, turning it off. 77 if (reg_values > PinStatus){ 78 //using bit-wise OR to send modified bits only 79 check_key(reg_values ^ PinStatus, index, true, left); 80 } 81 else { 82 check_key(reg_values ^ PinStatus, index, false, left); 83 } 84 } 85 }
85 строк программы при вставке превратились в 300 (((
Попробуйте к пину TX0 подключиться по этой схеме
https://www.arduino.cc/en/Tutorial/Midi
Не уверен в работоспособности этой схемы, но попробовать можно. В теории, у вас сигнал будет уходить и на USB и на сг-5 параллельно.